#include<fstream>
#include<iostream>
#include<math.h>
#include<cstring>
using namespace std;
ifstream f("cifra.in");
ofstream g("cifra.out");
int c2(int x, long long k){
if(k%4==0) return x*x*x*x%10;
if(k%4==1) return x%10;
if(k%4==2) return x*x%10;
if(k%4==3) return x*x*x%10;
}
int main(){
char n[105];
int c,T;
char denis;
f>>T;
f.get(denis);
for(int j=1;j<=T;j++){
f.getline(n,105);
int s=0;
c=strlen(n);
int v;
if(c>1)
v=(n[c-2]-'0')*10+(n[c-1]-'0');
else v=(n[c-1]-'0');
for(long long i=1;i<=v;i++){
int u=i%10;
s=s+c2(u,i);s=s%10;
}
g<<s%10<<endl;
}
}