Read problems statements in Mandarin chinese, Russian and Vietnamese as well. 

As New Year is approaching, Salik is studying functions in order to sharpen his math skills. Instead of regular functions, he is studying a strange function F which operates on non-negative integers. The value of F(A) is obtained by the following process:

Compute the sum of digits of A; let's denote this sum by S.
If S is a single-digit integer, then F(A) = S.
Otherwise, F(A) = F(S).

It's guaranteed that this process correctly defines the function F.

Since this problem was pretty straightforward, he invented a new problem: Given two integers A and N, compute F(A^{N}). Can you help him solve this task?

------ Input ------ 

The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
The first and only line of each test case contains two space-separated integers A and N.

------ Output ------ 

For each test case, print a single line containing one integer — the value of F(A^{N}).

------ Constraints ------ 

$1 ≤ T ≤ 10^{5}$
$1 ≤ A, N ≤ 10^{18}$

------ Subtasks ------ 

Subtask #1 (10 points):

$1 ≤ N ≤ 3$
$1 ≤ A ≤ 10^{5}$

Subtask #2 (20 points):

$1 ≤ N ≤ 100$
$1 ≤ A ≤ 10^{5}$

Subtask #3 (70 points): original constraints

----- Sample Input 1 ------ 
3
5 2
7 2
127 1
----- Sample Output 1 ------ 
7
4
1
----- explanation 1 ------ 
Example case 1: F(5 · 5) = F(25) = F(2+5) = F(7) = 7 Example case 2: F(7 · 7) = F(49) = F(4+9) = F(13) = F(1+3) = F(4) = 4
Example case 3: F(127) = F(1+2+7) = F(10) = F(1+0) = F(1) = 1

Difficulty: MEDIUM_HARD

Solutions:
def F(num):
	digit_sum = 0
	while num:
		digit_sum += num % 10
		num = num // 10
	if digit_sum < 10:
		return digit_sum
	return F(digit_sum)
T = int(input())
for var in range(0, T):
	temp = input().split(' ')
	A = int(temp[0])
	N = int(temp[1])
	if pow(A, N, 9):
		print(pow(A, N, 9))
	else:
		print(9)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	ans = pow(n, m, 9)
	print(ans if ans else 9)


t = int(input())
for i in range(t):
	(a, n) = map(int, input().split())
	c = pow(a, n, 9)
	print(c if c else 9)


T = int(input())
for K in range(T):
	(a, n) = map(int, input().split())
	num = pow(a, n, 9)
	if num:
		print(num)
	else:
		print(9)


def powr(a, n):
	ans = 1
	while n:
		if n & 1:
			ans *= a
			ans = ans % 9
		a *= a
		a = a % 9
		n //= 2
	return ans % 9
t = int(input())
for _ in range(t):
	(a, n) = [int(i) for i in input().strip().split()]
	val = pow(a, n, 9)
	if not val:
		val = 9
	print(val)


t = int(input())
while t:
	t -= 1
	(n, a) = map(int, input().split())
	val = pow(n, a, 9)
	if val == 0:
		print(9)
	else:
		print(val)


for _ in range(int(input())):
	(a, n) = map(int, input().split())
	num = pow(a, n, 9)
	if num != 0:
		print(num)
	else:
		print(9)


t = int(input())
while t:
	t -= 1
	(a, b) = map(int, input().split())
	if pow(a, b, 9) != 0:
		print(pow(a, b, 9))
	else:
		print(9)


T = int(input())
for i in range(T):
	(base, power) = map(int, input().split())
	ans = pow(base, power, 9)
	if ans == 0:
		print(9)
	else:
		print(ans)


for _ in range(int(input())):
	(A, N) = map(int, input().split())
	res = pow(A, N, 9)
	if res == 0:
		print(9)
	else:
		print(res)


test = int(input())
for _ in range(test):
	(base, power) = map(int, input().split())
	ans = pow(base, power, 9)
	if ans == 0:
		print(9)
	else:
		print(ans)


def sumnum(num):
	c = 0
	while True:
		c += num % 10
		num = num // 10
		if num == 0:
			if c < 10:
				break
			else:
				num = c
				c = 0
	return c
l2 = [1, 2, 4, 8, 7, 5]
l5 = [1, 5, 7, 8, 4, 2]
for _ in range(int(input())):
	(b, p) = map(int, input().split())
	b = sumnum(b)
	if b == 1:
		print(1)
	elif b == 9:
		if p == 0:
			print(1)
		else:
			print(9)
	elif b == 3:
		if p == 0:
			print(1)
		elif p == 1:
			print(3)
		else:
			print(9)
	elif b == 6:
		if p == 0:
			print(1)
		elif p == 1:
			print(6)
		else:
			print(9)
	elif b == 8:
		r = p % 2
		if r == 0:
			print(1)
		else:
			print(8)
	elif b == 4:
		r = p % 3
		if r == 0:
			print(1)
		elif r == 1:
			print(4)
		else:
			print(7)
	elif b == 7:
		r = p % 3
		if r == 0:
			print(1)
		elif r == 1:
			print(7)
		else:
			print(4)
	elif b == 2:
		r = p % 6
		print(l2[r])
	elif b == 5:
		r = p % 6
		print(l5[r])


def sum_dig(n):
	if n < 10:
		return n
	res = 0
	while n:
		res += n % 10
		n //= 10
	return sum_dig(res)
for _ in range(int(input())):
	(a, n) = map(int, input().split())
	(x, y) = (sum_dig(a), n % 6)
	if n == 1:
		print(x)
	elif x == 3 or x == 6 or x == 9:
		print(9)
	else:
		print(sum_dig(x ** y))


for i in range(int(input())):
	(N, M) = map(int, input().split())
	s = pow(N, M, 9)
	print(9) if s == 0 else print(s)


for i in range(int(input())):
	(n, m) = map(int, input().split())
	s = pow(n, m, 9)
	print(9) if s == 0 else print(s)


from collections import defaultdict as dd
from collections import Counter as ccd
from itertools import permutations as pp
from itertools import combinations as cc
from random import randint as rd
from bisect import bisect_left as bl
from bisect import bisect_right as br
import heapq as hq
import math
mod = pow(10, 9) + 7

def inp(flag=0):
	if flag == 0:
		return list(map(int, input().strip().split(' ')))
	else:
		return int(input())
t = inp(1)
while t:
	t -= 1
	(a, n) = inp()
	x = pow(a, n, 9)
	if x == 0:
		print(9)
	else:
		print(x)


from math import ceil
for _ in range(int(input())):
	(a, n) = map(int, input().split())
	a = a % 9
	if a == 1:
		print(1)
	elif a == 2 or a == 5:
		n = n % 6
		ans = a ** n
		an = ans + (1 - ceil(ans / 9)) * 9
		print(an)
	elif a == 3 or a == 6:
		if n == 1:
			print(a)
		else:
			print(9)
	elif a == 4 or a == 7:
		n = n % 3
		ans = a ** n
		an = ans + (1 - ceil(ans / 9)) * 9
		print(an)
	elif a == 8:
		n = n % 2
		ans = a ** n
		an = ans + (1 - ceil(ans / 9)) * 9
		print(an)
	else:
		print(9)


t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	x = pow(a, b, 9)
	if x == 0:
		print(9)
	else:
		print(x)


t = int(input())
for i in range(0, t):
	(n, k) = list(map(int, input().split()))
	ans = pow(n, k, 9)
	if ans == 0:
		print(9)
	else:
		print(ans)


t = int(input())
for T in range(t):
	(a, n) = [int(x) for x in input().split()]
	ans = pow(a, n, 9)
	if a != 0 and ans == 0:
		ans = 9
	print(ans)


for _ in range(int(input())):
	(A, N) = [int(x) for x in input().split()]
	ans = pow(A, N, 9)
	if ans == 0:
		print('9')
	else:
		print(ans)


import math
T = int(input())
for i in range(T):
	(A, N) = list(map(int, input().strip().split()))
	ans = pow(A, N, 9)
	if ans == 0:
		print('9')
	else:
		print(str(ans))


import math
t = int(input())
for i in range(t):
	(a, n) = map(int, input().split())
	z = pow(a, n, 9)
	if z == 0:
		print(9)
	else:
		print(z)


def sod(n):
	r = 0
	while n:
		(r, n) = (r + n % 10, n // 10)
	while r > 9:
		r = sod(r)
	return r
ans = {0: [0], 1: [1], 2: [2, 4, 8, 7, 5, 1], 3: [9], 4: [4, 7, 1], 5: [5, 7, 8, 4, 2, 1], 6: [9], 7: [7, 4, 1], 8: [8, 1], 9: [9]}
for _ in range(int(input())):
	(n, a) = map(int, input().split())
	k = sod(n)
	if a == 1:
		print(k)
	else:
		print(ans[k][a % len(ans[k]) - 1])


t = int(input())
while t:
	t -= 1
	(a, n) = map(int, input().split(' '))
	ans = pow(a, n, 9)
	if ans == 0:
		ans = 9
	print(ans)


t = int(input())
for _ in range(t):
	(a, n) = map(int, input().split())
	ans = pow(a, n, 9)
	if ans == 0:
		print(9)
	else:
		print(ans)


import sys

def ds(n):
	if n % 9:
		return n % 9
	else:
		return 9
t = int(input())
i = 0
while t > i:
	(a, n) = map(int, input().split())
	a = ds(a)
	ans = pow(a, n, 9)
	print(ds(ans))
	i += 1


for test in range(int(input())):
	(a, n) = (int(j) for j in input().split())
	x = pow(a, n, 9)
	if x == 0:
		print(9)
	else:
		print(x)


for i in range(int(input())):
	(a, n) = map(int, input().split())
	d = pow(a, n, 9)
	if d == 0:
		print('9')
	else:
		print(d)


for test in range(int(input())):
	arriN = [0] * 2
	arriN = [int(x) for x in input().split()]
	a = arriN[0]
	n = arriN[1]
	k = pow(a, n, 9)
	if k == 0:
		print(9)
	else:
		print(k)


def soln(a, n):
	res = 1
	while n > 0:
		if n % 2 == 1:
			res = res * F(a)
			res = F(res)
		a = F(F(a) * F(a))
		n /= 2
	return res
for test in range(int(input())):
	arriN = [0] * 2
	arriN = [int(x) for x in input().split()]
	a = arriN[0]
	n = arriN[1]
	k = pow(a, n, 9)
	if k == 0:
		print(9)
	else:
		print(k)


def digi(a):
	if a % 9 == 0:
		return 9
	return a % 9

def exp(a, n):
	if n == 1:
		return digi(a)
	if n == 0:
		return 1
	elif n % 2 == 1:
		return exp(exp(a, 1) * exp(a, n - 1), 1)
	else:
		temp = exp(a, n // 2)
		return exp(temp * temp, 1)

def key(a, n):
	if a == 1:
		return 1
	elif a == 2 or a == 5:
		return n % 6
	elif a == 3 or a == 6 or a == 9:
		if n == 1:
			return 1
		else:
			return 2
	elif a == 4 or a == 7:
		return n % 3
	elif a == 8:
		return n % 2
for _ in range(int(input())):
	(a, n) = [int(satish) for satish in input().split()]
	print(exp(digi(a), key(digi(a), n)))


import math

def fast_power(base, power):
	result = 1
	while power > 0:
		if power % 2 == 1:
			result = result * base
		power = power // 2
		base = base * base
	return result
t = int(input())
while t:
	(a, b) = map(int, input().split())
	k = pow(a, b, 9)
	if k == 0:
		print(9)
	else:
		print(k)
	t = t - 1


r = ['1', '124875', '9', '147', '157842', '9', '174', '18']

def solve(a, n):
	a %= 9
	if a == 0:
		return 9
	if n == 1:
		return a
	l = r[a - 1]
	return l[n % len(l)]
t = int(input())
for _ in range(t):
	print(solve(*map(int, input().split())))


ter = int(input())
for im in range(0, ter):
	(b, d) = map(int, input().split())
	e = pow(b, d, 9)
	if e == 0:
		n = 9
	else:
		n = e % 9
	print(n)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	result = pow(a, b, 9)
	print(9 if result == 0 else result)


for i in range(int(input())):
	(a, n) = map(int, input().split())
	if a % 9 == 0:
		print(9)
	elif a % 9 == 1:
		print(1)
	elif a % 9 == 2:
		if n % 6 == 0:
			print(1)
		elif n % 6 == 1:
			print(2)
		elif n % 6 == 2:
			print(4)
		elif n % 6 == 3:
			print(8)
		elif n % 6 == 4:
			print(7)
		else:
			print(5)
	elif a % 9 == 3:
		if n == 1:
			print(3)
		else:
			print(9)
	elif a % 9 == 4:
		if n % 3 == 0:
			print(1)
		elif n % 3 == 1:
			print(4)
		else:
			print(7)
	elif a % 9 == 5:
		if n % 6 == 0:
			print(1)
		elif n % 6 == 1:
			print(5)
		elif n % 6 == 2:
			print(7)
		elif n % 6 == 3:
			print(8)
		elif n % 6 == 4:
			print(4)
		else:
			print(2)
	elif a % 9 == 6:
		if n == 1:
			print(6)
		else:
			print(9)
	elif a % 9 == 7:
		if n % 3 == 0:
			print(1)
		elif n % 3 == 1:
			print(7)
		else:
			print(4)
	elif n % 2 == 0:
		print(1)
	else:
		print(8)


for i in range(int(input())):
	(n, m) = input().split()
	n = int(n)
	m = int(m)
	x = pow(n, m, 9)
	if x == 0:
		print(9)
	else:
		print(x)


list1 = [1]
list2 = [1, 2, 4, 8, 7, 5]
list3 = [9]
list4 = [1, 4, 7]
list5 = [1, 5, 7, 8, 4, 2]
list6 = [9]
list7 = [1, 7, 4]
list8 = [1, 8]
list9 = [9]
t = int(input())
for j in range(t):
	(a, n) = map(int, input().split(' ', 2)[:2])
	if n > 1:
		if a % 9 == 1:
			print(list1[0])
		elif a % 9 == 2:
			print(list2[n % 6])
		elif a % 9 == 3:
			print(list3[0])
		elif a % 9 == 4:
			print(list4[n % 3])
		elif a % 9 == 5:
			print(list5[n % 6])
		elif a % 9 == 6:
			print(list6[0])
		elif a % 9 == 7:
			print(list7[n % 3])
		elif a % 9 == 8:
			print(list8[n % 2])
		else:
			print(9)
	elif a % 9 == 1:
		print(list1[0])
	elif a % 9 == 2:
		print(list2[n % 6])
	elif a % 9 == 3:
		print(3)
	elif a % 9 == 4:
		print(list4[n % 3])
	elif a % 9 == 5:
		print(list5[n % 6])
	elif a % 9 == 6:
		print(6)
	elif a % 9 == 7:
		print(list7[n % 3])
	elif a % 9 == 8:
		print(list8[n % 2])
	else:
		print(9)


t = int(input())
while t > 0:
	t = t - 1
	(a, n) = input().split()
	(a, n) = (int(a), int(n))
	result = pow(a, n, 9)
	if result == 0:
		print('9')
	else:
		print(str(result))


for _ in range(int(input())):
	(a, n) = map(int, input().split())
	t = pow(a, n, 9)
	if not t:
		print('9')
	else:
		print(t)


t = int(input())
for i in range(t):
	(a, n) = map(int, input().strip().split(' '))
	j = pow(a, n, 9)
	if j == 0:
		print('9')
	else:
		print(j)


def getsum(x):
	c = 0
	while x != 0:
		c += x % 10
		x //= 10
	if c < 10:
		return c
	else:
		return getsum(c)
for t in range(int(input())):
	temp = list(map(int, input().split()))
	a = temp[0]
	n = temp[1]
	a = getsum(a)
	if a == 1:
		print(1)
	elif a == 2:
		l = [2, 4, 8, 7, 5, 1]
		n -= 1
		n %= 6
		print(l[n])
	elif a == 3:
		if n == 1:
			print(3)
		else:
			print(9)
	elif a == 4:
		l = [4, 7, 1]
		n -= 1
		n %= 3
		print(l[n])
	elif a == 5:
		l = [5, 7, 8, 4, 2, 1]
		n -= 1
		n %= 6
		print(l[n])
	elif a == 6:
		if n == 1:
			print(6)
		else:
			print(9)
	elif a == 7:
		l = [7, 4, 1]
		n -= 1
		n %= 3
		print(l[n])
	elif a == 8:
		l = [8, 1]
		n -= 1
		n %= 2
		print(l[n])
	elif a == 9:
		print(9)


r = [[1], [1, 2, 4, 8, 7, 5], [9], [1, 4, 7], [1, 5, 7, 8, 4, 2], [9], [1, 7, 4], [1, 8]]

def solve(a, n):
	a %= 9
	if a == 0:
		return 9
	if n == 1:
		return a
	l = r[a - 1]
	return l[n % len(l)]
t = int(input())
for _ in range(t):
	print(solve(*map(int, input().split())))


for _ in range(int(input())):
	str1 = input()
	(N, A) = str1.split(' ')
	sum1 = 0
	for i in N:
		sum1 += int(i)
	A = int(A)
	k = sum1 % 9
	ans = 0
	if k == 0:
		ans = 9
	elif k == 1:
		ans = 1
	elif k == 2:
		l = [1, 2, 4, 8, 7, 5, 1]
		ans = l[A % 6]
	elif k == 3 or k == 6:
		if A > 1:
			ans = 9
		else:
			ans = k
	elif k == 4:
		l = [1, 4, 7, 1]
		ans = l[A % 3]
	elif k == 5:
		l = [1, 5, 7, 8, 4, 2, 1]
		ans = l[A % 6]
	elif k == 7:
		l = [1, 7, 4, 1]
		ans = l[A % 3]
	else:
		l = [1, 8, 1]
		ans = l[A % 2]
	print(ans)


import math
for _ in range(int(input())):
	(N, A) = map(int, input().split(' '))
	k = N % 9
	ans = 0
	if k == 0:
		ans = 9
	elif k == 1:
		ans = 1
	elif k == 2:
		l = [1, 2, 4, 8, 7, 5, 1]
		ans = l[A % 6]
	elif k == 3 or k == 6:
		if A > 1:
			ans = 9
		else:
			ans = k
	elif k == 4:
		l = [1, 4, 7, 1]
		ans = l[A % 3]
	elif k == 5:
		l = [1, 5, 7, 8, 4, 2, 1]
		ans = l[A % 6]
	elif k == 7:
		l = [1, 7, 4, 1]
		ans = l[A % 3]
	else:
		l = [1, 8, 1]
		ans = l[A % 2]
	print(ans)


for i in range(int(input())):
	(a, b) = map(int, input().split())
	val = pow(a, b, 9)
	if val == 0:
		print(9)
	else:
		print(val)


def digSum(n):
	if n == 0:
		return 0
	if n % 9 == 0:
		return 9
	else:
		return n % 9
for _ in range(int(input())):
	(a, n) = map(int, input().split())
	a = digSum(a)
	z = n % 6
	if a == 3 or a == 6 or a == 9:
		if n == 1:
			print(a)
		else:
			print('9')
	else:
		ans = a ** z
		ans = digSum(ans)
		print(ans)


from math import sqrt
import sys
t = int(input())
while t:
	s = 0
	y = 0
	(a, n) = map(int, sys.stdin.readline().split())
	x = pow(a, n, 9)
	if x == 0:
		s = 9
	else:
		while x or s > 9:
			if x == 0:
				x = s
				s = 0
			s = s + x % 10
			x = x // 10
	print(s)
	t = t - 1


def digSum(x):
	if x == 0:
		return 0
	if x % 9 == 0:
		return 9
	else:
		return x % 9
for _ in range(int(input())):
	(a, n) = map(int, input().split())
	q = digSum(a)
	if q == 1:
		print(1)
	elif q == 2:
		if n % 6 == 0:
			print(1)
		elif n % 6 == 1:
			print(2)
		elif n % 6 == 2:
			print(4)
		elif n % 6 == 3:
			print(8)
		elif n % 6 == 4:
			print(7)
		elif n % 6 == 5:
			print(5)
	elif q == 3:
		if n == 1:
			print(3)
		else:
			print(9)
	elif q == 4:
		if n % 3 == 0:
			print(1)
		elif n % 3 == 1:
			print(4)
		elif n % 3 == 2:
			print(7)
	elif q == 5:
		if n % 6 == 0:
			print(1)
		elif n % 6 == 1:
			print(5)
		elif n % 6 == 2:
			print(7)
		elif n % 6 == 3:
			print(8)
		elif n % 6 == 4:
			print(4)
		elif n % 6 == 5:
			print(2)
	elif q == 6:
		if n == 1:
			print(6)
		else:
			print(9)
	elif q == 7:
		if n % 3 == 1:
			print(7)
		elif n % 3 == 2:
			print(4)
		else:
			print(1)
	elif q == 8:
		if n % 2 == 0:
			print(1)
		else:
			print(8)
	elif q == 9:
		print(9)


t = int(input())
for I in range(t):
	(a, n) = [int(i) for i in input().split()]
	g = pow(a, n, 9)
	if g == 0:
		print(9)
	else:
		print(g)


def getsum(n):
	if n >= 0 and n <= 9:
		return n
	else:
		s = 0
		while n:
			s += n % 10
			n //= 10
	return getsum(s)
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	y = k % 6
	s = getsum(n)
	if s == 9 and k > 0:
		print('9')
	elif k == 1 and (s == 3 or s == 6):
		print(s)
	elif k > 1 and (s == 3 or s == 6):
		print('9')
	else:
		print(getsum(s ** y))


t = int(input())
for i in range(t):
	s = input().split()
	s = list(s)
	a = int(s[0])
	n = int(s[1])
	val = pow(a, n, 9)
	val = val % 9
	if val == 0:
		print(9)
	else:
		print(val)


def digisum(n):
	r = 0
	while n:
		(r, n) = (r + n % 10, n // 10)
	return r
t = int(input())
for x in range(t):
	(a, n) = input().split()
	n = int(n)
	a = int(a)
	digit = digisum(a)
	while digit >= 10:
		digit = digisum(digit)
	if digit == 1:
		print(1)
	elif digit == 2:
		rem = n % 6
		d2 = [1, 2, 4, 8, 7, 5]
		print(d2[rem])
	elif digit == 3:
		if n == 1:
			print(3)
		else:
			print(9)
	elif digit == 4:
		rem = n % 3
		d4 = [1, 4, 7]
		print(d4[rem])
	elif digit == 5:
		rem = n % 6
		d5 = [1, 5, 7, 8, 4, 2]
		print(d5[rem])
	elif digit == 6:
		if n == 1:
			print(6)
		else:
			print(9)
	elif digit == 7:
		rem = n % 3
		d7 = [1, 7, 4]
		print(d7[rem])
	elif digit == 8:
		rem = n % 2
		d8 = [1, 8]
		print(d8[rem])
	elif digit == 9:
		print(9)


for _ in range(int(input())):
	(a, n) = [int(x) for x in input().split()]
	ans = pow(a, n, 9)
	print((ans, 9)[ans == 0])


for _ in range(int(input())):
	(numb, exp) = map(int, input().split())
	x = pow(numb, exp, 9)
	print(x if x else 9)


t = int(input())
for _ in range(t):
	(a, n) = map(int, input().split())
	b = [[0], [1], [2, 4, 8, 7, 5, 1], [9], [4, 7, 1], [5, 7, 8, 4, 2, 1], [9], [7, 4, 1], [8, 1]]
	if a % 9 == 0:
		print(9)
	else:
		k = a % 9
		if n == 1 and k == 3:
			print(3)
		elif n == 1 and k == 6:
			print(6)
		else:
			l = len(b[a % 9])
			print(b[a % 9][n % l - 1])


def getsum(n):
	return n % 9 + 9 * (n % 9 == 0)
T = 1
T = int(input().strip())
for _ in range(T):
	(n, k) = map(int, input().split())
	p = getsum(n)
	print(getsum(pow(p, k, 9)))