import random def mod_sqrt(a: int, p: int) -> int: if a == 0: return 0 if p == 2: return 1 k = (p - 1) // 2 if pow(a, k, p) != 1: return -1 while True: n = random.randint(2, p - 1) r = (n ** 2 - a) % p if r == 0: return n if pow(r, k, p) == p - 1: break k += 1 w, x, y, z = n, 1, 1, 0 while k: if k % 2: y, z = w * y + r * x * z, x * y + w * z w, x = w * w + r * x * x, 2 * w * x w %= p; x %= p; y %= p; z %= p k >>= 1 return y import typing # 拡張Euclidの互除法 def extgcd(a: int, b: int, d: int = 0) -> typing.Tuple[int, int, int]: g = a if b == 0: x, y = 1, 0 else: x, y, g = extgcd(b, a % b) x, y = y, x - a // b * y return x, y, g # mod p における逆元 def invmod(a: int, p: int) -> int: x, y, g = extgcd(a, p) x %= p return x p, r = map(int, input().split()) q = int(input()) for _ in range(q): a, b, c = map(int, input().split()) n = ((b ** 2) * invmod(4 * a, p) % p - c) * invmod(a, p) % p m = mod_sqrt(n, p) ans = [] if m == 0: tmp = (m - b * invmod(2 * a, p)) % p if (a * tmp ** 2 + b * tmp + c) % p == 0: ans.append(tmp) else: m2 = p - m x, y = (m - b * invmod(2 * a, p)) % p, (m2 - b * invmod(2 * a, p)) % p if x > y: x, y = y, x if (a * x ** 2 + b * x + c) % p == 0: ans.append(x) if (a * y ** 2 + b * y + c) % p == 0: ans.append(y) if len(ans) == 0: print(-1) else: print(*sorted(ans))