#include using namespace std; int par[100010], siz[100010]; void init(int N) { iota(par, par + N, 0); fill(siz, siz + N, 1); } int root(int v) { return par[v] = (v == par[v] ? v : root(par[v])); } void merge(int a, int b) { a = root(a); b = root(b); if (a == b) return; if (siz[a] < siz[b]) swap(a, b); par[b] = a; siz[a] += siz[b]; } int main () { int N, M; cin >> N >> M; using P = pair; std::vector

E(M); for (auto& [a, b] : E) { cin >> a >> b; a --; b --; } vector C(N); for (auto& a : C) { cin >> a; a --; } using ll = long long; ll W[10]; for (auto& a : W) cin >> a; int Q; cin >> Q; vector ans(Q, (ll)1e18); vector

que(Q); for (auto& [a, b] : que) { cin >> a >> b; a --; b --; } for (int s = 1; s < (1 << 10); s ++) { ll w = 0; for (int i = 0; i < 10; i ++) { w += W[i] * ((s >> i) & 1); } init(N); for (auto [a, b] : E) { if ((s >> a) & (s >> b) & 1) { merge(a, b); } } for (int i = 0; i < Q; i ++) { auto [a, b] = que[i]; if (root(a) == root(b)) { ans[i] = min(ans[i], w); } } } for (auto a : ans) { cout<< a << endl; } }