1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

#define FOR(i,p,q) for(int i=(p); i<=(q); ++i)
#define ROF(i,p,q) for(int i=(p); i>=(q); --i)
#define REP(i,q) for(int i=0; i<(q); ++i)
#define pb push_back
#define as assign
#define rz resize
#define Co const
#define all(X) X.begin(), X.end()
#define rall(X) X.rbegin(), X.rend()
#define sz(X) (int)(X.size())
#define ckmax(a,b) a=max(a,b)
#define ckmin(a,b) a=min(a,b)
#define V vector
typedef long long ll;
typedef mt19937_64 mt;
#ifndef UNCLE
 typedef basic_string<bool> vb;
 typedef basic_string<int> vi;
 typedef basic_string<ll> vl;
#else
 typedef V<bool> vb;
 typedef V<int> vi;
 typedef V<ll> vl;
#endif

int N,M, res=-1;
vi prz;
struct EdG{int v,w;};
V<V<EdG>> edg;
V<unordered_set<int>> vis;

void Input(){
    cin>>N>>M;
    prz.rz(N), edg.as(N,V<EdG>()), vis.as(N,unordered_set<int>());
    for(int &vv:prz) cin>>vv;
    FOR(i,0,N-2) ckmin(prz[i],prz[N-1]);
    int u,v,w;
    REP(_,M) cin>>u>>v>>w,--u,--v, edg[u].pb({v,w});
}
void Dfs(int v, int cw){
    if(v==N-1) ckmax(res,cw);
    vis[v].insert(cw);
    for(EdG &nv:edg[v]) if(cw<=prz[nv.v]/nv.w&&!vis[nv.v].count(cw*nv.w))
        Dfs(nv.v,cw*nv.w);
}
void Solve(){
    Input();
    res=-1;
    Dfs(0,1);
    cout<<res<<"\n";
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    int T; cin>>T;
    while(T--) Solve();
}