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
/*
 * Opis: Główny nagłówek
 */
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r)for(int i=(l);i<=(r);++i)
#define REP(i,n)FOR(i,0,(n)-1)
#define ssize(x)int(x.size())
#ifdef DEBUG
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif

constexpr int W = 32000;

void solve() {
	int n, m;
	cin >> n >> m;
	vector<int> arr(n);
	for (int& v : arr) {
		cin >> v;
	}
	vector<vector<pair<int, int>>> kraw(n), wstecz(n);
	REP(i, m) {
		int a, b, w;
		cin >> a >> b >> w;
		a--, b--;
		kraw[a].push_back({b, w});
		wstecz[b].push_back({a, w});
	}

	vector<bitset<W>> vis(n);

	function<void(int, int)> dfs;
	dfs = [&] (int x, int val) {
		vis[x].set(val);
		for (auto [v, w] : kraw[x]) {
			if (LL(W) <= LL(w) * val) {
				continue;
			}
			int new_val = w * val;
			if (new_val > arr[v]) {
				continue;
			}
			if (vis[v].test(new_val)) {
				continue;
			}
			dfs(v, new_val);
		}
	};
	dfs(0, 1);
//	cout << "siema " << endl;
	vector<array<int, W>> first_left(n);
	REP(i, n) {
		first_left[i][0] = 0;
		REP(j, W-1) {
			if (vis[i].test(j+1)) {
				first_left[i][j+1] = j+1;
			}
			else {
				first_left[i][j+1] = first_left[i][j];
			}
		}
	}
	int res = 0;
	if (n == 1) {
		res = 1;
	}

	vector<array<int, W>> dist(n);
	REP(i, n) {
		fill(dist[i].begin(), dist[i].end(), 0);
	}
	priority_queue<array<int,3>> order;
	order.push({arr[n-1], n-1, 1});
	while (!order.empty()) {
		auto [d, x, val] = order.top();
		order.pop();
		debug(d, x, val);
		if (d <= dist[x][val]) {
			continue;
		}
		dist[x][val] = d;
		for (auto [v, w] : wstecz[x]) {
			if (w > d) {
				continue;
			}
			int rest = min(d / w, arr[v]);
			res = max(res, val * first_left[v][min(rest, W-1)] * w);
			if (LL(W) <= LL(w) * val) {
				continue;
			}
			int new_val = w * val;
			if (dist[v][new_val] >= rest) {
				continue;
			}
			order.push({rest, v, new_val});
		}
	}
	if (res == 0) {
		res = -1;
	}
	cout << res << "\n";
}

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