#include<iostream>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int N = 2010,M = 40010;
int n,m;
int arr[N],w[N][N];
ll dist[N][N];
int main()
{
cin>>n>>m;
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= n; j ++)
dist[i][j] = 1e9;
for(int i = 1; i <= n; i ++)
{
cin>>arr[i];
dist[i][i] = 0;
}
int a,b,c;
while(m --)
{
cin>>a>>b>>c;
w[a+1][b+1] = c;
w[b+1][a+1] = c;
}
cin>>m;
int now = 1;
while(m --)
{
int x,y,t;
cin>>x>>y>>t;
x ++,y ++;
while(now <= n&&t >= arr[now])
{
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= n; j ++)
if(dist[i][j] > dist[i][now]+dist[j][now])
dist[i][j] = dist[j][i] = dist[i][now]+dist[j][now];
now ++;
}
if(arr[x]>t||arr[y]>t) cout<<"-1"<<endl;
if(dist[x][y] == 1e9) cout<<"-1"<<endl;
else cout<<dist[x][y]<<endl;
}
return 0;
}