from heapq import * from collections import defaultdict removed = set() dic = defaultdict(list) cnt = 1 C2S = [] #客番→席番 S2C = [-1] * 21 #席番→客番 cstm = 0 for _ in range(int(input())): t, *dat = list(input().split()) t = int(t) if t == 0: seat, n, *dishes = dat seat = int(seat) C2S.append(seat) S2C[seat] = cstm for d in dishes: heappush(dic[d],(seat,cstm)) cstm += 1 elif t == 1: d = dat[0] if d in dic: while dic[d] and dic[d][0][1] in removed: _,_ = heappop(dic[d]) if dic[d]: s,c = heappop(dic[d]) print(C2S[c]) cnt += 1 else: print(-1) cnt += 1 else: print(-1) cnt += 1 else: seat = int(dat[0]) removed.add(S2C[seat]) S2C[seat] = -1