from collections import deque from collections import defaultdict 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] cust = defaultdict(deque) through = [] for s in U: if s[0] == '0': for t in s[3:]: cust[t].append(int(s[1])) elif s[0] == '1': while cust[s[1]]: seat = cust[s[1]].popleft() if seat not in through: print(seat) break else: print(-1) elif s[0] == '2': through.append(int(s[1])) main()