from collections import defaultdict N=int(input()) guest=[None]*20 for n in range(N): query=input().split() if query[0]=="0": _,n,m,*A=query n=int(n)-1 guest[n]=defaultdict(int) for a in A: guest[n][a]+=1 elif query[0]=="1": _,B=query for n in range(20): if guest[n]==None: continue if guest[n][B]: guest[n][B]-=1 ans=n+1 break else: ans=-1 print(ans) else: _,C=query C=int(C)-1 guest[C]=None