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

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef double db;

template<typename T>
void updMin(T& a, T b)
{
	a = min(a, b);
}

template<typename T>
void updMax(T& a, T b)
{
	a = max(a, b);
}

void solve()
{
	int n, m;
	cin >> n >> m;
	VI p(n);
	for (int& pi : p)
		cin >> pi;
	vector<tuple<int, int, int>> edges(m);
	vector<VI> g1(n);
	for (auto& [u, v, w] : edges)
	{
		cin >> u >> v >> w;
		u--;
		v--;
		if (w == 1)
			g1[u].PB(v);
	}
	VI vec;
	for (int pi : p)
	{
		for (int d = 1; d <= pi; d = pi / (pi / d) + 1)
		{
			vec.PB(d);
		}
	}
	sort(ALL(vec));
	vec.erase(unique(ALL(vec)), vec.end());
	vector nxt(SZ(vec), VI(m));
	FOR(iEdge, 0, m)
	{
		int w = get<2>(edges[iEdge]), ptr = 0;
		FOR(j, 0, SZ(vec))
		{
			while (ptr + 1 < SZ(vec) && (LL)vec[j] * w >= vec[ptr + 1])
				ptr++;
			nxt[j][iEdge] = ptr;
		}
	}
	vector dp(SZ(vec), VI(n, -1));
	dp[0][0] = 1;
	VI used(n);
	VI curVec(n);
	iota(ALL(curVec), 0);
	queue<int> q;
	FOR(i, 0, SZ(vec))
	{
		used.assign(n, 0);
		sort(ALL(curVec), [&](int u, int v)
		{
			return dp[i][u] > dp[i][v];
		});
		for (int s : curVec)
		{
			if (dp[i][s] == -1)
				break;
			if (used[s])
				continue;
			q.push(s);
			used[s] = 1;
			while (!q.empty())
			{
				int u = q.front();
				q.pop();
				for (int v : g1[u])
				{
					if (!used[v])
					{
						dp[i][v] = dp[i][s];
						q.push(v);
						used[v] = 1;
					}
				}
			}
		}
		FOR(iEdge, 0, m)
		{
			auto [u, v, w] = edges[iEdge];
			if (dp[i][u] != -1 && w > 1 && (LL)vec[i] * w <= p[v])
			{
				updMax(dp[nxt[i][iEdge]][v], dp[i][u] * w);
			}
		}
	}
	int ans = -1;
	FOR(i, 0, SZ(vec))
	{
		updMax(ans, dp[i][n - 1]);
	}
	cout << ans << "\n";
}

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