#include <bits/stdc++.h>
using namespace std;
const int maxn=305;
const int INF=0x3f3f3f3f;
int n,m;
int t[maxn],dis[maxn][maxn];
void repair(int k)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
}
}
int main()
{
for(int i=0;i<maxn;i++)
{
for(int j=0;j<maxn;j++)
{
dis[i][j]=INF;
}
dis[i][i]=0;
}
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&t[i]);
}
for(int i=0;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
dis[u+1][v+1]=dis[v+1][u+1]=w;
}
int q,time=1;
scanf("%d",&q);
for(int i=0;i<q;i++)
{
int x,y,ti;
scanf("%d%d%d",&x,&y,&ti);
x++;y++;
if(t[time]<=ti&&time<=n)
{
repair(time);
time++;
}
if(t[x]>ti||t[y]>ti) cout<<"-1"<<endl;
else
{
if(dis[x][y]==INF) cout<<"-1"<<endl;
else cout<<dis[x][y]<<endl;
}
}
return 0;
}