from collections import defaultdict n = int(input()) Cnt = defaultdict(lambda :[0 for _ in range(21)]) for _ in range(n): L = list(input().split()) if L[0] == '0': j = int(L[1]) m = int(L[2]) L = L[3:] for i in range(m): Cnt[L[i]][j] += 1 elif L[0] == '1': s = L[1] flag = False for i in range(1, 21): if Cnt[s][i] > 0: Cnt[s][i] -= 1 print(i) flag = True break if not flag: print(-1) elif L[0] == '2': j = int(L[1]) for s in Cnt.keys(): Cnt[s][j] = 0