#include<stdio.h>
#include<string.h>
int a[10];
int main()
{
int T,n,i,k,temp,b,t;
scanf("%d",&T);
while(T--)
{
memset(a,0,sizeof(a));
scanf("%d",&n);
t = n;
n %= 10;
if(n==1||n==0)
{
printf("%d
",n);
continue ;
}
temp = 0;
b = n;
i = 0;
while(temp!=n)
{
a[i++] = b;
temp = (b*n)%10;
b = temp;
}
k = t%i==0?i-1:t%i-1;
printf("%d
",a[k]);
}
return 0;
}