#include using namespace std; using ll = long long; struct UnionFind { int ngroup, N; vector par, siz; UnionFind(int _N) : N(_N), par(_N), siz(_N){ ngroup = _N; for(int i = 0; i < N; i++) par[i] = i, siz[i] = 1; } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } int unite(int x, int y) { int rx = root(x), ry = root(y); if (rx == ry) return rx; ngroup--; if (siz[rx] > siz[ry]) swap(rx, ry); par[rx] = ry; siz[ry] += siz[rx]; return ry; } bool same(int x, int y) { int rx = root(x), ry = root(y); return rx == ry; } int size(int x) {return siz[root(x)];} int group_count() {return ngroup;} vector> groups(){ vector rev(N); int nrt=0; for (int i=0; i> res(nrt); for (int i=0; i> N >> M; vector A(M+1), B(M+1), C(N+1), W(10); for (int i=1; i<=M; i++) cin >> A[i] >> B[i]; for (int i=1; i<=N; i++){ cin >> C[i]; C[i]--; } for (int i=0; i<10; i++) cin >> W[i]; cin >> Q; vector u(Q+1), v(Q+1), ans(Q+1, 1e18); for (int i=1; i<=Q; i++) cin >> u[i] >> v[i]; for (int i=0; i>j&1) sm += W[j]; } UnionFind uf(N+1); for (int j=1; j<=M; j++){ if (i>>C[A[j]]&1 && i>>C[B[j]]&1) uf.unite(A[j], B[j]); } for (int j=1; j<=Q; j++){ if (uf.same(u[j], v[j])) ans[j] = min(ans[j], sm); } } for (int i=1; i<=Q; i++){ if (ans[i] == 1e18) cout << -1 << endl; else cout << ans[i] << endl; } return 0; }