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
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
#include <bits/stdc++.h>
using namespace std;
#define fwd(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) fwd(i, 0, n)
#define all(X) X.begin(), X.end()
#define sz(X) int(size(X))
#define pb push_back
#define eb emplace_back
#define st first
#define nd second
using pii = pair<int, int>; using vi = vector<int>;
using ll = long long; using ld = long double;
#ifdef LOC
auto SS = signal(6, [](int) { *(int *)0 = 0; });
#define DTP(x, y) auto operator << (auto &o, auto a) -> decltype(y, o) { o << "("; x; return o << ")"; }
DTP(o << a.st << ", " << a.nd, a.nd);
DTP(for (auto i : a) o << i << ", ", all(a));
void dump(auto... x) { (( cerr << x << ", " ), ...) <<
'\n'; }
#define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x)
#else
#define deb(...) 0
#endif
 
const int K = 40000;
const int N = 1e9;
const int M = ((N + K -1) / K) + 10;

void solve(){
    int n, m; cin >> n >> m;
    vector<int> p(n);
    rep(i, n)cin >> p[i];
    vector<vector<pii>> g(n);
    vector<vector<pii>> revG(n);
    rep(i, m){
        int a, b, w; cin >> a >> b >> w;
        a--; b--;
        g[a].push_back({b, w});
        revG[b].push_back({a, w});
    }
    vector<array<bool, K+1>> reach(n);
    rep(i, n)rep(j, K+1)reach[i][j] = 0;
    reach[0][1] = 1;
    queue<pii> q;
    q.push({0, 1});
    while(!q.empty()){
        auto [v, d] = q.front(); q.pop();
        for(auto [u, w] : g[v]){
            if(1ll * d * w <= p[u] && d * w <= K && !reach[u][d * w]){
                reach[u][d*w] = 1;
                q.push({u, d*w});
            }
        }
    }
    // deb(reach);
    vector<array<int, M+1>> maxPow(n);
    rep(i, n)rep(j, M+1)maxPow[i][j] = 0;
    maxPow[n-1][1] = p[n-1];
    priority_queue<array<int, 3>> pq;
    pq.push({p[n-1], n-1, 1});
    while(!pq.empty()){
        auto [mp, v, mul] = pq.top(); pq.pop();
        if(maxPow[v][mul] == mp){
            for(auto [u, w] : revG[v]){
                int d = min(p[u], mp / w);
                if(1ll * mul * w <= M && maxPow[u][mul * w] < d){
                    maxPow[u][mul * w] = d;
                    pq.push({d, u, mul * w});
                }
            }
        }
    }
    rep(i, n){
        for(int j = M-1; j >= 0; j--){
            maxPow[i][j] = max(maxPow[i][j], maxPow[i][j+1]);
        }
    }
    // deb(maxPow);
    int res = -1;
    if(n == 1)res = 1;
    rep(v, n){
        for(auto [u, w] : g[v]){
            int j = M;
            for(int i = 1; i <= K; i++){
                if(!reach[v][i])continue;
                while(j > 0 && maxPow[u][j] < 1ll * i * w)j--;
                if(j == 0)break;
                res = max(res, i * w * j);
            }
        }
    }
    cout << res << "\n";
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout << fixed << setprecision(10);
    int t; cin >> t;
    while(t--)solve();
    return 0;
}