import sys, time, random from collections import deque, Counter, defaultdict input = lambda: sys.stdin.readline().rstrip() ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) inf = 2 ** 61 - 1 mod = 998244353 class dsu(): n=1 parent_or_size=[-1 for i in range(n)] def __init__(self,N): self.n=N self.num = 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 def count(self): return self.num n, m = mi() EDGE = [li() for _ in range(m)] c = [v - 1 for v in li()] w = li() q = ii() Qu = [li() for _ in range(q)] ans = [inf] * q for bit in range(1 << (10)): U = dsu(n) cost = 0 for i in range(10): if 1 & (bit >> i): cost += w[i] for u, v in EDGE: u -= 1; v -= 1 if 1 & (bit >> c[u]) and 1 & (bit >> c[v]): U.merge(u, v) for i in range(q): x, y = Qu[i] x -= 1; y -= 1 if U.same(x, y): ans[i] = min(ans[i], cost) for i in range(q): if ans[i] == inf: ans[i] = -1 print(ans[i])