#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair pii; typedef pair pll; const int INF = 1e9; const ll LINF = 1e18; inline ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; } inline ll lcm(ll a, ll b) { return a / gcd(a, b)*b; } template ostream& operator << (ostream& out,const pair& o){ out << "(" << o.first << "," << o.second << ")"; return out; } template ostream& operator << (ostream& out,const vector V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; } template ostream& operator << (ostream& out,const vector > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; } template ostream& operator << (ostream& out,const map mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; } /* 問題文============================================================ ================================================================= 解説============================================================= ================================================================ */ const ll MAX_PRIME = 1e5; int divprime[MAX_PRIME]; void init_primes(){ for(ll i = 2; i <= MAX_PRIME;i++){ if(!divprime[i]){ for(ll j = i*i; j <= MAX_PRIME;j+=i) divprime[j]=i; } } } const ll MOD = 9; // a^b % MOD; ll powmod(ll a,ll b) {ll res=1;a%=MOD; for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;} template Type solve(Type res = Type()){ init_primes(); int T; cin >> T; while(T--){ string S; cin >> S; if(atoll(S.c_str()) == 0) { cout << 0 << endl; continue; } ll ans = 0; int sz = (int)S.length(); int num[10] = {}; for(int i = 0; i < sz;i++){ if(num[3] < 2){ ll x = 1; for(int j = 2; j < 9;j++) x *= powmod(j,num[j]); ans += x*(S[i]-'0'); }else{ ans += 9*(S[i]-'0'); } if(i < sz-1){ ll x = sz-1-i; ll y = 1+i; while(divprime[x]){ num[divprime[x]%9]++; x /= divprime[x];} num[x%9]++; while(divprime[y]){ num[divprime[y]%9]--; y /= divprime[y];} num[y%9]--; } } while(ans>=10) ans = ans%10+ans/10; cout << ans << endl; } return res; } int main(void) { cin.tie(0); ios::sync_with_stdio(false); solve(0); //cout << fixed << setprecision(12) << solve() << endl; return 0; }