#include #include using namespace std; using ll = long long; constexpr ll INF = 1000000000000000000LL; class UnionFind { private: int N; vector par, size; public: UnionFind (int N_) { reset(N_); } void reset (int N_) { N = N_; par.resize(N); size.resize(N); for (int i = 0; i < N; i++) { par[i] = i; size[i] = 1; } } int root (int u) { if (par[u] == u) return u; return par[u] = root(par[u]); } bool same (int u, int v) { return root(u) == root(v); } bool unite (int u, int v) { u = root(u); v = root(v); if (u == v) return false; int l = u, s = v; if (size[u] < size[v]) swap(l, s); par[s] = par[l]; size[l] += size[s]; return true; } }; int main () { int N, M; cin >> N >> M; vector> edges(M); for (int i = 0; i < M; i++) { int A, B; cin >> A >> B; A--, B--; edges[i] = make_pair(A, B); } vector C(N); for (int i = 0; i < N; i++) cin >> C[i]; for (int i = 0; i < N; i++) C[i]--; vector W(10); for (int i = 0; i < 10; i++) cin >> W[i]; int Q; cin >> Q; vector ans(Q); vector> query(Q); for (int i = 0; i < Q; i++) { int u, v; cin >> u >> v; u--, v--; query[i] = make_pair(u, v); ans[i] = INF; } // UF全通り UnionFind UF(N); for (int bit = 0; bit < (1<<10); bit++) { UF.reset(N); ll cost = 0; for (int i = 0; i < 10; i++) if ( 0 < (bit & (1<