import collections

customers = [collections.Counter() for i in range(21)]

def set_costomer(row):
    customers[int(row[0])] = collections.Counter()
    for neta in row[2:]:
        customers[int(row[0])][neta] += 1

def remove_customer(row):
    customers[int(row[0])] = collections.Counter()

def roling_neta(neta):
    for i in range(1,21):
        if(customers[i][neta[0]] > 0):
            customers[i][neta[0]] -= 1
            return i
    return -1 

N = int(input().strip())
for n in range(N):
    row = list(input().strip().split(" "))
    if(int(row[0]) == 0):
        set_costomer(row[1:])
    if(int(row[0]) == 1):
        eaten_by = roling_neta(row[1:])
        print(eaten_by)
    if(int(row[0]) == 2):
        remove_customer(row[1:])