#include #include using namespace std; struct DSU { vector grp; DSU(int n = 0) : grp(n) { for (int i = 0; i < n; ++i) grp.at(i) = i; } int find(int u) { return grp.at(u) == u ? u : grp.at(u) = find(grp.at(u)); } void merge(int u, int v) { u = find(u); v = find(v); if (u != v) grp.at(v) = u; } }; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int N, M; cin >> N >> M; vector> edges(M); for (auto &[a, b] : edges) cin >> a >> b, --a, --b; vector C(N); for (auto &c : C) cin >> c, --c; constexpr int D = 10; vector W(D); for (auto &w : W) cin >> w; vector Wsum(1 << D); for (int S = 0; S < (1 << D); ++S) { for (int i = 0; i < D; ++i) { if (S & (1 << i)) Wsum.at(S) += W.at(i); } } vector dsus(1 << D); for (int S = 0; S < (1 << D); ++S) { dsus.at(S) = DSU(N); for (auto [a, b] : edges) { if ((S & 1 << C.at(a)) and (S & 1 << C.at(b))) dsus.at(S).merge(a, b); } } int Q; cin >> Q; constexpr long long inf = 1e18; while (Q--) { int u, v; cin >> u >> v; --u, --v; long long best = inf; for (int S = 0; S < (1 << D); ++S) { if (dsus.at(S).find(u) == dsus.at(S).find(v)) { best = min(best, Wsum.at(S)); } } if (best == inf) best = -1; cout << best << '\n'; } }