#include #include #include using namespace std; using ll=long long; #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() template bool chmax(T &a, T b){if (a < b){a = b;return true;} else return false;} template bool chmin(T &a, T b){if (a > b){a = b;return true;} else return false;} struct UnionFind { private: vector par, siz; public: UnionFind(){} UnionFind(int n) : par(n, -1), siz(n, 1) {} int find(int x) { if (par[x] == -1) return x; else { par[x] = find(par[x]); return par[x]; } } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return siz[find(x)]; } bool merge(int x, int y) { x = find(x); y = find(y); if (x == y) return false; if (siz[x] < siz[y]) swap(x, y); siz[x] += siz[y]; par[y] = x; return true; } }; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N,M; cin>>N>>M; vectorA(M),B(M); for(int i=0;i>A[i]>>B[i]; A[i]--; B[i]--; } vectorC(N); for(int i=0;i>C[i]; C[i]--; } vectorW(10); for(int i=0;i<10;i++)cin>>W[i]; int Q; cin>>Q; vectorufs(1<<10); for(int i=0;i<1<<10;i++){ UnionFind uf(N); ufs[i]=uf; } for(int bit=0;bit<1<<10;bit++){ for(int i=0;i>C[A[i]]&1)&&(bit>>C[B[i]]&1)){ ufs[bit].merge(A[i],B[i]); } } } vectorBs(1<<10); for(int bit=0;bit<1<<10;bit++){ for(int i=0;i<10;i++){ if(bit>>i&1)Bs[bit]+=W[i]; } } while(Q--){ int u,v; cin>>u>>v; u--; v--; ll ans=1LL<<60; for(int bit=0;bit<1<<10;bit++){ if(ufs[bit].same(u,v)){ chmin(ans,Bs[bit]); } } if(ans==1LL<<60)cout<<-1<<"\n"; else cout<