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
#include <bits/stdc++.h> 
using namespace std;
#define st first
#define nd second
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int maxn = 109;
ll p[maxn];
vector <pll> t[maxn];
map<ll,bool> czy;

ll f(ll wei , ll v)
{
    return wei*maxn+v;
}

void solve()
{
    ll  N , M , a , b , w  , A , H;
    cin >> N >> M;
    for(int i = 1 ; i <= N ; i++)cin >> p[i];
    for(int i = 0; i < M ; i++)
    {
        cin >> a >> b >> w;
        t[a].push_back({b,w});
    }

    queue <ll> Q;
    czy[f(1,1)] = 1;
    Q.push(f(1,1));

    while(!Q.empty())
    {
        A = Q.front();
        Q.pop();
        w = A/maxn;
        a = A%maxn;

        for(auto q : t[a])
        {
            if(p[q.st] < w*q.nd)continue;
            H = f(w*q.nd , q.st);
            if(czy[H])continue;

            czy[H] = 1;
            Q.push(H);
        }
    }

    ll res = -1;
    for(auto m : czy)
    {
        if(m.st%maxn != N || m.nd == 0)
        {
            continue;
        }

        res = max(res , m.st/maxn);
    }

    cout << res << '\n';


    czy.clear();
    for(int i =1 ; i <= N ; i++)
    {
        t[i].clear();
    }
}

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