usingnamespace std; constint N = 205, M = 400005; int n, m, q; int t[N], f[N][N], x, y, z;
voidupdate(int k){ for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (f[i][j] > f[i][k] + f[j][k]) { f[i][j] = f[j][i] = f[i][k] + f[j][k]; } } } }
intmain(){ cin >> n >> m; for (int i = 0; i < n; i++) { cin >> t[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { f[i][j] = 0x3f3f3f3f; } } for (int i = 0; i < n; i++) { f[i][i] = 0; } for (int i = 0; i < m; i++) { cin >> x >> y >> z; f[x][y] = z; f[y][x] = z; } cin >> q; int cur = 0; while (q--) { cin >> x >> y >> z; while (t[cur] <= z && cur < n) { update(cur); cur++; } if (t[x] > z || t[y] > z) cout << -1 << endl; else { if (f[x][y] == 0x3f3f3f3f) cout << -1 << endl; else cout << f[x][y] << endl; }