2017.11.24
#include<iostream>
#include<cstring>
using namespace std;
int main(){
int n, m;
bool flag;
int num, sum, old_num,count;
int a[10];
cin >> n;
while(n--){
count = 0;
cin >> m;
old_num = m;
num = m % 10;
sum = 1;
int i = 0;
memset(a, 0, sizeof(a));
flag = false;
while(m--){
sum *= num;
sum = sum % 10;
if(sum == 0){
cout << sum << endl;
flag = true;
break;
}
count++;
for(int j = 0; j <= i; j++){
if(a[j] == sum){
count--;
num = old_num % count;
if(num == 0)
cout << a[count - 1] <<endl;
else
cout << a[num - 1] <<endl;
flag = true;
break;
}
}
a[i++] = sum;
if(flag)
break;
}
if(flag)
continue;
cout << sum << endl;
}
return 0;
}