def solve(b, n, m): if pow(m, b, b) != 1: return -1 if n == 0: return 0 b2n = b ** (2 * n) mbn = m for _ in range(n): mbn = pow(mbn, b, b2n) a = mbn // (b ** n) return a for _ in range(int(input())): b, n, m = map(int, input().split()) print(solve(b, n, m))