I,R=input,range J=lambda:map(int,I().split()) N,M=J() E=[list(J())for j in R(M)] E=sorted([w,u,v]for u,v,w in E) T=int(I()) Q=list(J()) Q=sorted([Q[t],t]for t in R(T)) A=[0]*T e=0 c=N P=list(R(N)) H=[0]*N def r(i): m=P[i] while i!=m:P[i]=i=P[m];m=P[i] return i for[q,t]in Q: while eq:break else: u,v,e=r(u),r(v),e+1 if H[v]