#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
long long fast_exp(long long, long long, long long);
int /*<<< orphan*/ printf(char *, long long);
int /*<<< orphan*/ scanf(char *, ...);
long long totientFunc(long long);
int main() {
  long long int a, b, c, x, tot, p, q, g;
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%lld %lld %lld", &a, &b, &x);
    tot = totientFunc(x);
    if (b > 0) {
      b = b % tot;
      c = fast_exp(a, b, x);
    } else if (b < 0) {
      b = b * (-1);
      p = (tot - 1) * b;
      p = p % tot;
      c = fast_exp(a, p, x);
    } else {
      c = 1;
    }
    printf("%lld\n", c);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
lea    -0x4c(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
mov    -0x4c(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x4c(%rbp)
cmp    $0x0,%eax
je     123e <main+0xfe>
lea    0xe87(%rip),%rdi
lea    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x28(%rbp),%rcx
mov    $0x0,%al
call   1040 <scanf@plt>
mov    -0x28(%rbp),%rdi
call   1060 <totientFunc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x18(%rbp)
jle    11cf <main+0x8f>
mov    -0x18(%rbp),%rax
cqto
idivq  -0x30(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <fast_exp@plt>
mov    %rax,-0x20(%rbp)
jmp    1229 <main+0xe9>
cmpq   $0x0,-0x18(%rbp)
jge    121c <main+0xdc>
imul   $0xffffffffffffffff,-0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%rax
sub    $0x1,%rax
imul   -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cqto
idivq  -0x30(%rbp)
mov    %rdx,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <fast_exp@plt>
mov    %rax,-0x20(%rbp)
jmp    1224 <main+0xe4>
movq   $0x1,-0x20(%rbp)
jmp    1229 <main+0xe9>
mov    -0x20(%rbp),%rsi
lea    0xdde(%rip),%rdi
call   1030 <printf@plt>
jmp    1161 <main+0x21>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
lea    0xeac(%rip),%rdi
lea    0x4(%rsp),%rsi
xor    %eax,%eax
call   1040 <scanf@plt>
subl   $0x1,0x4(%rsp)
jae    1177 <main+0x37>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xe85(%rip),%r14
lea    0x18(%rsp),%r15
lea    0x10(%rsp),%r12
lea    0x8(%rsp),%r13
lea    0xe7e(%rip),%rbx
jmp    11ce <main+0x8e>
cs nopw 0x0(%rax,%rax,1)
cqto
idiv   %rcx
mov    %rdx,%rsi
mov    %rsi,0x10(%rsp)
mov    0x18(%rsp),%rdi
mov    0x8(%rsp),%rdx
call   1050 <fast_exp@plt>
mov    %rax,%rsi
mov    %rbx,%rdi
call   1030 <printf@plt>
subl   $0x1,0x4(%rsp)
jb     1167 <main+0x27>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
xor    %eax,%eax
call   1040 <scanf@plt>
mov    0x8(%rsp),%rdi
call   1060 <totientFunc@plt>
mov    %rax,%rcx
mov    0x10(%rsp),%rax
test   %rax,%rax
jle    1210 <main+0xd0>
mov    %rcx,%rdx
or     %rax,%rdx
shr    $0x20,%rdx
jne    11a0 <main+0x60>
xor    %edx,%edx
div    %ecx
mov    %edx,%esi
jmp    11a8 <main+0x68>
nopl   0x0(%rax)
mov    $0x1,%esi
jns    11bf <main+0x7f>
neg    %rax
mov    %rax,0x10(%rsp)
lea    -0x1(%rcx),%rdx
imul   %rax,%rdx
mov    %rcx,%rax
or     %rdx,%rax
shr    $0x20,%rax
je     123d <main+0xfd>
mov    %rdx,%rax
cqto
idiv   %rcx
jmp    1243 <main+0x103>
mov    %edx,%eax
xor    %edx,%edx
div    %ecx
mov    0x18(%rsp),%rdi
mov    0x8(%rsp),%rax
mov    %rdx,%rsi
mov    %rax,%rdx
jmp    11b7 <main+0x77>

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [15 x i8] c"%lld %lld %lld\00"
@.str.2 = [6 x i8] c"%lld\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
store i32 0, i32* %1
%11 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %10)
br label %12
12: ; preds = %49, %0
%13 = load i32, i32* %10
%14 = add nsw i32 %13, -1
store i32 %14, i32* %10
%15 = icmp ne i32 %13, 0
br i1 %15, label %16, label %52
16: ; preds = %12
%17 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i64* %2, i64* %3, i64* %5)
%18 = load i64, i64* %5
%19 = call i64 @totientFunc(i64 %18)
store i64 %19, i64* %6
%20 = load i64, i64* %3
%21 = icmp sgt i64 %20, 0
br i1 %21, label %22, label %30
22: ; preds = %16
%23 = load i64, i64* %3
%24 = load i64, i64* %6
%25 = srem i64 %23, %24
store i64 %25, i64* %3
%26 = load i64, i64* %2
%27 = load i64, i64* %3
%28 = load i64, i64* %5
%29 = call i64 @fast_exp(i64 %26, i64 %27, i64 %28)
store i64 %29, i64* %4
br label %49
30: ; preds = %16
%31 = load i64, i64* %3
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %47
33: ; preds = %30
%34 = load i64, i64* %3
%35 = mul nsw i64 %34, -1
store i64 %35, i64* %3
%36 = load i64, i64* %6
%37 = sub nsw i64 %36, 1
%38 = load i64, i64* %3
%39 = mul nsw i64 %37, %38
store i64 %39, i64* %7
%40 = load i64, i64* %7
%41 = load i64, i64* %6
%42 = srem i64 %40, %41
store i64 %42, i64* %7
%43 = load i64, i64* %2
%44 = load i64, i64* %7
%45 = load i64, i64* %5
%46 = call i64 @fast_exp(i64 %43, i64 %44, i64 %45)
store i64 %46, i64* %4
br label %48
47: ; preds = %30
store i64 1, i64* %4
br label %48
48: ; preds = %47, %33
br label %49
49: ; preds = %48, %22
%50 = load i64, i64* %4
%51 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %50)
br label %12
52: ; preds = %12
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [15 x i8] c"%lld %lld %lld\00"
@.str.2 = [6 x i8] c"%lld\0A\00"
define i32 @main() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i64
%4 = alloca i32
%5 = bitcast i64* %1 to i8*
%6 = bitcast i64* %2 to i8*
%7 = bitcast i64* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %4)
%10 = load i32, i32* %4
%11 = add nsw i32 %10, -1
store i32 %11, i32* %4
%12 = icmp eq i32 %10, 0
br i1 %12, label %40, label %13
13: ; preds = %0, %34
%14 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i64* %1, i64* %2, i64* %3)
%15 = load i64, i64* %3
%16 = call i64 @totientFunc(i64 %15)
%17 = load i64, i64* %2
%18 = icmp sgt i64 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %13
%20 = srem i64 %17, %16
store i64 %20, i64* %2
%21 = load i64, i64* %1
%22 = load i64, i64* %3
%23 = call i64 @fast_exp(i64 %21, i64 %20, i64 %22)
br label %34
24: ; preds = %13
%25 = icmp slt i64 %17, 0
br i1 %25, label %26, label %34
26: ; preds = %24
%27 = sub nsw i64 0, %17
store i64 %27, i64* %2
%28 = add nsw i64 %16, -1
%29 = mul nsw i64 %28, %27
%30 = srem i64 %29, %16
%31 = load i64, i64* %1
%32 = load i64, i64* %3
%33 = call i64 @fast_exp(i64 %31, i64 %30, i64 %32)
br label %34
34: ; preds = %24, %26, %19
%35 = phi i64 [ %23, %19 ], [ %33, %26 ], [ 1, %24 ]
%36 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %35)
%37 = load i32, i32* %4
%38 = add nsw i32 %37, -1
store i32 %38, i32* %4
%39 = icmp eq i32 %37, 0
br i1 %39, label %40, label %13
40: ; preds = %34, %0
ret i32 0
}