// P1119 灾后重建
// Memory Limit: 125 MB
// Time Limit: 1000 ms
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e2 + 3;
int n , m , q;
int f[N][N][N];
int t[N];
int main(){
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++){
if(i != j) f[0][i][j] = 1e9 + 7;
}
}
for(int i = 1 ; i <= m ; i++){
int x , y , z;
cin >> x >> y >> z;
f[0][x][y] = f[0][y][x] = z;
}
//floyd
for(int k = 1 ; k < n ; k++){
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < n ; j++){
f[k][i][j] = min(f[k - 1][i][j] , f[k - 1][i][k] + f[k - 1][k][j]);
}
}
}
// 处理询问
cin >> q;
for(int i = 1 ; i <= q ; i++){
int x , y , z;
cin >> x >> y >> z;
if(t[x] > z || t[y] > z) cout << -1;
else{
int pos = upper_bound(t , t + n , z) - (t + 1);
if(f[pos][x][y] == 1e9 + 7) cout << -1;
else cout << f[pos][x][y];
}
cout << '\n';
}
return 0;
}