n = int(input()) for i in range(n): s = input() while(len(s) > 1): s = str(sum([int(v) for v in s])) print(s)