#!/usr/bin/env python3 # † import random def mod_inv(a, m): b, s, t = m, 1, 0 while b: q = a // b a %= b a, b = b, a s -= t * q s, t = t, s if a != 1: return None return s + m if s < 0 else s # ヤコビ記号 (a/n) def jacobi(a, n): assert 0 < n and n & 1 a %= n t = 1 while a != 0: while a & 1 == 0: a >>= 1 if n & 7 in (3, 5): t = -t a, n = n, a if a & 3 == 3 and n & 3 == 3: t = -t a %= n return t * (n == 1) def mod_sqrt(n, p): if jacobi(n, p) == -1: return -1 def mul(a, b, c, d, sq): x0 = (a*c + sq*b*d) % p x1 = (a*d + b*c) % p return x0, x1 a = 1 while jacobi(a*a - n, p) == 1: a = random.randint(1, p-1) m = (p+1) // 2 sq = a*a - n r0, r1, x0, x1 = 1, 0, a, 1 while m: if m & 1: r0, r1 = mul(r0, r1, x0, x1, sq) x0, x1 = mul(x0, x1, x0, x1, sq) m >>= 1 return r0 ### if __name__ == '__main__': P, _ = map(int, input().split()) Q = int(input()) def f(a, b, c): D = (b*b - 4*a*c) % P inv2a = mod_inv(2*a, P) if D == 0: return -b * inv2a % P sq = mod_sqrt(D, P) if sq == -1: return -1 x0 = (-b + sq) * inv2a % P x1 = (-b - sq) * inv2a % P return ' '.join(map(str, sorted([x0, x1]))) for _ in range(Q): a, b, c = map(int, input().split()) res = f(a, b, c) print(res)