#include<bits/stdc++.h>
using namespace std;
int n,m;
int g[210][210];
int te[210];
int floyd(int x,int y,int t)
{
int g1[210][210];
memset(g1,0x3f,sizeof g1);
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
if(i==j)
g1[i][j]=g1[j][i]=0;
if(te[i]<=t&&te[j]<=t)
{
g1[i][j]=g1[i][j]=g1[j][i]=g1[j][i];
}
}
}
for(int k=1; k<=n; k++)\
{
for(int j=1; j<=n; j++)
{
for(int i=1; i<=n; i++)
g1[i][j]=min(g1[i][j],g1[j][k]+g1[k][i]);
}
}
return g1[x][y];
}
int main()
{
cin>>n>>m;
for(int i=1; i<=n; i++)
{
int x;
cin>>x;
te[i]=x;
}
while(m--)
{
int i,j,w;
cin>>i>>j>>w;
g[i][j]=g[j][i]=w;
}
int q;
cin>>q;
while(q--)
{
int x,y,t;
cin>>x>>y>>t;
if(floyd(x,y,t)>0x3f3f3f3f/2)
cout<<"-1"<<endl;
else
cout<<floyd(x,y,t)<<endl;
}
}