#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; const double PI = 3.14159265358979323846; const double EPS = 1e-12; const int INF = 1<<25; typedef pair P; typedef long long ll; typedef unsigned long long ull; #define T 10000000 #define R 33000 #define N 200 int n, m, r[N], s[N], s2[N], s3[N], t[R][N], a[N], tmp[N]; void ap(int *a, int *b){ memcpy(tmp, a, sizeof(tmp)); for(int i = 0; i < n; i++) a[i] = tmp[b[i]]; } int lcm(int a, int b){ return a/__gcd(a, b)*b; } int main(){ srand(time(NULL)); cin>>n>>m; int rnd1 = rand()%n, rnd2 = rand()%n, rr = 0, cc = 0; for(int i = 0; i < n; i++) r[i] = i; memcpy(s, r, sizeof(s)); memcpy(s2, r, sizeof(s)); memcpy(s3, r, sizeof(s)); for(int i = 0; i < m; i++){ int x, y; cin>>x>>y; swap(s[x-1], s[x]); } ll E = 1; for(int i = 0; i < n; i++){ int cnt = 1, p = s[i]; while(p!=i){ p = s[p]; cnt++; } if(cnt>cc){ cc = cnt; rr = i; } E = lcm(E, cnt); } ll F = sqrt(E)+2; for(int i = 0; i < F; i++){ ap(s2, s); //for(int i = 0; i < n; i++) cout<>Q; while(Q--){ for(int i = 0; i < n; i++){ cin>>a[i]; a[i]--; } int res = -1; for(int i = 0; i < F; i++){ for(int j = 0; j < F; j++){ if(a[rr]==t[j][rr]&&!memcmp(a, t[j], sizeof(a))){ //cerr<