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 rep(i,a,n) for (int i=(a);i<(n);i++)
#define per(i,a,n) for (int i=(n)-1;i>=(a);i--)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef basic_string<int> BI;
typedef long long ll;
typedef pair<int,int> PII;
typedef long double db;
mt19937 mrand(1111); 
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int N=110;
const int M1=100000,M2=10000;
int n,m,p[N];
vector<PII> e[N],re[N];
VI e1[N],re1[N];
int dp[M1+10][N],pd[M2+10][N];
int pre[N][M1+10];

int val[1010],op[1010];
array<int,3> E[1010];
int __;
void solve() {
	scanf("%d%d",&n,&m);
	rep(i,1,n+1) {
		scanf("%d",&p[i]);
		e[i].clear();
		re[i].clear();
		e1[i].clear();
		re1[i].clear();
	}
	rep(i,0,m) {
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		E[i]={u,v,w};
		if (w>=2) {
			e[u].pb(mp(v,w));
			re[v].pb(mp(u,w));
		} else {
			e1[u].pb(v);
			re1[v].pb(u);
		}
	}
	rep(i,1,n+1) rep(j,1,M1+1) dp[j][i]=0;
	dp[1][1]=1;
	queue<int> q;
	rep(i,1,M1+1) {
		rep(u,1,n+1) if (dp[i][u]) q.push(u);
		while (!q.empty()) {
			int u=q.front(); q.pop();
			for (auto v:e1[u]) if (!dp[i][v]&&i<=p[v]) {
				dp[i][v]=1;
				q.push(v);
			}
		}
		rep(u,1,n+1) if (dp[i][u]) {
			for (auto [v,w]:e[u]) if ((ll)i*w<=M1&&i*w<=p[v]) dp[i*w][v]=1;
		}
	}
	rep(i,1,n+1) {
		int pz=0;
		rep(j,1,M1+1) {
			if (dp[j][i]) pz=j;
			pre[i][j]=pz;
		}
	}
	rep(i,1,n+1) rep(j,1,M2+1) pd[j][i]=0;
	pd[1][n]=p[n];
	int M=1;
	while (M<n) M*=2;
	rep(i,1,M2+1) {
		rep(x,1,M+1) {
			if (x<=n) val[x]=pd[i][x];
			else val[x]=-1;
			op[x+M-1]=x;
		}
		auto upd=[&](int x) {
			if (val[op[x*2]]>val[op[x*2+1]]) op[x]=op[x*2];
			else op[x]=op[x*2+1];
		};
		auto change=[&](int x,int y) {
			val[x]=y;
			x=(x+M-1)>>1;
			while (x) upd(x),x/=2;
		};
		per(x,1,M) upd(x);
		while (1) {
			int x=op[1];
			if (val[x]==-1) break;
			pd[i][x]=val[x];
			change(x,-1);
			for (auto v:re1[x]) {
				int w=min(pd[i][x],p[v]);
				if (val[v]!=-1&&w>val[v]) change(v,w);
			}
		}
		rep(u,1,n+1) if (pd[i][u]) {
			for (auto [v,w]:re[u]) if ((ll)i*w<=M2)
				pd[i*w][v]=max(pd[i*w][v],min(pd[i][u]/w,p[v]));
		}
	}
	int ans=0;
	rep(i,0,m) {
		auto [u,v,w]=E[i];
		rep(j,1,M2+1) {
			int z=pd[j][v]/w;
			ans=max(ans,pre[u][min(z,M1)]*w*j);
		}
	}
	if (n==1) ans=max(ans,1);
	if (ans==0) ans=-1;	
	printf("%d\n",ans);
}

int _;
int main() {
	for (scanf("%d",&_);_;_--) {
		solve();
	}
}