#include #define GET_MACRO(a, b, c, NAME, ...) NAME #define rep(...) GET_MACRO(__VA_ARGS__, rep3, rep2)(__VA_ARGS__) #define rep2(i, a) rep3 (i, 0, a) #define rep3(i, a, b) for (int i = (a); i < (b); i++) #define repr(...) GET_MACRO(__VA_ARGS__, repr3, repr2)(__VA_ARGS__) #define repr2(i, a) repr3 (i, 0, a) #define repr3(i, a, b) for (int i = (b) - 1; i >= (a); i--) #define chmin(a, b) ((b) < a && (a = (b), true)) #define chmax(a, b) (a < (b) && (a = (b), true)) using namespace std; typedef long long ll; template ostream &operator <<(ostream &os, const vector &v) { rep (i, v.size()) { if (i) os << " "; os << v[i]; } return os; } ll modulo(ll a, ll mod) { a %= mod; a += mod; a %= mod; return a; } pair extgcd(ll a, ll b) { if (b == 0) return {1, 0}; ll x, y; tie(x, y) = extgcd(b, a % b); return {y, x - a / b * y}; } ll modinv(ll a, ll mod) { if (a == 0) return -1; if (__gcd(a, mod) != 1) return -1; return modulo(extgcd(a, mod).first, mod); } ll garner(vector> eq, ll mod) { eq.emplace_back(0, mod); int n = eq.size(); vector coef(n, 1), sum(n, 0); rep (i, n - 1) { ll v = (eq[i].first - sum[i]) * modinv(coef[i], eq[i].second) % eq[i].second; if (v < 0) v += eq[i].second; rep (j, i + 1, n) { (sum[j] += coef[j] * v) %= eq[j].second; (coef[j] *= eq[i].second) %= eq[j].second; } } return sum.back(); } map primefactor(ll n) { map res; for (ll i = 2; i * i <= n; i++) { while (n % i == 0) { res[i]++; n /= i; } } if (n != 1) res[n]++; return res; } vector> normalize(vector> eq) { vector> pf(eq.size()); rep (i, eq.size()) { pf[i] = primefactor(eq[i].second); } map> pnum; // prime, value, index rep (i, pf.size()) { for (auto p : pf[i]) { if (pnum[p.first].first < p.second) { pnum[p.first].first = p.second; pnum[p.first].second = i; } } } for (auto p : pnum) { rep (i, eq.size()) if (i != p.second.second) { while (eq[i].second % p.first == 0) { eq[i].second /= p.first; } eq[i].first %= eq[i].second; } } return eq; } bool check(vector> eq) { rep (i, eq.size()) { rep (j, i + 1, eq.size()) { ll a1 = eq[i].first; ll a2 = eq[j].first; ll m1 = eq[i].second; ll m2 = eq[j].second; ll g = __gcd(m1, m2); ll l = m1 / g * m2; if ((a2 - a1) % g != 0) return false; } } return true; } int main() { int N, K; cin >> N >> K; vector s(N); rep (i, N) s[i] = i; rep (i, K) { int X, Y; cin >> X >> Y; X--; Y--; swap(s[X], s[Y]); } vector id(N, -1), index(N, -1), orbit(N); rep (i, N) if (id[i] == -1) { int curr = i; int j = 0; while (id[curr] == -1) { id[curr] = i; index[curr] = j++; orbit[i]++; curr = s[curr]; } } int Q; cin >> Q; rep (i_, Q) { vector A(N); rep (i, N) { cin >> A[i]; A[i]--; } bool bad = false; vector dist(N, -1); map mp; rep (i, N) { if (id[i] != id[A[i]]) bad = true; else { int orb = orbit[id[i]]; int d = modulo(index[A[i]] - index[i], orb); if (mp.count(orb)) { if (mp[orb] != d) bad = true; } else { mp[orb] = d; } } } if (bad) { cout << -1 << endl; continue; } vector> eq; for (auto kv : mp) { eq.emplace_back(kv.second, kv.first); } if (!check(eq)) { cout << -1 << endl; continue; } eq = normalize(eq); ll prod = 1; for (auto p : eq) prod *= p.second; const ll mod = 1e18; ll ans = garner(eq, mod); if (ans == 0) ans += prod; cout << ans << endl; } return 0; }