T = int(input()) def calc(S): l = list(map(int,list(S))) while len(l) > 1: nx = [0] * (len(l) - 1) for i in range(len(l) - 1): nx[i] = (l[i] + l[i+1]) % 9 l = nx return l[0] for _ in range(T): print(calc(input()))