//2023-06-17 Baekjoon 13458 시험 감독
/*
import java.util.*;
public class Main {
public static void main(String[] args) {
int n,b,c;
long min = 0;
int A[];
Scanner scan = new Scanner(System.in);
n = scan.nextInt();
A = new int[n];
for(int i= 0; i <n; i++) {
A[i] = scan.nextInt();
}
b = scan.nextInt();
c = scan.nextInt();
for(int i =0; i <n; i++) {
if(A[i] <= b) {
min ++;
}
else {
min += 1+(A[i]-b)/c + ((A[i]-b)%c == 0 ? 0 : 1);
}
}
System.out.println(min);
}
}
*/
// 2023-06-17 Baekjoon 5543 상근날드
/*
import java.util.*;
public class Main {
public static void main(String[] args) {
int burger[] = new int[3];
int drink[] = new int[2];
Scanner scan = new Scanner(System.in);
burger[0] = scan.nextInt();
burger[1] = scan.nextInt();
burger[2] = scan.nextInt();
drink[0] = scan.nextInt();
drink[1] = scan.nextInt();
System.out.println((burger[0] < burger[1] ? (burger[0] < burger[2] ? burger[0] : burger[2]) : (burger[1] < burger[2] ? burger[1] : burger[2])) + (drink[0] < drink[1] ? drink[0] : drink[1]) - 50);
}
}
*/
/*
// 2023-06-17 Baekjoon 11382 꼬마정민
import java.util.*;
public class Main {
public static void main(String[] args) {
long a,b,c;
Scanner scan = new Scanner(System.in);
a = scan.nextLong();
b = scan.nextLong();
c = scan.nextLong();
System.out.println(a+b+c);
}
}
*/
// 2023-06-17 Baekjoon 1924 2007년
/*
import java.util.*;
public class Main {
public static void main(String[] args) {
int x,y;
Scanner scan = new Scanner(System.in);
x = scan.nextInt();
y = scan.nextInt();
System.out.println(((x == 1 ? 0 : (x == 2 ? 31 : (x == 3 ? 31+28 : (x == 4 ? 31+28+31 : (x == 5 ? 31+28+31+30 : (x == 6 ? 31+28+31+30+31 : (x == 7 ? 31+28+31+30+31+30 : (x == 8 ? 31+28+31+30+31+30+31 : (x == 9 ? 31+28+31+30+31+30+31+31 : (x== 10 ? 31+28+31+30+31+30+31+31+30 : (x == 11 ? 31+28+31+30+31+30+31+31+30+31 : 31+28+31+30+31+30+31+31+30+31+30))))))))))) + y)%7 == 1 ? "MON" : (((x == 1 ? 0 : (x == 2 ? 31 : (x == 3 ? 31+28 : (x == 4 ? 31+28+31 : (x == 5 ? 31+28+31+30 : (x == 6 ? 31+28+31+30+31 : (x == 7 ? 31+28+31+30+31+30 : (x == 8 ? 31+28+31+30+31+30+31 : (x == 9 ? 31+28+31+30+31+30+31+31 : (x== 10 ? 31+28+31+30+31+30+31+31+30 : (x == 11 ? 31+28+31+30+31+30+31+31+30+31 : 31+28+31+30+31+30+31+31+30+31+30))))))))))) + y)%7 == 2 ? "TUE" : (((x == 1 ? 0 : (x == 2 ? 31 : (x == 3 ? 31+28 : (x == 4 ? 31+28+31 : (x == 5 ? 31+28+31+30 : (x == 6 ? 31+28+31+30+31 : (x == 7 ? 31+28+31+30+31+30 : (x == 8 ? 31+28+31+30+31+30+31 : (x == 9 ? 31+28+31+30+31+30+31+31 : (x== 10 ? 31+28+31+30+31+30+31+31+30 : (x == 11 ? 31+28+31+30+31+30+31+31+30+31 : 31+28+31+30+31+30+31+31+30+31+30))))))))))) + y)%7 == 3 ? "WED" : (((x == 1 ? 0 : (x == 2 ? 31 : (x == 3 ? 31+28 : (x == 4 ? 31+28+31 : (x == 5 ? 31+28+31+30 : (x == 6 ? 31+28+31+30+31 : (x == 7 ? 31+28+31+30+31+30 : (x == 8 ? 31+28+31+30+31+30+31 : (x == 9 ? 31+28+31+30+31+30+31+31 : (x== 10 ? 31+28+31+30+31+30+31+31+30 : (x == 11 ? 31+28+31+30+31+30+31+31+30+31 : 31+28+31+30+31+30+31+31+30+31+30))))))))))) + y)%7 == 4 ? "THU" : (((x == 1 ? 0 : (x == 2 ? 31 : (x == 3 ? 31+28 : (x == 4 ? 31+28+31 : (x == 5 ? 31+28+31+30 : (x == 6 ? 31+28+31+30+31 : (x == 7 ? 31+28+31+30+31+30 : (x == 8 ? 31+28+31+30+31+30+31 : (x == 9 ? 31+28+31+30+31+30+31+31 : (x== 10 ? 31+28+31+30+31+30+31+31+30 : (x == 11 ? 31+28+31+30+31+30+31+31+30+31 : 31+28+31+30+31+30+31+31+30+31+30))))))))))) + y)%7 == 5 ? "FRI" : (((x == 1 ? 0 : (x == 2 ? 31 : (x == 3 ? 31+28 : (x == 4 ? 31+28+31 : (x == 5 ? 31+28+31+30 : (x == 6 ? 31+28+31+30+31 : (x == 7 ? 31+28+31+30+31+30 : (x == 8 ? 31+28+31+30+31+30+31 : (x == 9 ? 31+28+31+30+31+30+31+31 : (x== 10 ? 31+28+31+30+31+30+31+31+30 : (x == 11 ? 31+28+31+30+31+30+31+31+30+31 : 31+28+31+30+31+30+31+31+30+31+30))))))))))) + y)%7 == 6 ? "SAT" : "SUN"))))));
}
}
*/
/*
// 2023-06-17 Baekjoon 2748 피보나치 수 2
import java.util.*;
public class Main {
static long memo[] = new long [91];
static long Fibonacci(int n) {
if(n == 0)
return 0;
else if(n == 1)
return 1;
else {
if(memo[n] == 0)
memo[n] = Fibonacci(n-1) + Fibonacci(n-2);
return memo[n];
}
}
public static void main(String[] args) {
int n;
Scanner scan = new Scanner(System.in);
n = scan.nextInt();
System.out.println(Fibonacci(n));
}
}
*/
/*
// 2023-06-17 Baekjoon 2475 검증수
import java.util.*;
import java.math.*;
public class Main {
public static void main(String[] args) {
int n,nov = 0;
Scanner scan = new Scanner(System.in);
for(int i= 0; i <5; i++) {
n = scan.nextInt();
nov += Math.pow(n, 2);
}
System.out.println(nov%10);
}
}
*/
// 2023-06-17 Baekjoon 1009 분산처리
import java.util.*;
public class Main {
public static void main(String[] args) {
int t;
int a,b;
Vector<Integer> cycle = new Vector<Integer>();
Scanner scan = new Scanner(System.in);
t = scan.nextInt();
while(t > 0) {
cycle = new Vector<Integer>();
a = scan.nextInt()%10;
b = scan.nextInt();
cycle.add((int)(Math.pow(a, 1) % 10));
for(int i = 2 ;; i++) {
if((int)(Math.pow(a, i) % 10) == cycle.get(0)) {
break;
}
else {
cycle.add((int)(Math.pow(a, i) % 10));
}
}
System.out.println(cycle.get((b-1)%cycle.size()) == 0 ? 10 : cycle.get((b-1)%cycle.size()));
t--;
}
}
}