k = int(input()) customer = [] sushi_list = {} def sushi_flow(neta): for i in customer: if neta in sushi_list[i]: print(i) sushi_list[i].remove(neta) return print(-1) def customer_comein(c_num,netas): customer.append(c_num) customer.sort() sushi_list[c_num] = netas for i in range(k): lst = input().split() if lst[0] == "1": sushi_flow(lst[1]) elif lst[0] == "0": customer_comein(int(lst[1]),lst[3:]) else: customer.remove(int(lst[1])) del sushi_list[int(lst[1])]