#include #include using namespace std; typedef long long ll; inline ll mod_inverse(ll a, ll m){ ll b = m, u = 1, v = 0; while (b) { ll t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return (u % m + m) % m; } inline ll gcd(ll a, ll b){ if(b == 0){ return a; } return gcd(b, a % b); } pair linear_congruence(const vector& A, const vector& B, const vector& M){ ll x = 0, m = 1; for(int i=0;i<(int)A.size();i++){ ll a = A[i] * m, b = B[i] - A[i] * x, d = gcd(M[i], a); if(b % d != 0){ return make_pair(0, -1); } ll t = b / d * mod_inverse(a / d, M[i] / d) % (M[i] / d); x = x + m * t; m *= M[i] / d; } return make_pair(x % m, m); } int main(){ int N, K; cin >> N >> K; vector nxt(N); for(int i=0;i> X >> Y; swap(nxt[X-1], nxt[Y-1]); } int Q; cin >> Q; while(Q--){ vector A(N); for(int i=0;i> A[i]; --A[i]; } bool valid = true; vector B(N), M(N); for(int i=0;i(N, 1), B, M); if(res.second == -1){ cout << -1 << endl; } else { if(res.first == 0){ cout << res.second << endl; } else { cout << res.first << endl; } } } return 0; }