import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, 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(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } // a^-1 (mod m) long modInv(long a, long m) in { assert(m > 0, "modInv: m > 0 must hold"); } do { long b = m, x = 1, y = 0, t; for (; ; ) { t = a / b; a -= t * b; if (a == 0) { assert(b == 1 || b == -1, "modInv: gcd(a, m) != 1"); if (b == -1) { y = -y; } return (y < 0) ? (y + m) : y; } x -= t * y; t = b / a; b -= t * a; if (b == 0) { assert(a == 1 || a == -1, "modInv: gcd(a, m) != 1"); if (a == -1) { x = -x; } return (x < 0) ? (x + m) : x; } y -= t * x; } } // xorshift uint xrand() { static uint x = 314159265, y = 358979323, z = 846264338, w = 327950288; uint t = x ^ x << 11; x = y; y = z; z = w; return w = w ^ w >> 19 ^ t ^ t >> 8; } // Jacobi symbol (a/m) int jacobi(long a, long m) in { assert(m > 0, "jacobi: m > 0 must hold"); assert(m & 1, "jacobi: m must be odd"); } do { import core.bitop : bsf; import std.algorithm.mutation : swap; int s = 1; if (a < 0) a = a % m + m; for (; m > 1; ) { a %= m; if (a == 0) return 0; const r = bsf(a); if ((r & 1) && ((m + 2) & 4)) s = -s; a >>= r; if (a & m & 2) s = -s; swap(a, m); } return s; } // sqrt(a) (mod p) // p: be prime // (b + sqrt(b^2 - a))^((p+1)/2) in F_p(sqrt(b^2 - a)) long[] modSqrt(long a, long p) in { assert(p < 1L << 32, "modSqrt: p < 2^32 must hold"); assert(-p * p <= a && a <= p * p, "modSqrt: -p^2 <= a <= p^2 must hold"); } do { if (p == 2) return [a & 1]; const j = jacobi(a, p); if (j == 0) return [0]; if (j == -1) return []; long b, d; for (; ; ) { b = xrand() % p; d = (b * b - a) % p; if (d < 0) d += p; if (jacobi(d, p) == -1) break; } long[2] multiply(in long[2] x, in long[2] y) { return [(x[0] * y[0] + d * ((x[1] * y[1]) % p)) % p, (x[0] * y[1] + x[1] * y[0]) % p]; } long[2] f = [b, 1], g = [1, 0]; for (long e = (p + 1) >> 1; e; e >>= 1) { if (e & 1) g = multiply(g, f); f = multiply(f, f); } assert(g[1] == 0); return (g[0] < p - g[0]) ? [g[0], p - g[0]] : [p - g[0], g[0]]; } void main() { try { for (; ; ) { const P = readLong(); const R = readLong(); const Q = readInt(); foreach (q; 0 .. Q) { const A = readLong(); const B = readLong(); const C = readLong(); long[] ans; foreach (d; modSqrt(B * B - 4 * A * C, P)) { long x = (-B + d) * modInv(2 * A, P) % P; x = (x % P + P) % P; ans ~= x; } ans.sort; if (ans.empty) { writeln(-1); } else { writeln(ans.to!string.replaceAll(regex(`[\[\],]`), "")); } } } } catch (EOFException e) { } }