#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
ofstream g("cifra.out");
/*int sum(int x,int p) // GENERAREA numerelor pana la 100, ultima cifra, dupa 100 se repeta //
{
if(x==1||p==0)
return 1;
else
if(x==2)
{
if(p%4==0)
return 6;
else
if(p%4==1)
return 2;
else
if(p%4==2)
return 4;
else
if(p%4==3)
return 8;
}
else
if(x==3)
{
if(p%4==0)
return 1;
else
if(p%4==1)
return 3;
else
if(p%4==2)
return 9;
else
if(p%4==3)
return 7;
}
else
if(x==4)
{
if(p%2==0)
return 6;
else
if(p%2==1)
return 4;
}
else
if(x==5)
return 5;
else
if(x==6)
return 6;
else
if(x==7)
{
if(p%4==0)
return 1;
else
if(p%4==1)
return 7;
else
if(p%4==2)
return 9;
else
if(p%4==3)
return 3;
}
else
if(x==8)
{
if(p%4==0)
return 6;
else
if(p%4==1)
return 8;
else
if(p%4==2)
return 4;
else
if(p%4==3)
return 2;
}
else
if(x==9)
{
if(p%2==0)
return 1;
else
if(p%2==1)
return 9;
}
}
int main()
{
int i,s=0,j;
g<<0<<",";
for(i=1;i<=200;i++)
{
s=0;
for(j=1;j<=i;j++)
{
s=s+sum(j%10,j)%10;
}
g<<s%10<<",";
if(i==100)
{
g<<endl;
g<<0<<",";
}
}
return 0;
}*/
const int vec[]={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,0};
int main()
{
ifstream f("cifra.in");
char nr[100],n;
int T;
f>>T;
for(int i=1;i<=T;i++)
{
f>>nr;
n=nr[strlen(nr)-1]-'0';
if(strlen(nr)>=2)
n=n+10*(nr[strlen(nr)-2]-'0');
g<<vec[n]<<'\n';
}
}