/*
n个村,m条双向,ti村庄修好可通车
问第t天x,到y的最短路
*/
#include<bits/stdc++.h>
using namespace std;
const int N=205;
int n,m;
int t[N],dist[N][N];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
memset(dist,127,sizeof dist);
cin>>n>>m;
for(int i=0;i<n;i++)
{
cin>>t[i];
dist[i][i]=0;
}
int u,v,w,q;
for(int i=1;i<=m;i++)
{
cin>>u>>v>>w;
dist[u][v]=w;
dist[v][u]=w;
}
cin>>q;
int cun=0,x,y,shi;//cun是下一个是哪个村子当中转
while(q--)
{
cin>>x>>y>>shi;
if(t[x]>shi||t[y]>shi)
{
cout<<-1<<endl;
continue;
}
while(t[cun]<=shi)//村子能走的时候 即可以作为中转站
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
dist[i][j]=min(1ll*dist[i][j],1ll*dist[i][cun]+1ll*dist[cun][j]);
}
}
cun++;
}
if(dist[x][y]>=1000000000)cout<<-1<<endl;
else cout<<dist[x][y]<<endl;
}
return 0;
}