#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; UnionFind Z; Z.make(N); vector> Graph(N); for(int i=0; i> u >> v; u--; v--; Z.unite(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; int Q; cin >> Q; for(int i=0; i> u >> v; u--; v--; if(Z.issame(u,v) == false){cout << -1 << endl; return 0;} long long now = 1e18; vector visited(N); auto dfs = [&](auto dfs,int pos,int W2,long long cost) -> void { if(pos == v){now = min(cost,now); return;} if(cost >= now) return; visited.at(pos) = true; for(auto to : Graph.at(pos)){ if(visited.at(to)) continue; if(W2&(1<