N,M=map(int, input().split()) A=[] for i in range(M): a,b=map(int, input().split()) a-=1;b-=1 A.append((a,b)) C=list(map(int, input().split())) W=list(map(int, input().split())) par=[i for i in range(N)] rank=[0]*(N) def find(x): if par[x]==x: return x else: par[x]=find(par[x]) return par[x] #同じ集合か判定 def same(x,y): return find(x)==find(y) def union(x,y): x=find(x) y=find(y) if x==y: return if rank[x]>rank[y]: par[y]=x else: par[x]=y if rank[x]==rank[y]: rank[y]+=1 DD={} par=[i for i in range(N)];rank=[0]*(N) for j in range(2**10): D=[];s=0;now=j for k in range(10): D.append(j%2) s+=D[-1]*W[k] j//=2 for x,y in A: if D[C[x]-1]==1 and D[C[y]-1]==1: union(x,y) E=[] for i in range(N): E.append(find(i)) par[i]=i;rank[i]=0 DD[now]=(E,s) Q=int(input()) for _ in range(Q): ans=10**20 a,b=map(int, input().split()) for d in DD: P,s=DD[d] if P[a-1]==P[b-1]: ans=min(ans,s) if ans==10**20: print(-1) else: print(ans)