#include <iostream>
using namespace std;
int main()
{
int d,c,n,m,b;
int a[10][4]= {{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}};
cin>>d;
while( (cin>>c)&&(d--)&&(d>=0))
{m=c;
n=c;
if((m<10)&&(m>=0)&&(n>=0)&&(n<4))
{
b=a[m][n];
}
else if((m<10)&&(m>=0)&&(n>=4))
{
while(n>=4)
{
n=n%4;
}
b=a[m][n];
}
else if((m>=10)&&(n>=0)&&(n<4))
{
while(m>=10)
{
m=m%10;
}
b=a[m][n];
}
else
{
while((m>=10))
{
m=m%10;
}
while(n>=4)
{
n=n%4;
}
b=a[m][n];
}
cout<<b<<endl;
}
return 0;
}