T =int(input())for _ inrange(T):
x, p =map(str,input().split()); x =int(x)%10if x ==0or x ==1or x ==5or x ==6:print(x)elif x ==4:print(4ifint(p[-1])%2!=0else6)elif x ==9:print(9ifint(p[-1])%2!=0else1)else:
p =int(p[-2:])%4if x ==2:print([6,2,4,8][p])elif x ==3:print([1,3,9,7][p])elif x ==7:print([1,7,9,3][p])else:print([6,8,4,2][p])
T =int(input())for _ inrange(T):
N =int(input())
A =[0]+sorted(list(map(int,input().split())))# 需要单独处理 N = 2 的情况,否则下面会越界if N ==2:print(int(A[1]!= A[2]))continue# 保证首尾均有连续的数字
ans =(A[1]!= A[2])+(A[N-1]!= A[N])if ans ==2and(A[2]== A[3]or A[N -2]== A[N -1]):# 首位均无连续的数字 除第i位和最后一位首尾有连续
ans =1# 看情况将首改尾交换(或尾改首)就保证首尾均有连续的数字print(ans)
T =int(input())for _ inrange(T):
N =int(input())
A =[0]+sorted(list(map(int,input().split())))# 需要单独处理 N = 2 的情况,否则下面会越界if N ==2:print(int(A[1]!= A[2]))continue# 保证首尾均有连续的数字
ans =(A[1]!= A[2])+(A[N-1]!= A[N])if ans ==2and(A[2]== A[3]or A[N -2]== A[N -1]):# 首位均无连续的数字 除第i位和最后一位首尾有连续
ans =1# 看情况将首改尾交换(或尾改首)就保证首尾均有连续的数字print(ans)