//#include "stdafx.h"
//#include "LastDigit.h"
#include <fstream>
#include <iostream>
#include <string>
std::ifstream in("cifra.in");
std::ofstream out("cifra.out");
char GetLastDigitOfPower(int base, int power);
char GetLastDigitOfSequence(int number);
int strlen(char* s)
{
int count = 0;
char c;
while((c = s[count]) != '\0')
{
count++;
}
return count;
}
int main()
{
//auto res = strlen("1\0"); //3
//res = strlen("abs"); //3
//res = strlen("absabs"); //6
//res = strlen("absabsabsabs"); //12
// Constant vector
int r[10][10];
r[0][0]=0,r[0][1]=1,r[0][2]=5,r[0][3]=2,r[0][4]=8,r[0][5]=3,r[0][6]=9,r[0][7]=2,r[0][8]=8,r[0][9]=7;
r[1][0]=7,r[1][1]=8,r[1][2]=2,r[1][3]=9,r[1][4]=5,r[1][5]=0,r[1][6]=6,r[1][7]=9,r[1][8]=5,r[1][9]=4;
r[2][0]=4,r[2][1]=5,r[2][2]=9,r[2][3]=6,r[2][4]=2,r[2][5]=7,r[2][6]=3,r[2][7]=6,r[2][8]=2,r[2][9]=1;
r[3][0]=1,r[3][1]=2,r[3][2]=6,r[3][3]=3,r[3][4]=9,r[3][5]=4,r[3][6]=0,r[3][7]=3,r[3][8]=9,r[3][9]=8;
r[4][0]=8,r[4][1]=9,r[4][2]=3,r[4][3]=0,r[4][4]=6,r[4][5]=1,r[4][6]=7,r[4][7]=0,r[4][8]=6,r[4][9]=5;
r[5][0]=5,r[5][1]=6,r[5][2]=0,r[5][3]=7,r[5][4]=3,r[5][5]=8,r[5][6]=4,r[5][7]=7,r[5][8]=3,r[5][9]=2;
r[6][0]=2,r[6][1]=3,r[6][2]=7,r[6][3]=4,r[6][4]=0,r[6][5]=5,r[6][6]=1,r[6][7]=4,r[6][8]=0,r[6][9]=9;
r[7][0]=9,r[7][1]=0,r[7][2]=4,r[7][3]=1,r[7][4]=7,r[7][5]=2,r[7][6]=8,r[7][7]=1,r[7][8]=7,r[7][9]=6;
r[8][0]=6,r[8][1]=7,r[8][2]=1,r[8][3]=8,r[8][4]=4,r[8][5]=9,r[8][6]=5,r[8][7]=8,r[8][8]=4,r[8][9]=3;
r[9][0]=3,r[9][1]=4,r[9][2]=8,r[9][3]=5,r[9][4]=1,r[9][5]=6,r[9][6]=2,r[9][7]=5,r[9][8]=1,r[9][9]=0;
int inputsCount = 0;
in >> inputsCount;
while (inputsCount > 0)
{
char s[110];
in >> s;
int lastDigit;
int secondToLastDigit;
char c1, c2;
if(strlen(s)>=2)
{
c1 = s[strlen(s)-2];
c2 = s[strlen(s)-1];
secondToLastDigit = atoi(&c1);
lastDigit = atoi(&c2);
}
else
{
c2 = s[strlen(s)-1];
secondToLastDigit = 0;
lastDigit = atoi(&c2);
}
out << r[secondToLastDigit][lastDigit] << "\n";
inputsCount--;
}
return 0;
}
char GetLastDigitOfSequence(int maxNumber)
{
char digit = 0;
char lastDigit;
for (int number = 1; number <= maxNumber; number++)
{
lastDigit = GetLastDigitOfPower(number, number);
digit = (digit + lastDigit) % 10;
}
return digit;
}
char GetLastDigitOfPower(int base, int power)
{
char digit = 1;
while(power > 0)
{
digit = (digit * base ) % 10;
power--;
}
return digit;
}