n = int(input()) order_list = [ [] for i in range(20) ] for i in range(n): order = [ v for v in input().split() ] if int(order[0]) == 0: order_list[int(order[1])-1] = order[3:] elif int(order[0]) == 1: get = 0 for j in range(20): if order[1] in order_list[j]: order_list[j][order_list[j].index(order[1])] = "eaten" print(j+1) get = 1 break if get == 0: print(-1) elif int(order[0]) == 2: order_list[int(order[1])-1] = []