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

using namespace std;

int n, m;
int capacities[101];
set<pair<int, int>> graph[101];
unordered_set<long long> visited[101];
vector<int> multipliers[101][101];
vector<int> children[101];
set<int> childrenS[101];

void dfs(int node, long long power)
{
    visited[node].insert(power);
    
    for (int child : children[node])
    {
        for (auto m : multipliers[node][child])
        {
            if (power * m <= capacities[child])
            {
                if (visited[child].find(power * m) == visited[child].end())
                    dfs(child, power * m);
            }
            else
            {
                break;
            }
        }
    }
}

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

    for (int i = 1; i <= n; ++i) cin >> capacities[i];

    for (int i = 0; i < m; ++i)
    {
        int a, b, w;
        cin >> a >> b >> w;
        graph[a].emplace(w, b);
        childrenS[a].insert(b);
    }

    for (int i = 1; i <= n; ++i)
    {
        for (auto child : graph[i])
        {
            multipliers[i][child.second].push_back(child.first);
        }
        for (int c : childrenS[i]) children[i].push_back(c);
    }

    dfs(1, 1);


    long long best = 0;
    for (long long v : visited[n]) best = max(best, v);
    if (visited[n].size() == 0)
        cout << "-1\n";
    else
        cout << best << '\n';

    for (int i = 1; i <= n; ++i)
    {
        capacities[i] = 0;
        graph[i].clear();
        visited[i].clear();
        children[i].clear();
        childrenS[i].clear();

        for (int j = 1; j <= n; ++j) multipliers[i][j].clear();
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int t;
    cin >> t;

    for (int i = 0; i < t; ++i)
        solve();
}