import heapq def main(): n = int(input()) sd = {} seat = [0 for i in range(20)] for i in range(n): data = input().split() if data[0] == "0": seat[int(data[1])-1] = 1 for j in range(int(data[2])): if data[3+j] not in sd: sd[data[3+j]] = [] heapq.heappush(sd[data[3+j]], int(data[1])-1) elif data[0] == "1": if data[1] in sd: while len(sd[data[1]]) != 0 and seat[sd[data[1]][0]] == 0: heapq.heappop(sd[data[1]]) if len(sd[data[1]]) == 0: print(-1) else: idx = heapq.heappop(sd[data[1]]) print(idx+1) else: print(-1) else: seat[int(data[1])-1] = 0 if __name__ == '__main__': main()