#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 1^1 + ... + 1^20 ends in 4, and it's all periodic from there.
int a[4][10] = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 0},
{1, 4, 9, 6, 5, 6, 9, 4, 1, 0},
{1, 8, 7, 4, 5, 6, 3, 2, 9, 0},
{1, 6, 1, 6, 5, 6, 1, 6, 1, 0}};
FILE *fin, *fout;
void query(char* s) {
int l = strlen(s);
s[--l] = 0; // discard the \n
l -= 2;
if (l < 0) {
l = 0;
}
int n = atoi(s + l);
int result = (n / 20) * 4;
n %= 20;
for (int i = 0; i < n; i++) {
result += a[i % 4][i % 10];
}
fprintf(fout, "%d\n", result % 10);
}
int main(void) {
int numTests;
fin = fopen("cifra.in", "rt");
fout = fopen("cifra.out", "wt");
fscanf(fin, "%d\n", &numTests);
char s[200];
for (int testNum = 0; testNum < numTests; testNum++) {
fgets(s, 150, fin);
query(s);
}
fclose(fin);
fclose(fout);
return 0;
}