#include #include #include using namespace atcoder; using mint = modint1000000007; using namespace std; #define rep(i,n) for(int i=0;i<(n);i++) #define Inf 1000000001 int main(){ int N,K; cin>>N>>K; vector a(N); rep(i,N)a[i] = i; rep(i,K){ int x,y;cin>>x>>y; swap(a[x-1],a[y-1]); } int q; cin>>q; rep(_,q){ vector b(N); rep(i,N){ cin>>b[i]; b[i]--; } vector m,r; bool F = true; rep(i,N){ int cc = i; int M=0,R=-1; rep(j,10000){ cc = a[cc]; if(cc==b[i]){ R = j+1; } if(cc==i){ M = j+1; break; } } if(R==-1){ F = false; } else{ m.push_back(M); r.push_back(R%M); } } if(!F){ cout<<-1<