#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
const int N = 210;
inline int du(void) {
int x = 0, f = 1;
char ch = getchar();
while (ch > '9' || ch < '0') { if (ch == '-')f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
inline void write(int x) {
if (x < 0) { putchar('-'); x = -x; }
if (x > 9)write(x / 10);
putchar(x % 10 ^ 48);
return;
}
int n, m, q;
//int x[N], y[N], z[N],
int t[N];
int f[N][N][N];
int x, y, z;
int main(void) {
n = du(), m = du();
for (int i = 1; i <= n; ++i) {
t[i] = du();
}
memset(f, 0x3f, sizeof(f));
for (int i = 1; i <= n; ++i)
f[0][i][i] = 0;
for (int i = 1; i <= m; ++i) {
x = du(), y = du(), z = du();
++x, ++y;
f[0][x][y] = z;
f[0][y][x] = z;
}
for (int k = 1; k <= n; ++k)
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
f[k][i][j] = min(f[k - 1][i][j], f[k - 1][i][k] + f[k - 1][k][j]);
q = du();
int j = 0;
while (q--) {
x = du(), y = du(), z = du();
x++, y++;
while (t[j] <= z && j <= n)
++j;
while (t[j] > z) --j;
//int j = lower_bound(t + 1, t + 1 + n, z) - t;
if (t[x] > z || t[y] > z || f[j][x][y] == 1061109567)
cout << "-1" << endl;
else cout << f[j][x][y] << endl;
}
return 0;
}