#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; // Sieve of Eratosthenes // https://youtu.be/UTVg7wzMWQc?t=2774 struct Sieve { int n; vector f, primes; Sieve(int n=1):n(n), f(n+1) { f[0] = f[1] = -1; for (ll i = 2; i <= n; ++i) { if (f[i]) continue; primes.push_back(i); f[i] = i; for (ll j = i*i; j <= n; j += i) { if (!f[j]) f[j] = i; } } } bool isPrime(int x) { return f[x] == x;} vector factorList(int x) { vector res; while (x != 1) { res.push_back(f[x]); x /= f[x]; } return res; } vector

factor(int x) { vector fl = factorList(x); if (fl.size() == 0) return {}; vector

res(1, P(fl[0], 0)); for (int p : fl) { if (res.back().first == p) { res.back().second++; } else { res.emplace_back(p, 1); } } return res; } vector> factor(ll x) { vector> res; for (int p : primes) { int y = 0; while (x%p == 0) x /= p, ++y; if (y != 0) res.emplace_back(p,y); } if (x != 1) res.emplace_back(x,1); return res; } }sieve(350); ll modpow(ll a, ll b, ll mod) { ll res = 1; while (b) { if (b & 1)res = res * a % mod; a = a * a % mod; b >>= 1; } 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; Graph g(n,vec(9)); repd(i,1,n){ vector> v=sieve.factor(i); for(auto[x,y]:v){ g[i][x%9]+=y; } } rep(i,9){ rep(j,n-1)g[j+1][i]+=g[j][i]; } repd(i,0,n){ if(s[i]!='0')flg=0; ll now=1; rep(j,9){ ll x=g[n-1][j]-g[n-1-i][j]; ll y=g[i][j]; x-=y; x=modpow(j,x,9); now*=x; now%=9; } now*=s[i]-'0'; ans+=now; } ans%=9; if(flg==0&&ans==0)ans=9; cout<