from sortedcontainers import SortedList from collections import defaultdict removed = set() dic = defaultdict(SortedList) for _ in range(int(input())): t, *dat = list(input().split()) t = int(t) if t == 0: seat, n, *dishes = dat seat = int(seat) for d in dishes: dic[d].add(seat) elif t == 1: d = dat[0] if d in dic: while dic[d] and dic[d][0] in removed: x = dic[d].pop(0) if dic[d]: x = dic[d].pop(0) print(x) if not dic[d]: dic.pop(d) else: print(-1) else: removed.add(int(dat[0]))