import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines import numpy as np T = int(readline()) S = readlines() answer = [] for s in S: A = np.array(list(s.decode().rstrip()),np.int32) while len(A) > 1: A = (A[:-1] + A[1:]) % 9 x = A[0] if not x: x = 9 answer.append(x) print('\n'.join(map(str,answer)))