#include <cstdio> #include <iostream> #include <algorithm> #include <vector> using namespace std; const int N=210; const int M=N*N/2; const int INF=1<<20; int n,m,t[N],d[N][N],q,cnt; bool vis[N]; int main(){ scanf("%d%d",&n,&m); for(int i=0;i<n;i++) for(int j=0;j<n;j++)d[i][j]=INF; for(int i=0;i<n;i++)scanf("%d",&t[i]); while(m--){ int u,v,w; scanf("%d%d%d",&u,&v,&w); d[u][v]=d[v][u]=w; } scanf("%d",&q); while(q--){ int beg,end,nt; scanf("%d%d%d",&beg,&end,&nt); while(t[cnt]<=nt && cnt<n){ for(int i=0;i<n;i++) for(int j=0;j<n;j++)d[i][j]=min(d[i][j],d[i][cnt]+d[cnt][j]); cnt++; } if(t[beg]>nt || t[end]>nt || d[beg][end]==INF)puts("-1"); else printf("%d ",d[beg][end]); } return 0; }