#include using namespace std; struct UnionFind { vector data; UnionFind(int N) : data(N, -1) {} int find(int k) { return data[k] < 0 ? k : data[k] = find(data[k]); } int unite(int x, int y) { if ((x = find(x)) == (y = find(y))) return false; if (data[x] > data[y]) swap(x, y); data[x] += data[y]; data[y] = x; return true; } // f ... merge function template int unite(int x, int y,const F &f) { if ((x = find(x)) == (y = find(y))) return false; if (data[x] > data[y]) swap(x, y); data[x] += data[y]; data[y] = x; f(x, y); return true; } int size(int k) { return -data[find(k)]; } int same(int x, int y) { return find(x) == find(y); } }; int main() { int N, M; cin >> N >> M; vector A(M, 0), B(M, 0); for(int i = 0; i < M; i++) { cin >> A[i] >> B[i]; A[i]--, B[i]--; } vector C(N, 0); for(int i = 0; i < N; i++) { cin >> C[i]; C[i]--; } vector W(10, 0LL); for(int i = 0; i < 10; i++) cin >> W[i]; vector uf(1024, UnionFind(N)); for(int i = 0; i < M; i++) { for(int bit = 0; bit < 1024; bit++) { if((bit >> C[A[i]] & 1) && (bit >> C[B[i]] & 1)) { uf[bit].unite(A[i], B[i]); } } } vector cost(1024, 0LL); for(int bit = 0; bit < 1024; bit++) { for(int i = 0; i < 10; i++) { if(bit >> i & 1) { cost[bit] += W[i]; } } } int Q; cin >> Q; while(Q--) { int u, v; cin >> u >> v; u--, v--; long long ans(20000000000LL); for(int bit = 0; bit < 1024; bit++) { if(uf[bit].same(u, v)) ans = min(cost[bit], ans); } if(ans == 20000000000LL) { ans = -1LL; } cout << ans << endl; } return 0; }