#define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; long long gcd(long long a, long long b){ while(b != 0){ long long tmp = a % b; a = b; b = tmp; } return a; } long long extgcd(long long a, long long b, long long &x, long long &y) { long long g = a; if(b != 0){ g = extgcd(b, a % b, y, x); y -= (a / b) * x; }else{ x = 1; y = 0; } return g; } long long mod_inverse(long long a, long long m) { long long x, y; extgcd(a, m, x, y); return (x % m + m) % m; } pair ChineseRemainderTheorem(const vector& a, const vector& b, const vector& m) { pair ret(0, 1); for(unsigned i=0; i> n >> k; vector from(n); for(int i=0; i> x >> y; swap(from[x-1], from[y-1]); } vector to(n); for(int i=0; i > cycle(n); for(int i=0; i> q; while(--q >= 0){ vector b(n), m(n); bool ng = false; for(int i=0; i> x; -- x; auto it = find(cycle[x].begin(), cycle[x].end(), i); if(it == cycle[x].end()){ ng = true; } else{ b[x] = it - cycle[x].begin(); m[x] = cycle[x].size(); } } if(ng){ cout << -1 << endl; } else{ auto ans = ChineseRemainderTheorem(vector(n, 1), b, m); if(ans.first == 0) cout << ans.second << endl; else cout << ans.first << endl; } } return 0; }