def primeFactor(N): i = 2 ret = {} n = N mrFlg = 0 while i*i <= n: k = 0 while n % i == 0: n //= i k += 1 if k: ret[i] = k i += 1 + i%2 if i == 101 and n >= 2**20: def findFactorRho(N): def gcd(a, b): while b: a, b = b, a % b return a def f(x, c): return (x * x + c) % N for c in range(1, 99): X, d, j = [2], 1, 0 while d == 1: j += 1 X.append(f(X[-1], c)) X.append(f(X[-1], c)) d = gcd(abs(X[2*j]-X[j]), N) if d != N: if isPrimeMR(d): return d elif isPrimeMR(N//d): return N//d while n > 1: if isPrimeMR(n): ret[n], n = 1, 1 else: mrFlg = 1 j = findFactorRho(n) k = 0 while n % j == 0: n //= j k += 1 ret[j] = k if n > 1: ret[n] = 1 if mrFlg > 0: ret = {x: ret[x] for x in sorted(ret)} return ret def isPrimeMR(n): if n == 2: return True if n == 1 or n & 1 == 0: return False d = (n - 1) >> 1 while d & 1 == 0: d >>= 1 L = [2, 7, 61] if n < 1<<32 else [2, 13, 23, 1662803] if n < 1<<40 else [2, 3, 5, 7, 11, 13, 17] if n < 1<<48 else [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] for a in L: t = d y = pow(a, t, n) while t != n - 1 and y != 1 and y != n - 1: y = (y * y) % n t <<= 1 if y != n - 1 and t & 1 == 0: return False return True def discrete_logarithm(a, b, mod): a %= mod b %= mod m = int(mod**0.5+0.9) + 1 s = 1 for i in range(m): if s == b: return i s = s * a % mod inva = pow(a, mod-2, mod) am = pow(a, m, mod) D = {} k = b for i in range(m): if k not in D: D[k] = i k = k * inva % mod k = 1 for i in range(m): if k in D: return D[k] + i * m k = k * am % mod return -1 def primitive_root(mod, pf): for i in range(1, mod): for q in pf: if pow(i, (mod-1) // q, mod) == 1: break else: return i def gcd(a, b): while b: a, b = b, a % b return abs(a) def inv(x, nn): def invv(a, n): if a == 1: return (1, 0) if a == 0: return (-1, -1) b, m = a, n while m != 0: b,m = m,b%m if b > 1: return (-1, -1) k = n // a y, yy = invv(n % a, a) if y < 0: return (-1, -1) x = yy - k * y while x < 0: x += n y -= a return (x, y) return invv(x, nn)[0] T = int(input()) for _ in range(T): p, k, a = map(int, input().split()) pp = p-1 pf = primeFactor(pp) h = primitive_root(p, pf) b = discrete_logarithm(h, a, p) while True: g = gcd(k, pp) if g == 1: break if b % g: print(-1) break b //= g k //= g pp //= g if g > 1: continue print(pow(h, b * inv(k, pp) % pp, p))