#include <bits/stdc++.h>
using namespace std;
const int MAX_DIGITS = 101;
const int REPEAT = 40;
int T;
int last_digits[REPEAT] = {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};
void solve() {
char N[MAX_DIGITS];
int last_three = 0;
int N_length, last_length;
scanf("%s", N);
N_length = strlen(N);
last_length = min(3, N_length);
for (int i = last_length; i >= 1; --i) {
last_three *= 10;
last_three += N[N_length - i] - '0';
}
printf("%d\n", last_digits[last_three % REPEAT]);
}
int main() {
freopen("cifra.in", "r", stdin);
freopen("cifra.out", "w", stdout);
scanf("%d", &T);
for (int i = 0; i < T; ++i) {
solve();
}
}