# coding: utf-8 def fact(n): r = 1 while n > 1: r *= n n -= 1 return r def ncr(n, r): return fact(n) // fact(r) // fact(n - r) def solve(s): i_list = list(map(int, s)) n = len(i_list) r = 0 for i in range(n): r += ncr(n-1, i) * i_list[i] while r >= 10: r = sum(map(int, str(r))) return r T = int(input()) for i in range(T): print(solve(input().strip()))