## 【Codevs 1817】灾后重建

#include<iostream>
#include<cstdio>
#include<vector>
#define INF 1000099
using namespace std;

struct abc{int point,len;};
int n,m;
int t[299]={0};
int f[299][299];
bool yet[299]={0};
vector<abc> path[299];
bool day[100099]={0};
int pro=0;

void init(){
for (int i=1;i<=250;i++){
t[i]=INF;
}
for (int i=0;i<=250;i++){
for (int j=0;j<=250;j++){
f[i][j]=INF;
}
}
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++){
scanf("%d",&t[i]);
if (t[i]==0) yet[i]=true;
}
abc hc;
for (int i=1,a,b,leng;i<=m;i++){
scanf("%d%d%d",&a,&b,&leng);
a++;b++;
f[a][b]=leng;f[b][a]=leng;
hc.point=b;hc.len=leng;
path[a].push_back(hc);
hc.point=a;
path[b].push_back(hc);
}
}

void update(int num){
int end,length;
for (int k=pro+1;k<=end;k++)
yet[k]=true;
for (int k=pro+1;k<=end;k++){
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
}
}
}
pro=end;
}

int main(){
int Q,x,y,ti;
init();
cin>>Q;
for (int i=1;i<=Q;i++){
scanf("%d%d%d",&x,&y,&ti);
x++;y++;
if (!day[ti]) {
update(ti);
day[ti]=1;
}
if (f[x][y]<INF && yet[x] && yet[y]) printf("%d\n",f[x][y]);
else printf("-1\n");
}
return 0;
}