import sys input = sys.stdin.readline from collections import Counter LIST=[Counter() for i in range(25)] N=int(input()) for tests in range(N): L=input().split() if L[0]=="1": x=L[1] for i in range(25): if x in LIST[i] and LIST[i][x]>0: print(i) LIST[i][x]-=1 break else: print(-1) elif L[0]=="0": C=Counter(L[3:]) LIST[int(L[1])]=C else: x=int(L[1]) LIST[x]=Counter()