#include using namespace std; typedef signed long long ll; #undef _P #define _P(...) (void)printf(__VA_ARGS__) #define FOR(x,to) for(x=0;x crt(ll a1,ll mo1,ll a2,ll mo2) { // return (x,y) y=lcm(a1,a2),x%mo1=a1,x%mo2=a2 ll g,x,y,z; g=ext_gcd(mo1,mo2,x,y); a1=(a1%mo1+mo1)%mo1;a2=(a2%mo2+mo2)%mo2; if(a1%g != a2%g) return pair(-1,0); // N/A ll lcm=mo1*(mo2/g); if(lcm(-2,0); // overflow ll v=a1+((a2-a1)%lcm+lcm)*x%lcm*(mo1/g); return make_pair(((v%lcm)+lcm) % lcm,lcm); } void solve() { int i,j,k,l,r,x,y; string s; cin>>N; FOR(i,N) bar[i]=i; cin>>K; FOR(i,K) { cin>>x>>y; swap(bar[x-1],bar[x]); } FOR(i,N) rev[bar[i]]=i; MINUS(step); FOR(i,N) { step[i][i]=0; par[i]=1; x=rev[i]; while(x!=i) { step[i][x]=par[i]++; x=rev[x]; } } cin>>Q; while(Q--) { pair p(0,1); FOR(i,N) cin>>A[i], A[i]--; FOR(i,N) { if(step[A[i]][i]==-1) { cout<<-1<