#include using namespace std; typedef long long int64; const int mod = 9; int64 modCombination(int n, int r) { if(n < 0 || r < 0 || r > n) return 0; if(n - r < r) r = n - r; if(r == 0) return 1; if(r == 1) return n; int numer[r]; int denom[r]; for(int k = 0; k < r; k++) { numer[k] = n - r + k + 1; denom[k] = k + 1; } for(int p = 2; p <= r; p++) { int piv = denom[p - 1]; if(piv > 1) { int ofst = (n - r) % p; for(int k = p - 1; k < r; k += p) { numer[k - ofst] /= piv; denom[k] /= piv; } } } long ret = 1; for(int k = 0; k < r; k++) if(numer[k] > 1) ret = ret * numer[k] % mod; return ret; } int main() { int T; cin >> T; while(T--) { string S; cin >> S; bool f = false; for(int j = 0; j < S.size(); j++) { if(S[j] != '0') break; if(j + 1 == S.size()) { cout << 0 << endl; f = true; } } if(!f) { int ans = 0; for(int j = 0; j < S.size(); j++) { ans = (int) ((ans + (S[j] - '0') * modCombination(S.size() - 1, j)) % 9); } cout << (ans == 0 ? 9 : ans) << endl; } } }