class dsu(): n=1 parent_or_size=[-1 for i in range(n)] def __init__(self,N): self.n=N self.parent_or_size=[-1 for i in range(N)] def merge(self,a,b): assert 0<=a0: result2.append(result[i]) return result2 N,M=map(int,input().split()) E=[] for i in range(M): E.append(list(map(lambda x:int(x)-1,input().split()))) C=list(map(lambda x:int(x)-1,input().split())) W=list(map(int,input().split())) cost=[0]*(1<<10) for i in range(1<<10): for j in range(10): if i>>j&1: cost[i]+=W[j] Q=int(input()) ans=[10**12]*Q Qs=[] for i in range(Q): U,V=map(int,input().split()) U-=1 V-=1 Qs.append([U,V]) for i in range(1<<10): u=dsu(N) for j in E: if (i>>C[j[0]])&(i>>C[j[1]])&1: u.merge(j[0],j[1]) for j in range(Q): if u.same(Qs[j][0],Qs[j][1]): ans[j]=min(ans[j],cost[i]) for i in ans: if(i==10**12): i=-1 print(i)