def ej(l):
    for i in l:
        print((i[0]**i[1])%i[2])
if __name__ == '__main__':
    ej([[int(i) for i in input().split(' ')] for _ in range(int(input()))])

76aa5209a98451b4f7b13d9499f29f839ac7bf3b
<mask token>

def ej(l):
    for i in l:
        print(i[0] ** i[1] % i[2])


<mask token>

def ej(l):
    for i in l:
        print(i[0] ** i[1] % i[2])


if __name__ == '__main__':
    ej([[int(i) for i in input().split(' ')] for _ in range(int(input()))])

def ej(l):
    for i in l:
        print((i[0]**i[1])%i[2])
if __name__ == '__main__':
    ej([[int(i) for i in input().split(' ')] for _ in range(int(input()))])
