N = int(input()) check = set(range(10)) for i in range(N): S = input().split() num = list(map(int,S[:4])) flag = S[4] choice = set(num) if (flag == "YES"): check &= choice else: check -= choice print(check.pop())