from collections import defaultdict from heapq import heappush, heappop from sys import stdin, stdout input = lambda: stdin.readline().rstrip() write = stdout.write def main(): N = int(input()) U = [tuple(input().split()) for _ in [0] * N] sushi = defaultdict(list) seat = [[] for _ in [0] * 21] for s in U: if s[0] == '0': i = int(s[1]) for t in s[3:]: heappush(sushi[t], i) seat[i].append(t) elif s[0] == '1': while sushi[s[1]]: i = heappop(sushi[s[1]]) if s[1] in seat[i]: print(i) break else: print(-1) else: seat[int(s[1])] = [] main()