#include using namespace std; class UnionFind{ public: vector par,siz; void make(int N){ par.resize(N,-1); siz.resize(N,1); } int root(int x){ if(par.at(x) == -1) return x; else return par.at(x) = root(par.at(x)); } void unite(int u, int v){ u = root(u),v = root(v); if(u == v) return; if(siz.at(u) < siz.at(v)) swap(u,v); par.at(v) = u; siz.at(u) += siz.at(v); } bool issame(int u, int v){ if(root(u) == root(v)) return true; else return false; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int N,M; cin >> N >> M; vector> Graph(N); for(int i=0; i> u >> v; u--; v--; Graph.at(u).push_back(v); Graph.at(v).push_back(u); } vector C(N); vector W(10); for(auto &c : C) cin >> c,c--; for(auto &w : W) cin >> w; vector cost(1024); vector UF(1024); for(int i=0; i<1024; i++){ UnionFind &uf = UF.at(i); uf.make(N); for(int k=0; k> Q; for(int i=0; i> u >> v; u--; v--; long long now = 1e18; for(int k=0; k<1024; k++) if(UF.at(k).issame(u,v)) now = min(now,cost.at(k)); if(now == 1e18) now = -1; cout << now << endl; } }