import sys input = sys.stdin.readline class UnionFind: def __init__(self, n:int): self.n = n self.parents = [-1] * n def find(self, x:int): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x:int, y:int): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def size(self, x:int): return -self.parents[self.find(x)] def same(self, x:int, y:int): return self.find(x) == self.find(y) N,M=map(int,input().split()) AB=[list(map(lambda x:int(x)-1,input().split())) for _ in range(M)] C=list(map(lambda x:int(x)-1,input().split())) W=list(map(int,input().split())) bit_uf = [] for bit in range(1<<10): uf = UnionFind(N) for a,b in AB: if ((bit>>C[a])&1) and ((bit>>C[b])&1): uf.union(a,b) cost = 0 for i in range(10): if (bit>>i)&1: cost += W[i] bit_uf.append((uf,cost)) Q=int(input()) for _ in range(Q): u,v=map(lambda x:int(x)-1,input().split()) ans = 1<<60 for uf,c in bit_uf: if uf.same(u,v): ans = min(ans, c) print(ans if ans < (1<<60) else -1)