#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 pow(long long, long long);
int /*<<< orphan*/ printf(char *, long long);
int /*<<< orphan*/ scanf(char *, long long *, ...);
int main() {
  long long int a, b, x, p, i, j, k, t;
  scanf("%lld", &t);
  for (i = 0; i < t; i++) {
    scanf("%lld %lld %lld", &a, &b, &x);
    p = pow(a, b);
    k = p % x;
    if (k > x / 2) {
      printf("%lld\n", (p / x) * x + x);
    } else {
      printf("%lld\n", (p / x) * x);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xec4(%rip),%rdi
lea    -0x48(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x48(%rbp),%rax
jge    1221 <main+0xf1>
lea    0xe92(%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
call   1050 <pow@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cqto
idivq  -0x20(%rbp)
mov    %rdx,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x20(%rbp),%rax
mov    $0x2,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x50(%rbp),%rax
cmp    %rcx,%rax
jle    11ed <main+0xbd>
mov    -0x28(%rbp),%rax
cqto
idivq  -0x20(%rbp)
mov    %rax,%rsi
imul   -0x20(%rbp),%rsi
add    -0x20(%rbp),%rsi
lea    0xe2c(%rip),%rdi
call   1030 <printf@plt>
jmp    120b <main+0xdb>
mov    -0x28(%rbp),%rax
cqto
idivq  -0x20(%rbp)
mov    %rax,%rsi
imul   -0x20(%rbp),%rsi
lea    0xe09(%rip),%rdi
call   1030 <printf@plt>
jmp    1210 <main+0xe0>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1159 <main+0x29>
xor    %eax,%eax
add    $0x50,%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    0xec5(%rip),%rdi
xor    %ebp,%ebp
lea    0x8(%rsp),%rsi
xor    %eax,%eax
call   1040 <scanf@plt>
cmpq   $0x0,0x8(%rsp)
jle    11f5 <main+0xc5>
lea    0xe9a(%rip),%r14
lea    0x20(%rsp),%r15
lea    0x18(%rsp),%r12
lea    0x10(%rsp),%r13
lea    0xe93(%rip),%rbx
jmp    11ba <main+0x8a>
xchg   %ax,%ax
mov    %rsi,%rax
cqto
idiv   %rcx
mov    %rcx,%rax
shr    $0x3f,%rax
add    %rcx,%rax
sar    %rax
cmp    %rax,%rdx
mov    $0x0,%eax
cmovle %rax,%rcx
add    %rcx,%rsi
sub    %rdx,%rsi
mov    %rbx,%rdi
call   1030 <printf@plt>
add    $0x1,%rbp
cmp    0x8(%rsp),%rbp
jge    11f5 <main+0xc5>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
xor    %eax,%eax
call   1040 <scanf@plt>
mov    0x20(%rsp),%rdi
mov    0x18(%rsp),%rsi
call   1050 <pow@plt>
mov    %rax,%rsi
mov    0x10(%rsp),%rcx
or     %rcx,%rax
shr    $0x20,%rax
jne    1180 <main+0x50>
mov    %esi,%eax
xor    %edx,%edx
div    %ecx
jmp    1188 <main+0x58>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
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
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32 0, i32* %1
%10 = call i32 (i8*, i64*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64* %9)
store i64 0, i64* %6
br label %11
11: ; preds = %44, %0
%12 = load i64, i64* %6
%13 = load i64, i64* %9
%14 = icmp slt i64 %12, %13
br i1 %14, label %15, label %47
15: ; preds = %11
%16 = 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)
%17 = load i64, i64* %2
%18 = load i64, i64* %3
%19 = call i64 @pow(i64 %17, i64 %18)
store i64 %19, i64* %5
%20 = load i64, i64* %5
%21 = load i64, i64* %4
%22 = srem i64 %20, %21
store i64 %22, i64* %8
%23 = load i64, i64* %8
%24 = load i64, i64* %4
%25 = sdiv i64 %24, 2
%26 = icmp sgt i64 %23, %25
br i1 %26, label %27, label %36
27: ; preds = %15
%28 = load i64, i64* %5
%29 = load i64, i64* %4
%30 = sdiv i64 %28, %29
%31 = load i64, i64* %4
%32 = mul nsw i64 %30, %31
%33 = load i64, i64* %4
%34 = add nsw i64 %32, %33
%35 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %34)
br label %43
36: ; preds = %15
%37 = load i64, i64* %5
%38 = load i64, i64* %4
%39 = sdiv i64 %37, %38
%40 = load i64, i64* %4
%41 = mul nsw i64 %39, %40
%42 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %41)
br label %43
43: ; preds = %36, %27
br label %44
44: ; preds = %43
%45 = load i64, i64* %6
%46 = add nsw i64 %45, 1
store i64 %46, i64* %6
br label %11
47: ; preds = %11
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 = icmp sgt i64 %10, 0
br i1 %11, label %12, label %29
12: ; preds = %0, %12
%13 = phi i64 [ %26, %12 ], [ 0, %0 ]
%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 = call i64 @pow(i64 %15, i64 %16)
%18 = load i64, i64* %3
%19 = srem i64 %17, %18
%20 = sdiv i64 %18, 2
%21 = icmp sgt i64 %19, %20
%22 = select i1 %21, i64 %18, i64 0
%23 = add i64 %17, %22
%24 = sub i64 %23, %19
%25 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %24)
%26 = add nuw nsw i64 %13, 1
%27 = load i64, i64* %4
%28 = icmp slt i64 %26, %27
br i1 %28, label %12, label %29
29: ; preds = %12, %0
ret i32 0
}