n = int(input()) for i in range(n): s = input() while(len(s) > 1): s = str(sum(map(int, s.split()))) print(s)