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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#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(ssize(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 << ")"; }
auto operator<<(auto &o, auto a) -> decltype(all(a), o);
DTP(o << a.st << ", " << a.nd, a.nd);
DTP(for (auto i : a) o << i << ", ", all(a));
#define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", [](auto... arg_) { (( cerr << arg_ << ", " ), ...) << '\n'; }(x)
#else
#define deb(...) 0
#endif

namespace brute {
int solve(vi p, vector<vector<pii>> g) { // [where, mul]
    set<pii> seen;

    auto dfs = [&](auto f, int i, ll val) -> void {
        if (val > p[i]) return;
        if (!seen.emplace(i, (int)val).nd) return;

        for (auto [to, mul] : g[i])
            f(f, to, val * mul);
    };
    dfs(dfs, 0, 1);
    for (auto [i, val] : seen | views::reverse) if (i == sz(p) - 1)
        return val;
    return -1;
}
};

void maxi(int &i, int j) {
    if (j > i)
        i = j;
}

struct SCC : vi {
	vector<vi> comps; vi S;
	SCC(vector<vi>& G) : vi(sz(G),-1), S(sz(G))
	{ rep(i, sz(G)) if (!S[i]) dfs(G, i); }
	int dfs(vector<vi>& G, int v) {
		int low = S[v] = sz(S); S.pb(v);
		for (auto e : G[v]) if (at(e) < 0)
			low = min(low, S[e] ?: dfs(G, e));
		if (low == S[v]) {
			comps.pb({});
			fwd(i, S[v], sz(S)) {
				at(S[i]) = sz(comps)-1;
				comps.back().pb(S[i]);
			}
			S.resize(S[v]);
		}
		return low; } };

template <int MAXN>
int solve(vi p, vector<vector<pii>> gInput) {
    const int n = sz(p);

    if (n > MAXN) {
        return solve<min(2 * MAXN, 100)>(p, gInput);
    }

    vector<array<int, 4>> nonOneEdges;
    vector<vi> order, oneEdges(n);

    rep(i, n) for (auto [j, w] : gInput[i]) {
        if (w == 1) oneEdges[i].pb(j);
        else nonOneEdges.pb({i, j, w, 0});
    }

    vector<ll> bounds;
    for (int i : p) {
        for (int b = 1; b <= i; ++b)
            bounds.pb(b = i / (i / b));
    }
    sort(all(bounds));
    bounds.erase(unique(all(bounds)), bounds.end());
    // deb(sz(bounds));
    assert(bounds[0] == 1);

    vector<array<int, MAXN>> dp(sz(bounds));
    dp[0][0] = 1;

    rep(layerid, sz(bounds)) {
        // deb(layerid);
        if (!sz(order)) {
            vector<vi> g(n);
            rep(i, n) for (int j : oneEdges[i])
                g[j].pb(i);
            SCC scc(g);
            for (auto &comp : scc.comps) {
                if (sz(comp) == 1 && p[comp[0]] < bounds[layerid])
                    continue;
                order.pb(comp);
            }
            // deb(order);
        }
        for (auto &comp : order) {
            int mxhere = 0;
            for (int i : comp)
                maxi(mxhere, dp[layerid][i]);
            if (!mxhere) continue;
            for (int i : comp) {
                dp[layerid][i] = mxhere;
                for (int j : oneEdges[i])
                    maxi(dp[layerid][j], mxhere);
            }
        }
        bool delEdge = false;

        for (auto &[i, j, mul, nlayerid] : nonOneEdges) {
            if (!dp[layerid][i])
                continue;
            if (bounds[layerid] * mul > p[j]) {
                nlayerid = -1;
                delEdge = true;
                continue;
            }
            while (bounds[layerid] * mul > bounds[nlayerid])
                ++nlayerid;
            maxi(dp[nlayerid][j], dp[layerid][i] * mul);
        }

        if (delEdge) {
            erase_if(nonOneEdges, [](auto &ref) { return ref[3] == -1; });
        }
        rep(i, n) if (p[i] == bounds[layerid]) {
            order.clear();
            oneEdges[i].clear();
            for (auto &vec : oneEdges)
                erase(vec, i);
            erase_if(nonOneEdges, [&](auto &ref) { return ref[0] == i || ref[1] == i; });
        }
    }

    int ans = 0;
    for (auto &layer : dp)
        maxi(ans, layer[n - 1]);

    return ans ? ans : -1;
}

void solve() {
    int n, m;
    cin >> n >> m;

    vi p(n);
    vector<vector<pii>> g(n);

    rep(i, n) cin >> p[i];
    rep(_, m) {
        int a, b, w;
        cin >> a >> b >> w;
        g[a - 1].eb(b - 1, w);
    }
    int ans = solve<1>(p, g);
    #ifdef LOC
    // int br = brute::solve(p, g);
    // if (br != ans) {
    //     deb(ans, br);
    //     exit(-1);
    // }
    #endif
    cout << ans << '\n';
}

int32_t main() {
    cin.tie(0)->sync_with_stdio(0);
    cout << fixed << setprecision(10);

    int z = 1;
    cin >> z;
    rep(_, z) solve();

    cout << flush;
    _Exit(0);
}