#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 power_mod(long long, long long, long long);
int /*<<< orphan*/ printf(char *, long long);
int /*<<< orphan*/ scanf(char *, long long *, ...);
int main() {
  long long int a, b, m;
  long long int res, t;
  scanf("%lld", &t);
  while (t--) {
    scanf("%lld %lld %lld", &a, &b, &m);
    res = power_mod(a, b, m);
    printf("%lld\n", res);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xec4(%rip),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x30(%rbp)
cmp    $0x0,%rax
je     11ae <main+0x7e>
lea    0xe8f(%rip),%rdi
lea    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rcx
mov    $0x0,%al
call   1040 <scanf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <power_mod@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xe6b(%rip),%rdi
call   1030 <printf@plt>
jmp    1151 <main+0x21>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
lea    0xec6(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1040 <scanf@plt>
subq   $0x1,(%rsp)
jb     11b9 <main+0x89>
lea    0xea4(%rip),%r14
lea    0x18(%rsp),%r15
lea    0x10(%rsp),%r12
lea    0x8(%rsp),%r13
lea    0xe9d(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
xor    %eax,%eax
call   1040 <scanf@plt>
mov    0x18(%rsp),%rdi
mov    0x10(%rsp),%rsi
mov    0x8(%rsp),%rdx
call   1050 <power_mod@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
subq   $0x1,(%rsp)
jae    1180 <main+0x50>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%lld\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
store i32 0, i32* %1
%7 = call i32 (i8*, i64*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64* %6)
br label %8
8: ; preds = %12, %0
%9 = load i64, i64* %6
%10 = add nsw i64 %9, -1
store i64 %10, i64* %6
%11 = icmp ne i64 %9, 0
br i1 %11, label %12, label %20
12: ; preds = %8
%13 = call i32 (i8*, i64*, ...) @scanf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i64* %2, i64* %3, i64* %4)
%14 = load i64, i64* %2
%15 = load i64, i64* %3
%16 = load i64, i64* %4
%17 = call i64 @power_mod(i64 %14, i64 %15, i64 %16)
store i64 %17, i64* %5
%18 = load i64, i64* %5
%19 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %18)
br label %8
20: ; preds = %8
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"%lld\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 i64
%5 = bitcast i64* %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*, i64*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64* %4)
%10 = load i64, i64* %4
%11 = add nsw i64 %10, -1
store i64 %11, i64* %4
%12 = icmp eq i64 %10, 0
br i1 %12, label %23, label %13
13: ; preds = %0, %13
%14 = call i32 (i8*, i64*, ...) @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* %1
%16 = load i64, i64* %2
%17 = load i64, i64* %3
%18 = call i64 @power_mod(i64 %15, i64 %16, i64 %17)
%19 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %18)
%20 = load i64, i64* %4
%21 = add nsw i64 %20, -1
store i64 %21, i64* %4
%22 = icmp eq i64 %20, 0
br i1 %22, label %23, label %13
23: ; preds = %13, %0
ret i32 0
}