#include <iostream>
#include <fstream>
#include <string>
using namespace std;
ifstream f("cifra.in");
ofstream f2("cifra.out");
int per[100]={1,4,7,6,5,6,3,6,9,0,1,6,3,6,5,6,7,4,9,0,1,4,7,6,5,6,3,6,9,0,1,6,3,6,5,6,7,4,9,0,1,4,7,6,5,6,3,6,9,0,1,6,3,6,5,6,7,4,9,0,1,4,7,6,5,6,3,6,9,0,1,6,3,6,5,6,7,4,9,0,1,4,7,6,5,6,3,6,9,0,1,6,3,6,5,6,7,4,9,0};
int main() {
int n;
f>>n;
while(n>0) {
long s=0;
string a;
getline(f,a);
int i=(int)a.length();
int m=(a[i-2]-48)*10+a[i-1]-48;
for(int k=1;k<=100;k++) {
int tot=i/2+1;
if(k>m) tot--;
s+=tot*per[k-1];
}
s%=10;
if(m>=0) f2<<s<<endl;
if(m>=0) n--;
}
return n;
}