import std.conv, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, std.range, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = cast(int)(as.length); for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; } int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a < val)); } int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a <= val)); } Int mod(Int)(Int a, Int m) { if ((a %= m) < 0) a += m; return a; } Int gcd(Int)(Int a, Int b) { return (b != 0) ? gcd(b, a % b) : a; } Int lcm(Int)(Int a, Int b) { return a / gcd(a, b) * b; } Int gojo(Int)(Int a, Int b, out Int x, out Int y) { if (b != 0) { Int g = gojo(b, a % b, y, x); y -= (a / b) * x; return g; } x = 1; y = 0; return a; } // as[i] T == bs[i] (mod ms[i]) Tuple!(Int, "b", Int, "m") modSol(Int)(Int[] as, Int[] bs, Int[] ms) { Int B = 0, M = 1; foreach (i; 0 .. ms.length) { const a = as[i] % ms[i], b = bs[i] % ms[i]; const b0 = b - a * B; Int x, y; const g = gojo(a * M, ms[i], x, y); if (b0 % g != 0) { B = M = 0; break; } const m0 = ms[i] / g; B += M * ((x * ((b0 / g) % m0)) % m0); M *= m0; B %= M; } if (B < 0) B += M; return Tuple!(Int, "b", Int, "m")(B, M); } int N, K; int[] X, Y; int Q; int[][] A; void main() { try { for (; ; ) { N = readInt(); K = readInt(); X = new int[K]; Y = new int[K]; foreach (k; 0 .. K) { X[k] = readInt() - 1; Y[k] = readInt() - 1; } Q = readInt(); A = new int[][](Q, N); foreach (q; 0 .. Q) { foreach (u; 0 .. N) { A[q][u] = readInt() - 1; } } auto P = iota(N).array; foreach (k; 0 .. K) { swap(P[X[k]], P[Y[k]]); } debug { writeln("P = ", P); } auto vis = new bool[N]; int[][] cycles; foreach (u; 0 .. N) { if (!vis[u]) { cycles ~= [[]]; for (int v = u; !vis[v]; v = P[v]) { cycles[$ - 1] ~= v; vis[v] = true; } } } auto es = new int[N]; auto fs = new int[N]; foreach (int e, cycle; cycles) { foreach (int f, u; cycle) { es[u] = e; fs[u] = f; } } debug { writeln("cycles = ", cycles); } foreach (q; 0 .. Q) { long ans; long[] as, bs, ms; foreach (int e, cycle; cycles) { const len = cast(int)(cycle.length); int[] df; foreach (int f, u; cycle) { const ee = es[A[q][u]]; const ff = fs[A[q][u]]; if (e != ee) { ans = -1; goto failed; } df ~= (ff - f + len) % len; } df = df.sort.uniq.array; if (df.length != 1) { ans = -1; goto failed; } as ~= 1; bs ~= df[0]; ms ~= len; } { debug { writeln("bs = ", bs); writeln("ms = ", ms); } auto res = modSol(as, bs, ms); if (res.m == 0) { ans = -1; goto failed; } ans = res.b; if (ans == 0) { ans = res.m; } } failed: writeln(ans); } } } catch (EOFException e) { } }