def COMB(A, B): if B > A or B < 0: return 0 ANS = 1 for I in range(min(B, A - B)): ANS = ANS * (A - I) for I in range(min(B, A - B)): ANS = ANS // (min(B, A - B) - I) return ANS T = int(input()) for _ in range(T): S = list(map(int,list(input()))) if S == [0] * len(S): print(0) continue ANS = 0 for R in range(len(S)): if R >= 1000 or len(S) - 100 >= 1000: continue ANS += COMB(len(S) - 1, R) * S[R] ANS %= 9 if ANS == 0: ANS = 9 print(ANS)