import sys input = sys.stdin.readline class SegmentTree: def __init__(self, size, f=lambda x, y : x * y % 9, default=1): self.size = 2**(size-1).bit_length() self.default = default self.dat = [default]*(self.size*2) self.f = f def update(self, i, x): i += self.size self.dat[i] = x while i > 0: i >>= 1 self.dat[i] = self.f(self.dat[i*2], self.dat[i*2+1]) def query(self, l, r): l += self.size r += self.size lres, rres = self.default, self.default while l < r: if l & 1: lres = self.f(lres, self.dat[l]) l += 1 if r & 1: r -= 1 rres = self.f(self.dat[r], rres) l >>= 1 r >>= 1 res = self.f(lres, rres) return res def factorization(n): arr = [] temp = n for i in range(2, int(-(-n**0.5//1))+1): if temp%i==0: cnt=0 while temp%i==0: cnt+=1 temp //= i arr.append([i, cnt]) if temp!=1: arr.append([temp, 1]) return arr Q = int(input().rstrip()) T = SegmentTree(10**5+5) for _ in range(Q): S = list(input().rstrip()) S = list(map(int, S)) if len(set(S)) == 1 and S[0] == 0: print(0) continue N = len(S) - 1 L = [0] * (N + 1) ans = S[0] for i in range(1, N + 1): SS = set() for t in factorization(N - i + 1): SS.add(t[0]) L[t[0]] += t[1] for t in factorization(i): SS.add(t[0]) L[t[0]] -= t[1] for s in SS: T.update(s, pow(s, L[s], 9)) ans += T.query(0, N + 1) * S[i] ans %= 9 print(ans) if ans else print(9)