from sys import stdin from collections import deque N,M=map(int,stdin.readline().split()) G=[[] for _ in range(N)] for _ in range(M): A,B=map(int,stdin.readline().split()) A-=1 B-=1 G[A].append(B) G[B].append(A) C=list(map(lambda x:int(x)-1,stdin.readline().split())) W=list(map(int,stdin.readline().split())) root=[[-1]*N for _ in range(1<<10)] for i in range(1<<10): for j in range(N): if root[i][j]!=-1: continue root[i][j]=j if (i>>C[j]&1)==0: continue q=deque() q.append(j) while q: v=q.popleft() for k in G[v]: if root[i][k]==-1 and (i>>C[k]&1): root[i][k]=j q.append(k) 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(stdin.readline()) for _ in range(Q): U,V=map(int,stdin.readline().split()) U-=1 V-=1 ans=10**11 for i in range(1<<10): if root[i][U]==root[i][V]: ans=min(ans,cost[i]) if ans==10**11: ans=-1 print(ans)