from collections import Counter N = int(input()) seat = [{} for _ in range(20)] for _ in range(N): f, *data = input().split() f = int(f) if f == 0: n, m, *A = data seat[int(n) - 1] = Counter(A) elif f == 1: B, *_ = data for i, order in enumerate(seat): if order and order[B]: order[B] -= 1 print(i + 1) break else: print(-1) elif f == 2: C = int(*data) seat[C] = {}