import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
public class Main {
public static void main(String[] args) throws IOException {
// int[] table = new int[101];
// for (int i=1;i<=100;i++) {
// table[i] = (table[i-1] + digit(i)) % 10;
// }
int[] table = new int[]{0 ,1 ,5 ,2 ,8 ,3 ,9 ,2 ,8 ,7 ,7 ,8 ,4 ,7 ,3 ,8 ,4 ,1 ,5 ,4 ,4 ,5 ,9 ,6,2 ,7 ,3 ,6 ,2 ,1 ,1 ,2 ,8 ,1 ,7 ,2 ,8 ,5 ,9 ,8 ,8 ,9 ,3 ,0 ,6 ,1 ,7 ,0 ,6 ,5 ,5 ,6 ,2 ,5 ,1 ,6 ,2 ,9 ,3 ,2 ,2 ,3 ,7 ,4 ,0 ,5 ,1 ,4 ,0 ,9 ,9 ,0 ,6 ,9 ,5 ,0 ,6 ,3 ,7 ,6 ,6 ,7 ,1 ,8 ,4 ,9 ,5 ,8 ,4 ,3 ,3 ,4,0 ,3 ,9 ,4 ,0 ,7 ,1 ,0};
BufferedReader in = new BufferedReader(new FileReader("cifra.in"));
PrintWriter out = new PrintWriter("cifra.out");
int t = Integer.parseInt(in.readLine());
for (int tc = 1; tc <= t; tc++) {
String s = in.readLine();
int n = 0;
int count = 1;
for (int i=s.length()-1;i>=0 && i>=s.length()-2;i--) {
n = n + (s.charAt(i)-'0')* count;
count*=10;
}
out.println(table[n]);
}
in.close();
out.close();
}
private static int digit(int n) {
int[][] matrix = new int[][] {{0,0,0,0},
{1,1,1,1},
{6,2,4,8},
{1,3,9,7},
{6,4,6,4},
{5,5,5,5},
{6,6,6,6},
{1,7,9,3},
{6,8,4,2},
{1,9,1,9}};
return matrix[n%10][n%4];
}
}