You are given 3 numbers a, b and x. You need to output the multiple of x which is closest to a^{b}. If more than one answer exists , display the smallest one. 

Input Format

The first line contains T, the number of testcases. 

T lines follow, each line contains 3 space separated integers (a, b and x respectively)

Constraints

1 ≤ T ≤ 10^{5} 

1 ≤ x ≤ 10^{9} 

0 < a^{b} ≤ 10^{9} 

1 ≤ a ≤ 10^{9} 

-10^{9} ≤ b ≤ 10^{9} 

Output Format

For each test case , output the multiple of x which is closest to a^{b} 

Sample Input 0
3
349 1 4
395 1 7
4 -2 2

Sample Output 0
348
392
0

Explanation 0

The closest multiple of 4 to 349 is 348. 

The closest multiple of 7 to 395 is 392. 

The closest multiple of 2 to 1/16 is 0.

Difficulty: MEDIUM
Tags: Number theory

Starter code:

Solutions:
for t in range(int(input())):
	inp = [int(a) for a in input().split()]
	x = inp[2]
	c = inp[0] ** inp[1]
	if c % x <= x / 2:
		print(int(c // x * x))
	else:
		print(int((c // x + 1) * x))


t = int(input())

def closest(x, mod):
	low = x // mod * mod
	high = low + mod
	return low if x - low <= high - x else high

def solve(a, b, x):
	if a == 1:
		return closest(1, x)
	if b >= 0:
		return closest(a ** b, x)
	return 0
for _ in range(t):
	(a, b, x) = map(int, input().split())
	print(solve(a, b, x))


import sys
T = int(sys.stdin.readline())
for _ in range(T):
	(a, b, x) = list(map(int, sys.stdin.readline().split()))
	d = int(a ** b / x)
	if abs(a ** b - d * x) <= abs(a ** b - (d + 1) * x):
		print(d * x)
	else:
		print((d + 1) * x)


T = int(input())
for t in range(T):
	(a, b, x) = map(int, input().split())
	c = a ** b
	i = int(c) // x
	if c - i * x <= (i + 1) * x - c:
		print(i * x)
	else:
		print((i + 1) * x)


n = int(input())
for _ in range(n):
	(a, b, x) = map(int, input().split())
	v = int(a ** b)
	rem = v % x
	if rem < x / 2:
		print(v - rem)
	else:
		print(v + x - rem)


r = int(input(''))
for g in range(r):
	t = input('').split(' ')
	t = [int(x) for x in t]
	m = t[0]
	n = t[1]
	r = t[2]
	power = pow(m, n)
	div = power // r * r
	div2 = div + r
	if power - div > div2 - power:
		print(int(div2))
	else:
		print(int(div))


from sys import stdin as cin
from math import factorial as f
for t in range(int(cin.readline())):
	(a, b, x) = map(int, cin.readline().split())
	r = pow(a, b)
	m = r - r % x
	n = m + x
	if abs(n - r) == abs(m - r):
		if n < m:
			print(int(n))
		else:
			print(int(m))
	elif abs(n - r) < abs(m - r):
		print(int(n))
	else:
		print(int(m))


T = int(input())
for _ in range(T):
	(a, b, x) = [int(_) for _ in input().split()]
	p = a ** b / x
	r = p - int(p)
	if r <= 0.5:
		print(int((p - r) * x))
	else:
		print(int((p - r + 1) * x))


T = int(input())
for t in range(T):
	(a, b, x) = map(int, input().split())
	if a != 1:
		(a, tmp) = (1, a)
		for i in range(b):
			a *= tmp
	if b < 0 and a != 1:
		print(0)
	else:
		n = a // x
		if abs((n + 1) * x - a) < abs(n * x - a):
			n += 1
		print(n * x)


test = int(input())
for t in range(test):
	(a, b, x) = map(int, input().split())
	a = pow(a, b)
	q = a // x
	if a % x == 0:
		print(int(a))
		continue
	c = q * x
	d = (q + 1) * x
	if abs(d - a) < abs(c - a):
		print(int(d))
	else:
		print(int(c))


for T in range(int(input())):
	(a, b, x) = (int(z) for z in input().split())
	if b < 0 and a != 1:
		print(0)
		continue
	elif a == 1 and b < 0:
		n = 1
	else:
		n = a ** b
	lo = x * (n // x)
	hi = lo + x
	if n - lo <= hi - n:
		print(lo)
	else:
		print(hi)


import math
t = int(input())
for i in range(t):
	(a, b, x) = list(map(int, input().split()))
	exp = a ** b
	flr = math.floor(a ** b / x) * x
	cei = math.ceil(a ** b / x) * x
	diff1 = math.fabs(flr - exp)
	diff2 = math.fabs(cei - exp)
	print(flr if diff1 < diff2 else cei)


import math
max = int(input())
for i in range(0, max):
	vallist = list(map(int, input().split()))
	target = math.pow(vallist[0], vallist[1])
	multiplier = target // vallist[2]
	val1 = int(multiplier * vallist[2])
	val2 = int(val1 + vallist[2])
	if target - val1 <= val2 - target:
		print(val1)
	else:
		print(val2)


T = int(input())
for _ in range(T):
	(a, b, x) = [int(i) for i in input().split()]
	res = a ** b + 0.0
	lo = round(res - res % x)
	up = lo + x
	if up - res > res - lo:
		print(lo)
	else:
		print(up)


test = int(input())
for _ in range(test):
	stri = input().split()
	(a, b, x) = (int(stri[0]), int(stri[1]), int(stri[2]))
	aRb = a ** b
	multiple = aRb // x
	multiple2 = multiple + 1
	multiple3 = multiple - 1
	first = multiple * x
	second = multiple2 * x
	third = multiple3 * x
	if abs(first - aRb) <= abs(second - aRb) and abs(first - aRb) <= abs(third - aRb):
		print(int(first))
	elif abs(second - aRb) <= abs(first - aRb) and abs(second - aRb) <= abs(third - aRb):
		print(int(second))
	else:
		print(int(third))


import sys
inp = sys.stdin.read().split(sep='\n')
T = inp.pop(0)
for i in range(int(T)):
	(a, b, x) = inp.pop(0).split()
	x = int(x)
	a = int(a)
	b = int(b)
	k = int(a ** b // x)
	if abs((k + 1) * x - a ** b) > abs(k * x - a ** b):
		print(k * x)
	else:
		print((k + 1) * x)


import math
t = int(input())
for _ in range(t):
	(a, b, x) = [int(x) for x in str(input()).split()]
	ab = a ** b
	floor = math.floor(ab / x) * x
	ceil = math.ceil(ab / x) * x
	ans = floor if math.fabs(ab - floor) < math.fabs(ab - ceil) else ceil
	print(ans)


import math
t = int(input())
for i in range(t):
	(a, b, x) = map(int, input().split())
	temp = math.pow(a, b)
	rem = temp % x
	rem1 = x - rem
	if temp > 0 and temp <= 0.5:
		print('0')
	elif temp > 0.5 and temp < 1:
		if x == 1:
			print('1')
		else:
			print('0')
	elif temp == 1:
		if x == 1:
			print('1')
		else:
			print('0')
	elif rem <= rem1:
		print(int(temp - rem))
	else:
		print(int(temp + rem1))


import math
t = int(input())
for _ in range(t):
	(a, b, x) = map(int, input().split())
	t = int(pow(a, b) // x)
	diff1 = pow(a, b) - x * t
	diff2 = x * (t + 1) - pow(a, b)
	if diff1 <= diff2:
		print(x * t)
	elif diff1 > diff2:
		print(x * (t + 1))


import math
t = int(input())
for _i in range(t):
	(a, b, x) = map(int, input().split())
	if b < 0:
		tar = math.pow(1.0 / a, abs(b))
		if tar > a - tar:
			print(1)
		else:
			print(0)
	else:
		tar = int(math.pow(a, b))
		k = tar // x
		k1 = tar % x
		if k1 == 0:
			print(tar)
		elif k1 > x - k1:
			print((k + 1) * x)
		else:
			print(k * x)


def find(a, b, x):
	ab_pow = a ** b
	first = int(ab_pow // x * x)
	second = first + x
	if abs(first - ab_pow) < abs(second - ab_pow):
		return first
	else:
		return second
n = int(input())
for index in range(0, n):
	nums = input().rstrip().split(' ')
	num = find(int(nums[0]), int(nums[1]), int(nums[2]))
	print(num)


for t in range(int(input())):
	(a, b, c) = [int(x) for x in input().split()]
	if abs(int((a ** b // c + 1) * c) - a ** b) < abs(int(a ** b // c * c) - a ** b):
		print(int((a ** b // c + 1) * c))
	else:
		print(int(a ** b // c * c))


def solve(a, b, x):
	if b < 0:
		if a > 1 or x > 1:
			return 0
		else:
			return 1
	elif b == 0:
		if x > 1:
			return 0
		else:
			return 1
	else:
		p = a ** b
		x_ = p // x * x
		if p - x_ <= x / 2:
			return x_
		else:
			return x_ + x
T = int(input())
for _ in range(T):
	(a, b, x) = (int(_) for _ in input().split())
	print(solve(a, b, x))


from math import floor
for t in range(int(input())):
	(a, b, x) = [int(i) for i in input().split()]
	a_pow_b = a ** b
	ans1 = floor(a_pow_b / x) * x
	ans2 = floor(a_pow_b / x + 1) * x
	if a_pow_b - ans1 > ans2 - a_pow_b:
		print(ans2)
	else:
		print(ans1)


t = int(input())
while t > 0:
	t -= 1
	l = input().split()
	l = [int(x) for x in l]
	a = l[0]
	b = l[1]
	x = l[2]
	powervalue = a ** b
	q = powervalue // x
	prev = q * x
	nxt = (q + 1) * x
	difprev = powervalue - prev
	difnext = nxt - powervalue
	if difnext < difprev:
		print(int(nxt))
	else:
		print(int(prev))


import math
C = int(input())
while C > 0:
	(Ai, Bi, Xi) = input().split()
	a = int(Ai)
	b = int(Bi)
	x = int(Xi)
	closest = -100
	i = 0
	if a is 1:
		print('1')
		C -= 1
		continue
	if b >= 0:
		i = math.floor(a ** b / x)
		closest = i * x
		if x * (i + 1) - a ** b < a ** b - closest:
			closest = x * (i + 1)
	else:
		closest = 0
	print(closest)
	C -= 1


cases = int(input())
for _ in range(cases):
	(a, b, x) = [int(i) for i in input().split()]
	target = a ** b
	low = int(target // x * x)
	high = low + x
	if target - low > high - target:
		print(high)
	else:
		print(low)


T = int(input())
for j in range(T):
	value = [int(i) for i in input().split()]
	(a, b, c) = (value[0], value[1], value[2])
	val = pow(a, b)
	leftnearest = int(val)
	leftnearest = leftnearest - leftnearest % c
	rightnearest = leftnearest + c
	if val - leftnearest <= rightnearest - val:
		print(int(leftnearest))
	else:
		print(int(rightnearest))


import math
T = int(input())
for _ in range(T):
	d = input().split()
	a = int(d[0])
	b = int(d[1])
	x = int(d[2])
	num = a ** b
	div = num / x
	mn = x * math.floor(div)
	mx = x * math.ceil(div)
	if mx - num > num - mn:
		print(mn)
	else:
		print(mx)


import sys
T = int(sys.stdin.readline())
for _ in range(T):
	(a, b, x) = list(map(int, sys.stdin.readline().split()))
	n = a ** b
	d = int(n / x)
	if abs(n - d * x) <= abs(n - (d + 1) * x):
		print(d * x)
	else:
		print((d + 1) * x)


for T in range(int(input())):
	(a, b, x) = map(int, input().split())
	print(int(a ** b) // x * x + (x if int(a ** b) % x > x / 2 else 0))


test = int(input())
while test > 0:
	test -= 1
	(a, b, x) = map(int, input().split())
	s = a ** b
	s1 = int(s) - int(s) % x
	s2 = s1 + x
	if s - s1 <= s2 - s:
		print(s1)
	else:
		print(s2)


numtest = int(input())
for z in range(numtest):
	(a, b, x) = input().split()
	a = int(a)
	b = int(b)
	x = int(x)
	m = a ** b / x
	diff = m - int(m)
	m = int(m)
	if diff <= 0.5:
		print(x * m)
	else:
		print(x * (m + 1))


def closestMultiple(a, b, x):
	pow = a ** b
	low = pow // x * x
	high = (pow // x + 1) * x
	if high - pow < pow - low:
		return high
	else:
		return low
num_cases = int(input())
for case in range(num_cases):
	(a, b, x) = map(int, input().split())
	print(int(closestMultiple(a, b, x)))


a = int(input())
for i in range(a):
	c = input().split(' ')
	(q, w, e) = (int(c[0]), int(c[1]), int(c[2]))
	qw = q ** w
	m = int(qw / e)
	x = e * m
	y = e * (m + 1)
	if y - qw < qw - x:
		print(y)
	else:
		print(x)


import math
t = int(input())
res = []
for i in range(t):
	(a, b, x) = [int(i) for i in input().split()]
	apb = pow(a, b)
	apb2 = int(math.ceil(apb))
	mod = apb2 % x
	res.append(apb2 - mod if apb - apb2 + mod <= apb2 + x - mod - apb else apb2 + x - mod)
for i in res:
	print(i)


import math

def solve():
	(a, b, x) = map(int, input().strip().split())
	r = math.pow(a, b)
	li = max(0, int(r / x) - 5)
	ls = int(r / x) + 5
	bestdiff = None
	for m in range(li, ls + 1):
		diff = abs(m * x - r)
		if bestdiff is None or diff < bestdiff:
			(bestm, bestdiff) = (m, diff)
	print(bestm * x)
T = int(input().strip())
for i in range(T):
	solve()


import math
for t in range(int(input())):
	(a, b, x) = (int(i) for i in input().split())
	z = a ** b
	l1 = int(z / x) * x
	l2 = int(z / x + 1) * x
	if l2 - z >= z - l1:
		print(l1)
	else:
		print(l2)


numTrials = int(input())
for i in range(0, numTrials):
	values = input().split()
	for j in range(0, len(values)):
		values[j] = int(values[j])
	a = values[0]
	b = values[1]
	x = values[2]
	result = a ** b
	time1 = int(result / x)
	time2 = time1 + 1
	if abs(result - time1 * x) < abs(result - time2 * x):
		times = time1
	else:
		times = time2
	print(x * times)


from math import log, exp
T = int(input())
for _ in range(T):
	(a, b, x) = [int(i) for i in input().split()]
	log_n = b * log(a) - log(x)
	print(x * int(round(exp(log_n))))


from math import pow

def closest_number(a, b, x):
	powered = int(pow(a, b))
	if powered < 1:
		return 0
	difference_negative = powered % x
	difference_positive = x - difference_negative
	if difference_negative == difference_positive:
		return powered - difference_negative
	elif min(difference_positive, difference_negative) == difference_negative:
		return powered - difference_negative
	else:
		return powered + difference_positive
t = int(input())
for case in range(t):
	tokens = input().split()
	a = int(tokens[0])
	b = int(tokens[1])
	x = int(tokens[2])
	print(closest_number(a, b, x))


T = int(input())
for i in range(0, T):
	A_B_X_split = str(input()).split(' ')
	A = int(A_B_X_split[0])
	B = int(A_B_X_split[1])
	X = int(A_B_X_split[2])
	if (A == 1) & (X == 1):
		print(1)
	else:
		A_mod_X = A % X
		if B > 0:
			A_mod_X_exp_B = A_mod_X ** B
			A_mod_X_exp_B_mod_X = A_mod_X_exp_B % X
			if A_mod_X_exp_B_mod_X <= X // 2:
				print(A ** B - A_mod_X_exp_B_mod_X)
			else:
				print(A ** B + (X - A_mod_X_exp_B_mod_X))
		else:
			print(0)


for _ in range(int(input())):
	(a, b, x) = map(int, input().split())
	fact = a ** b / x
	print(int(round(fact) * x))


import sys
T = int(sys.stdin.readline())
for _ in range(T):
	(a, b, x) = list(map(int, sys.stdin.readline().split()))
	n = a ** b
	d = int(n / x)
	if abs(n - d * x) <= abs(n - (d + 1) * x):
		print(d * x)
	else:
		print((d + 1) * x)


def closest(x, a, b):
	return a if x - a <= b - x else b
n = int(input())
for i in range(n):
	(base, power, test) = input().split()
	(base, power, test) = (int(base), int(power), int(test))
	check = int(pow(base, power))
	print(closest(check, check // test * test, (check // test + 1) * test))


import sys
T = int(sys.stdin.readline())
for _ in range(T):
	(a, b, x) = list(map(int, sys.stdin.readline().split()))
	n = a ** b
	d = int(n / x)
	if abs(n - d * x) <= abs(n - (d + 1) * x):
		print(d * x)
	else:
		print((d + 1) * x)


t = int(input())
for i in range(t):
	(a, b, x) = list(map(int, input().split()))
	ab = a ** b
	res = ab // x * x
	if ab - res > x / 2:
		res += x
	print(int(res))


for _ in range(int(input())):
	(a, b, x) = [int(_) for _ in input().split()]
	k = a ** b // x
	a_b = a ** b
	while True:
		if x * k > a_b:
			xh = x * k
			xl = x * (k - 1)
			break
		k += 1
	if xh - a_b >= a_b - xl:
		print(int(xl))
	else:
		print(int(xh))


def fun(a, b, x):
	res = 0
	t = a ** b
	if t >= 1:
		y = t % x
		if y <= x - y:
			res = t - y
		else:
			res = t + (x - y)
	else:
		res = 0
	return res

def main():
	for i in range(int(input())):
		(a, b, x) = input().split()
		(a, b, x) = (int(a), int(b), int(x))
		print(int(fun(a, b, x)))
main()


import sys
T = int(sys.stdin.readline())
for _ in range(T):
	(a, b, x) = list(map(int, sys.stdin.readline().split()))
	n = a ** b
	d = int(n / x)
	if abs(n - d * x) <= abs(n - (d + 1) * x):
		print(d * x)
	else:
		print((d + 1) * x)


MODULO = 1000000007
t = int(input())
for i in range(t):
	(a, b, x) = map(int, input().split())
	a = a ** b
	z = a // x
	(lm, um) = (z * x, (z + 1) * x)
	print(int(lm if abs(lm - a) <= abs(um - a) else um))


def closest(a, b):
	x = a // b * b
	if a - x < x + b - a:
		return x
	return x + b
t = int(input())
for _ in range(t):
	A = [int(x) for x in input().split()]
	if A[1] < 0:
		if A[0] == 1:
			print(closest(1, A[2]))
		else:
			print(0)
	else:
		print(closest(pow(A[0], A[1]), A[2]))


def closestMultiple(a, b, x):
	if a == 1 or b == 0:
		if x == 1:
			return 1
		else:
			return 0
	elif b < 0:
		return 0
	else:
		big = pow(a, b)
		remainder = pow(a, b, x)
		closest_multiple = big - remainder
		if remainder <= x // 2:
			return closest_multiple
		else:
			return closest_multiple + x

def main():
	t = int(input())
	for _ in range(t):
		(a, b, x) = list(map(int, input().split()))
		print(closestMultiple(a, b, x))
main()


t = int(input())
while t > 0:
	l = input().split()
	a = int(l[0])
	b = int(l[1])
	x = int(l[2])
	exp = a ** b
	diff1 = exp % x
	diff2 = x - diff1
	if diff2 < diff1:
		print(int(exp + (x - diff1)))
	else:
		print(int(exp - diff1))
	t -= 1


t = int(input())
while t:
	t = t - 1
	(a, b, x) = map(int, input().split())
	y = a ** b
	if y > 0 and y < 1:
		print(0)
		continue
	i = 0
	y = int(y)
	p = y // x
	(p1, p2) = (p * x, (p + 1) * x)
	if abs(y - p1) <= abs(y - p2):
		print(p1)
	else:
		print(p2)


for i in range(int(input())):
	[a, b, x] = [int(i) for i in input().split()]
	print(int(round(a ** b / x)) * x)


T = int(input())
while T != 0:
	T -= 1
	X = list(map(lambda x: int(x), input().split(' ')))
	Z = X[0] ** X[1] / X[2]
	if Z - int(Z) >= int(Z) - Z + 1:
		print((int(Z) + 1) * X[2])
	else:
		print(int(Z) * X[2])


T = int(input())
for i in range(0, T):
	(a, b, x) = map(int, input().split())
	k = a ** b
	m = int(k / x)
	if abs(m * x - k) <= abs((m + 1) * x - k):
		print(m * x)
	else:
		print((m + 1) * x)


import math
num_test_cases = int(input())
for i in range(num_test_cases):
	test_values = input()
	test_values = test_values.split()
	test_values = list(map(lambda x: int(x), test_values))
	a_to_the_b = math.pow(test_values[0], test_values[1])
	c = test_values[2]
	lower_value = a_to_the_b - a_to_the_b % c
	upper_value = lower_value + c
	if upper_value - a_to_the_b > a_to_the_b - lower_value:
		print(int(lower_value))
	else:
		print(int(upper_value))


import math

def Closest(a, b, x):
	t = math.pow(a, b) % x
	if t > x / 2:
		return math.pow(a, b) + (x - math.pow(a, b)) % x
	else:
		return math.pow(a, b) - math.pow(a, b) % x
N = int(input())
for i in range(0, N):
	(a, b, x) = input().split()
	(a, b, x) = (int(a), int(b), int(x))
	t = Closest(a, b, x)
	print(int(t))


t = int(input())
for case in range(t):
	(a, b, x) = tuple(map(int, input().split()))
	ab = a ** b
	mod = ab % x
	print(int(ab - mod) if mod <= x / 2 else int(ab + x - mod))


from math import ceil
num_cases = int(input())
for i in range(num_cases):
	line = input().split()
	target = int(line[0]) ** int(line[1])
	x = int(line[2])
	print(x * ceil(target / x - 0.5))


for _ in range(int(input())):
	(a, b, x) = [int(ns) for ns in str(input()).split()]
	c = a ** b
	r = c % x
	d = c - r
	e = d + x
	if e - c < c - d:
		print(e)
	else:
		print(int(d))


n = int(input())
for i in range(n):
	line = input()
	(a, b, x) = [int(y) for y in line.split()]
	goal = a ** b
	floor = goal // x
	lower = floor * x
	dist = goal - lower
	result = lower
	if x < 2 * dist:
		result = lower + x
	print(int(result))


t = int(input())
for _ in range(t):
	(a, b, x) = map(int, input().split())
	y = a ** b
	z = y % x
	one = int(y - z)
	two = one + x
	if abs(y - one) <= abs(y - two):
		print(one)
	else:
		print(two)


from sys import stdin
t = int(stdin.readline())
for ti in range(t):
	(a, b, x) = tuple((int(z) for z in stdin.readline().split()))
	p = pow(a, b)
	l = x * int(p / x)
	if p - l <= l + x - p:
		print(int(l))
	else:
		print(int(x + l))


for _ in range(int(input())):
	(a, b, x) = [int(n) for n in input().split()]
	n = a ** b
	if n < 1:
		print(0)
	elif abs(x * (n // x) - n) < abs(x * (n // x) + x - n):
		print(int(x * (n // x)))
	else:
		print(int(x * (n // x)) + x)


t = int(input())
for i in range(0, t):
	s = input()
	ss = s.split(' ')
	d = int(ss[0])
	b = int(ss[1])
	c = int(ss[2])
	a = d ** b
	tem = a % c
	if tem == 0:
		print(int(a))
	else:
		tem = int(a / c)
		tem1 = tem * c
		tem2 = (tem + 1) * c
		if a - tem1 <= tem2 - a:
			print(int(tem1))
		else:
			print(int(tem2))


t = int(input().strip())
for j in range(t):
	(a, b, x) = [int(i) for i in input().strip().split(' ')]
	c = pow(a, b)
	print(int(c // x * x + (c % x > x // 2) * x))


import math
from fractions import Fraction
t = int(input())
while t:
	(a, b, x) = [int(elem) for elem in input().split()]
	if b > 0:
		c = pow(a, b, x)
		if c > x // 2:
			print(pow(a, b) - c + x)
		else:
			print(pow(a, b) - pow(a, b, x))
	elif a == 1:
		print(1)
	else:
		print(0)
	t = t - 1


tests = int(input().strip())
for test in range(tests):
	line = input().strip()
	(a, b, x) = [int(i) for i in line.split()]
	(q, r) = divmod(a ** b, x)
	if r * 2 > x:
		result = int((q + 1) * x)
	else:
		result = int(q * x)
	print(result)


T = int(input())
for i in range(T):
	listie = input().split()
	a = int(listie[0])
	b = int(listie[1])
	c = int(listie[2])
	a = pow(a, b)
	d = a % c
	slt1 = a - d
	slt2 = a + c - d
	if abs(slt1 - a) > abs(slt2 - a):
		print(int(slt2))
	else:
		print(int(slt1))


import math
nTst = int(input())
inp = []
for i1 in range(nTst):
	inp.append(list(map(int, input().split())))
for i1 in range(nTst):
	a1 = inp[i1][0]
	b1 = inp[i1][1]
	x1 = inp[i1][2]
	y1 = a1 ** b1
	c1 = math.floor(y1 / x1)
	if abs(y1 - c1 * x1) <= abs(y1 - c1 * x1 - x1):
		print(c1 * x1)
	else:
		print((c1 + 1) * x1)


for _ in range(int(input())):
	(a, b, x) = map(int, input().split())
	n = a ** b
	(q, r) = divmod(n, x)
	m1 = int(x * q)
	d1 = abs(n - m1)
	m2 = int(x * (q + 1))
	d2 = abs(n - m2)
	if d1 <= d2:
		print(m1)
	else:
		print(m2)


def test_case():
	(a, b, x) = [int(y) for y in input().split()]
	if b < 0:
		print(1) if a == 1 else print(0)
	else:
		ab = pow(a, b)
		v = ab // x
		m1 = v * x
		m2 = (v + 1) * x
		if abs(ab - m1) < abs(ab - m2):
			print(m1)
		elif abs(ab - m1) > abs(ab - m2):
			print(m2)
		else:
			print(min(m1, m2))

def main():
	T = int(input())
	while T > 0:
		test_case()
		T -= 1
main()


import sys, math
n = int(input())
for _ in range(n):
	(a, b, x) = map(int, input().split())
	print(int(round(math.pow(a, b) / x)) * x)


import math
answers = []
for x in range(int(input())):
	(a, b, x) = input().split()
	(a, b, x) = [int(a), int(b), int(x)]
	total = a ** b
	modulo = a ** b % x
	if modulo <= 0.5 * x:
		answers.append(total - modulo)
	else:
		answers.append(total + x - modulo)
for x in answers:
	print(int(x))


t = int(input())
for _ in range(t):
	(a, b, x) = map(int, input().split())
	tmp = a ** b
	y = tmp // x
	if abs(tmp - y * x) <= abs(tmp - (y + 1) * x):
		print(int(y * x))
	else:
		print(int((y + 1) * x))


from math import *
testC = int(input())
for tc in range(testC):
	(a, b, x) = [int(i) for i in input().split(' ')]
	c = a ** b / x
	options = [floor(c), round(c), ceil(c)]
	options.sort(key=lambda t: abs(x * t - a ** b))
	answer = x * options[0]
	print(answer)


def check(a, b, x):
	d = int(pow(a, b))
	c = d % x
	f = int(d / x)
	f = int(f + 0)
	q = d - c
	w = d + x - c
	if d - q <= w - d:
		return q
	else:
		return w
T = int(input())
while T > 0:
	l = [int(x) for x in input().split()]
	q = check(l[0], l[1], l[2])
	print(q)
	T = T - 1


testcases = int(input())
ergebnisse = list()
for testcase in range(testcases):
	zahlen = list(map(int, input().split()))
	zahl = zahlen[0] ** zahlen[1]
	drunter = zahl // zahlen[2] * zahlen[2]
	drueber = (zahl // zahlen[2] + 1) * zahlen[2]
	if zahl - drunter <= drueber - zahl:
		ergebnisse.append(drunter)
	else:
		ergebnisse.append(drueber)
for ergebnis in ergebnisse:
	print(int(ergebnis))


t = int(input())
for i in range(t):
	(a, b, x) = map(int, input().split())
	num = a ** b
	lx = int(num // x)
	if lx * x + x - num < num - lx * x:
		print(lx * x + x)
	else:
		print(lx * x)


def solve(a, b, x):
	ab = a ** b
	t = int(ab // x)
	answers = [(t - 1) * x, t * x, (t + 1) * x]
	return min([(abs(ab - answer), answer) for answer in answers])[1]
t = int(input().strip())
for _ in range(t):
	(a, b, x) = (int(x) for x in input().strip().split())
	print(solve(a, b, x))


import math
t = int(input())
for test in range(t):
	(a, b, x) = input().split()
	a = int(a)
	b = int(b)
	x = int(x)
	y = int(math.pow(a, b)) + x // 2
	print(x * (y // x))


for _ in range(int(input())):
	(a, b, c) = map(int, input().split())
	d = a ** b + c / 2
	print('%.d' % (d - d % c))


import math

def tentative_k(a, b, x):
	return math.exp(b * math.log(a) - math.log(x))

def run():
	n = input()
	for i in range(int(n)):
		(a, b, x) = map(int, input().split())
		cl = round(tentative_k(a, b, x))
		print(cl * x)
run()


from math import exp, log

def solve(a, b, x):
	v = exp(b * log(a) - log(x))
	r = round(v)
	if abs(r - v) == abs(v - int(v)):
		r = int(v)
	print(int(r) * x)

def process():
	T = int(input())
	for i in range(T):
		(a, b, x) = map(int, input().split())
		solve(a, b, x)
process()


for _ in range(int(input())):
	(a, b, x) = map(int, input().strip().split())
	if a > 1 and b < 0:
		print(0)
	else:
		p = a ** b
		lb = int(x * (p // x))
		ub = int(x * (p // x + 1))
		if abs(p - lb) <= abs(p - ub):
			print(lb)
		else:
			print(ub)


for _ in range(int(input())):
	(a, b, x) = map(int, input().split())
	n = a ** b / x
	n = int(n) if n % 1 < 0.5 else int(n) + 1
	print(x * n)


T = int(input())
for index in range(T):
	L = input().split(' ')
	(a, b, x) = [int(z) for z in L]
	n = pow(a, b)
	if n <= 1 and x == 1:
		print(1)
	elif n <= 1 and x != 1:
		print(0)
	else:
		r = n % x
		nn = n - r + x
		print(nn if nn - n < r else n - r)


for _ in range(int(input())):
	(a, b, x) = map(int, input().strip().split())
	ans = pow(a, b) % x
	if ans > x - ans:
		print(pow(a, b) + x - ans)
	else:
		print(int(pow(a, b) - ans))


t = input()
t = int(t)
while t > 0:
	inp = input()
	arr = inp.split(' ')
	a = int(arr[0])
	b = int(arr[1])
	x = int(arr[2])
	num = pow(a, b)
	rem = num % x
	rem1 = x - rem
	if rem <= rem1:
		ans = num - rem
	else:
		ans = num + rem1
	ans = int(ans)
	print(ans)
	t -= 1


for t in range(int(input())):
	(a, b, x) = [int(x) for x in input().split()]
	ab = a ** b
	c = ab - ab % x
	if ab % x <= x / 2:
		print(int(c))
	else:
		print(int(c + x))


for _ in range(int(input())):
	(a, b, x) = [int(i) for i in input().split()]
	power = a ** b
	opt1 = power // x
	opt2 = opt1 + 1
	if abs(opt1 * x - power) <= abs(opt2 * x - power):
		print(int(opt1 * x))
	else:
		print(int(opt2 * x))


for i in range(int(input())):
	args = input().split(' ')
	a = int(args[0])
	b = int(args[1])
	x = int(args[2])
	power = pow(a, b)
	coef = round(power / x)
	print(coef * x)


t = int(input())
for i in range(t):
	(a, b, x) = map(int, input().strip().split())
	n = int(a ** b // x)
	if abs(a ** b - n * x) < abs(a ** b - (n + 1) * x):
		y = n * x
	else:
		y = (n + 1) * x
	print(y)


count = int(input())
for i in range(count):
	(a, b, x) = (int(num) for num in input().split())
	y = a ** b
	result = int(y + x / 2)
	result -= result % x
	if x % 2 == 0 and result - y == x // 2:
		result -= x
	print(result)