def solve(N, lines): hopelist = {} result = [] for l in lines[1:]: if l.startswith("0"): d = l.split(" ") seat = int(d[1]) _ = int(d[2]) hopelist[seat] = d[3:] if l.startswith("1"): neta = l.split(" ")[1] found = False for seatn, hopes in sorted(hopelist.items()): for i, h in enumerate(hopes): if h == neta: hopes.pop(i) result.append(seatn) found = True break if found: break if not found: result.append(-1) elif l.startswith("2"): seat = int(l.split(" ")[1]) hopelist[seat] = [] return result if __name__ == "__main__": N = int(input()) [print(s) for s in solve(N, [input() for _ in range(N)])]