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
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define st first
#define nd second

void solve() {
    int n, m;
    cin >> n >> m;
    ll p[n];
    rep(i, n) {
        cin >> p[i];
    }
    queue<pair<int, ll>> q;
    set<ll> odw[n];
    vector<pair<int, ll>> graf[n];
    q.push({0, 1});
    odw[0].insert(1ll);
    rep(i, m) {
        int a, b;
        ll w;
        cin >> a >> b >> w;
        a--;
        b--;
        graf[a].pb({b, w});
    }
    ll ans = -1;
    while (!q.empty()) {
        pair<int, ll> para = q.front();
        q.pop();
        int v = para.st;
        ll waga = para.nd;
        if (v == n - 1) {
            ans = max(ans, waga);
        }
        for (auto kraw: graf[v]) {
            ll x = waga * kraw.nd;
            int w = kraw.st;
            if (p[w] < x) {
                continue;
            }
            if (odw[w].find(x) != odw[w].end()) {
                continue;
            }
            odw[w].insert(x);
            q.push({w, x});
        }
    }
    cout << ans << '\n';
    cout.flush();
}

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