import sys import numpy as np sys.setrecursionlimit(10 ** 6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] # 1回だったら普通に割ってった方が早い class Sieve: def __init__(self, n): min_prime_factor = [2, 0] * (n // 2 + 5) for x in range(3, n + 1, 2): if min_prime_factor[x] == 0: min_prime_factor[x] = x if x ** 2 > n: continue for y in range(x ** 2, n + 5, 2 * x): if min_prime_factor[y] == 0: min_prime_factor[y] = x self.min_prime_factor = min_prime_factor # これが素因数分解(prime factorization) def pfct(self, x): if x==0:return [(0,1)] if x==1:return [(1,1)] pp, ee = [], [] while x > 1: mpf = self.min_prime_factor[x] if pp and mpf == pp[-1]: ee[-1] += 1 else: pp.append(mpf) ee.append(1) x //= mpf return [(p, e) for p, e in zip(pp, ee)] def main(): memo={} def nCr(n,r): if 2*r>n:r=n-r if r==0:return 1 if r==1:return n%9 if (n,r) in memo:return memo[n,r] ee=fac[n]-fac[r]-fac[n-r] if ee[3]>1:return 0 res=1 for a,e in enumerate(ee[1:],1): res=res*pow(a,int(e),9)%9 memo[n,r]=res return res # nCrのための前計算 mx=10**5 sv=Sieve(mx) # エラトステネスの篩 fac=np.zeros((mx+1,9),dtype="i8") for a in range(1,mx+1): for p,e in sv.pfct(a): fac[a][p%9]+=e fac=np.cumsum(fac,axis=0) # ここから本体 for _ in range(II()): s=SI() ans=0 n=len(s) for i,c in enumerate(s): ans+=int(c)*nCr(n-1,i) ans%=9 print((ans-1)%9+1) main()