#include using namespace std; #define repd(i,a,b) for (ll i=(a);i<(b);i++) #define rep(i,n) repd(i,0,n) #define all(x) (x).begin(),(x).end() template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } typedef long long ll; typedef pair P; typedef vector vec; using Graph = vector>; const long long INF = 1LL<<60; const long long MOD = 1000000007; ll nCk(ll n,ll k){ ll res=1; chmin(k,n-k); rep(i,k){ res*=(n-i); res/=(i+1); } if(k<0)res=0; return res; } int main() { ios::sync_with_stdio(false); cin.tie(0); ll T;cin>>T; rep(ti,T){ string s;cin>>s; bool flg=1; ll n=s.size(); ll ans=0; rep(i,n){ if(s[i]!='0')flg=0; ll x=n-1; ll y=i; vec u,v; while(x){ u.push_back(x%9); x/=9; } rep(j,u.size()){ v.push_back(y%9); y/=9; } ll now=1; rep(j,u.size()){ now*=nCk(u[j],v[j]); now%=9; } now*=s[i]-'0'; now%=9; ans+=now; } ans%=9; if(flg==0&&ans==0)ans=9; cout<