problem-1-39

One day Patrik met a magician.He asked magician for his magic pencil.So magician decided to give him magic pencil only if he correctely answers 
to his question. 
Magician gave him 3 numbers a, b and c and asked him to find the multiple of c which is closest to a^b.Help patrik to solve this problem and 
win magic pencil.

Note:If more than one answer is possible , print 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 c respectively.

Output Format: 

For each test case , print the multiple of c which is closest to a^b

Constraints: 

1 ≤ T ≤ 10^5 

1 ≤ a ≤ 10^9 

-10^9 ≤ b ≤ 10^9

1 ≤ c ≤ 10^9 

0 < a^b ≤ 10^9 
Register for IndiaHacks

SAMPLE INPUT
3
125 1 4
460 1 7
8 -2 2

SAMPLE OUTPUT
124
462
0

Register for IndiaHacks

Source: hackerearth | Difficulty: EASY


Solutions:
T = int(input())
for i in range(T):
	inp = input().split(" ")
	a = int(inp[0])
	b = int(inp[1])
	c = int(inp[2])
	p = a**b
	t = int((p)/c)
	r = p % c
	#print(t,r)
	if(r < c-r):
		print((int(p - r)))
	else:
		print((int(p+(c-r))))


t= int(input())
for i in range(t):
	a,b,c = tuple(map(int,input().split()))
	m=a ** b
	if(m<1):
		print((0))
	else:
		m = a ** b
		r = m % c
		p1 = m - r 
		p2 = p1 + c
		if((p2 - m) > (m - p1)):
			print((int(p1)))
		else:
			print((int(p2)))


'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
#print 'Hello World!'
t=eval(input())
while t>0:
	t-=1
	a,b,c=list(map(int,input().split()))
	x=int(pow(a,b))
	d=x/c
	e=x%c
	f=(d+1)*c
	if abs(x-f)>abs(x-d*c):
		print(d*c)
	else:
		print(((d+1)*c))


t= int(input())
for i in range(t):
	a,b,c = tuple(map(int,input().split()))
	m=a ** b
	if(m<1):
		print((0))
	else:
		m = a ** b
		r = m % c
		p1 = m - r 
		p2 = p1 + c
		if((p2 - m) > (m - p1)):
			print((int(p1)))
		else:
			print((int(p2)))


testCases = int(input())

def closest(a,b,c):
	a,b,c = int(a),int(b),int(c)
	xo = int(pow(a,b))
	num1 = xo + c - (xo % c)
	num2 = xo - (xo % c)
	if(xo < 0):
		return 0
	if(c - (xo % c) > (xo % c)):
		return num2
	elif(c - (xo % c) < (xo % c)):
		return num1
	else:
		return num2

for j in range(1, testCases + 1):
	a,b,c = input().split(" ")
	print(closest(a,b,c))
	


'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
#print 'Hello World!'
t=eval(input())
while t>0:
	t-=1
	a,b,c=list(map(int,input().split()))
	x=int(pow(a,b))
	d=x/c
	e=x%c
	f=(d+1)*c
	if abs(x-f)>abs(x-d*c):
		print(d*c)
	else:
		print(((d+1)*c))


testCases = int(input())

def closest(a,b,c):
	a,b,c = int(a),int(b),int(c)
	power = int(pow(a,b))
	num1 = power + c - (power % c)
	num2 = power - (power % c)

	if(c - (power % c) > (power % c)):
		return num2
	elif(c - (power % c) < (power % c)):
		return num1
	else:
		return num2

for j in range(1, testCases + 1):
	a,b,c = input().split(" ")
	print(closest(a,b,c))
	


testCases = int(input())

def closest(a,b,c):
	a,b,c = int(a),int(b),int(c)
	xo = int(pow(a,b))
	num1 = xo + c - (xo % c)
	num2 = xo - (xo % c)

	if(c - (xo % c) > (xo % c)):
		return num2
	elif(c - (xo % c) < (xo % c)):
		return num1
	else:
		return num2

for j in range(1, testCases + 1):
	a,b,c = input().split(" ")
	print(closest(a,b,c))
	


import math

t=eval(input())
for _ in range(t):
	a,b,c=list(map(int,input().split()))
	tot = a**b
	t1 = int(math.floor(tot/float(c))*c)
	t2 = int(math.ceil(tot/float(c))*c)
	#print t1,t2
	if abs(tot-t1)>abs(tot-t2):
		print(t2)
	else:
		print(t1)