#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 modinv(long, long);
int powmod(long, long, long);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ scanf(char *, ...);
int main() {
  short t, i;
  long a, b, k, j;
  long long c = 1;
  scanf("%hd", &t);
  for (i = t; i; i--) {
    scanf("%ld %ld %ld", &a, &b, &k);
    if (b < 0) {
      a = modinv(a, k) % k;
      b = -b;
    }
    printf("%ld\n", (b) ? powmod(a, b, k) : 1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x1,-0x30(%rbp)
lea    0xea2(%rip),%rdi
lea    -0x6(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
mov    -0x6(%rbp),%ax
mov    %ax,-0x8(%rbp)
cmpw   $0x0,-0x8(%rbp)
je     1213 <main+0xd3>
lea    0xe81(%rip),%rdi
lea    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rcx
mov    $0x0,%al
call   1050 <scanf@plt>
cmpq   $0x0,-0x18(%rbp)
jge    11c2 <main+0x82>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <modinv@plt>
cqto
idivq  -0x20(%rbp)
mov    %rdx,-0x10(%rbp)
xor    %eax,%eax
sub    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11e6 <main+0xa6>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <powmod@plt>
mov    %eax,-0x34(%rbp)
jmp    11f3 <main+0xb3>
mov    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11f3 <main+0xb3>
mov    -0x34(%rbp),%esi
lea    0xe13(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%ax
add    $0xffff,%ax
mov    %ax,-0x8(%rbp)
jmp    1171 <main+0x31>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0xeab(%rip),%rdi
lea    0xe(%rsp),%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
movzwl 0xe(%rsp),%ebp
test   %bp,%bp
je     1232 <main+0xf2>
lea    0xe8e(%rip),%r14
lea    0x20(%rsp),%r15
lea    0x18(%rsp),%r12
lea    0x10(%rsp),%r13
lea    0xe84(%rip),%rbx
jmp    11b3 <main+0x73>
xchg   %ax,%ax
mov    0x20(%rsp),%rdi
mov    0x10(%rsp),%rdx
call   1060 <powmod@plt>
mov    %eax,%esi
mov    %rbx,%rdi
call   1030 <printf@plt>
add    $0xffff,%bp
je     1232 <main+0xf2>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
xor    %eax,%eax
call   1050 <scanf@plt>
mov    0x18(%rsp),%rsi
test   %rsi,%rsi
js     11e0 <main+0xa0>
test   %rsi,%rsi
jne    1190 <main+0x50>
jmp    1228 <main+0xe8>
nopw   0x0(%rax,%rax,1)
mov    0x20(%rsp),%rdi
mov    0x10(%rsp),%rsi
call   1040 <modinv@plt>
mov    0x10(%rsp),%rcx
mov    %rax,%rdx
or     %rcx,%rdx
shr    $0x20,%rdx
je     1207 <main+0xc7>
cqto
idiv   %rcx
jmp    120b <main+0xcb>
xor    %edx,%edx
div    %ecx
mov    %rdx,0x20(%rsp)
mov    0x18(%rsp),%rax
xor    %esi,%esi
sub    %rax,%rsi
mov    %rsi,0x18(%rsp)
test   %rsi,%rsi
jne    1190 <main+0x50>
mov    $0x1,%esi
jmp    11a1 <main+0x61>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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