#include using namespace std; #define _p(...) (void)printf(__VA_ARGS__) #define forr(x,arr) for(auto&& x:arr) #define _overload3(_1,_2,_3,name,...) name #define _rep2(i,n) _rep3(i,0,n) #define _rep3(i,a,b) for(int i=int(a);i=int(a);i--) #define rrep(...) _overload3(__VA_ARGS__,_rrep3,_rrep2,)(__VA_ARGS__) #define ALL(x) (x).begin(), (x).end() #define BIT(n) (1LL<<(n)) #define SZ(x) ((int)(x).size()) #define fst first #define snd second using ll=long long;using pii=pair;using vb=vector; using vi=vector;using vvi=vector;using vvvi=vector; using vl=vector;using vvl=vector;using vvvl=vector; using vd=vector;using vvd=vector;using vvvd=vector; using vpii=vector;using vvpii=vector;using vvvpii=vector; template T read() {T t; cin >> t; return t;} template map factorize(T n) { map ret; for (T i = 2; i * i <= n; i++) { while (n % i == 0) { ++ret[i]; n /= i; } } if (n != 1) ret[n] = 1; return ret; } long long extgcd(long long a, long long b, long long& x, long long& y) { long long g = a; x = 1; y = 0; if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x; return g; } long long invMod(long long x, long long m) { long long s, t; extgcd(x, m, s, t); return (m + s) % m; } struct Combination { struct CombinationLucas { long long p, q; long long mod; vector power; vector F, invF; vector e; CombinationLucas(int n, long long p, long long q) : p(p), q(q), power(q + 1), F(n), invF(n), e(n) { mod = 1; for (int i = 0; i < q; i++) { mod *= p; } power[0] = 1; for (int i = 1; i <= q; i++) { power[i] = power[i - 1] * p % mod; } F[0] = 1; for (long long i = 1; i < n; i++) { if (i % p == 0) { F[i] = F[i - 1]; } else { F[i] = F[i - 1] * i % mod; } } invF[n - 1] = invMod(F[n - 1], mod); for (long long i = n - 2; i >= 0; i--) { if ((i + 1) % p == 0) { invF[i] = invF[i + 1]; } else { invF[i] = invF[i + 1] * (i + 1) % mod; } } for (int i = 1; i < n; i++) { F[i] = F[i] * F[i / p] % mod; invF[i] = invF[i] * invF[i / p] % mod; e[i] = i / p + e[i / p]; } } long long operator()(int n, int r) { if (n < 0 || r < 0 || n < r) return 0; long long result = F[n] * invF[n - r] % mod * invF[r] % mod; result = result * power[min(q, e[n] - e[n - r] - e[r])] % mod; return result; } }; vector inv; vector cs; Combination(int n, long long mod) { auto pf = factorize(mod); for (auto kv : pf) { cs.emplace_back(n, kv.first, kv.second); } long long m = 1; for (auto &c : cs) { inv.push_back(invMod(m, c.mod)); m *= c.mod; } } long long operator()(int n, int r) { long long x = 0; long long mod = 1; for (int i = 0; i < cs.size(); i++) { long long y = cs[i](n, r); x += mod * (y - x) * inv[i]; mod *= cs[i].mod; x %= mod; } return (x % mod + mod) % mod; } }; Combination cb(101010, 9); void Main() { string S = read(); int N = SZ(S); ll ans = 0; rep(i, N) { int a = S[i] - '0'; int c = cb(N-1, i); //_p("%d,C(%d,%d):%d\n", a, N-1,i,c); ans += 1 + (a * c - 1) % 9; ans = 1 + (ans - 1) % 9; } int c0 = 0; forr(c, S) if (c == '0') c0++; if (ans == 0 && c0 != N) cout << 9 << endl; else cout << ans << endl; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int T = read(); while (T--) Main(); return 0; }