class UnionFind: def __init__(self, n): self.n = n self.par = [-1] * n self.group_ = n def find(self, x): if self.par[x] < 0: return x lst = [] while self.par[x] >= 0: lst.append(x) x = self.par[x] for y in lst: self.par[y] = x return x def unite(self, x, y): x = self.find(x) y = self.find(y) if x == y: return False if self.par[x] > self.par[y]: x, y = y, x self.par[x] += self.par[y] self.par[y] = x self.group_ -= 1 return True def size(self, x): return -self.par[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) @property def group(self): return self.group_ n, m = map(int, input().split()) ab = [list(map(int, input().split())) for _ in range(m)] C = list(map(int, input().split())) W = list(map(int, input().split())) t = 10 UF = [UnionFind(n) for _ in range(1 << t)] cost = [0] * (1 << t) for bit in range(1 << t): for i in range(t): if bit >> i & 1: cost[bit] += W[i] for a, b in ab: a -= 1 b -= 1 bit = (1 << C[a] - 1) | (1 << C[b] - 1) for S in range(1 << t): if S & bit == bit: UF[S].unite(a, b) Q = int(input()) for _ in range(Q): x, y = map(int, input().split()) x -= 1 y -= 1 ans = cost[-1] + 1 for S in range(1 << t): if UF[S].same(x, y): ans = min(ans, cost[S]) if ans == cost[-1] + 1: ans = -1 print(ans)