from collections import defaultdict N = int(input()) exist = [None] + [False] * 20 want_dict = defaultdict(int) for _ in range(N): U = list(input().split()) flag = int(U[0]) if flag == 0: n, m, A = int(U[1]), int(U[2]), U[3:] exist[n] = True want_dict[n] = A if flag == 1: B = U[1] for i in range(1, 21): if exist[i]: if B in want_dict[i]: print(i) want_dict[i].remove(B) break else: print(-1) if flag == 2: C = int(U[1]) exist[C] = False