from collections import defaultdict seat=[0 for i in range(21)] order=[defaultdict(int) for i in range(21)] def po(s): for i in range(1,21): if seat[i]==1: if order[i][s]>0: order[i][s]-=1 print(i) return print(-1) return N=int(input()) for i in range(N): S=[str(x) for x in input().split()] if S[0]=="0": n=int(S[1]) m=int(S[2]) seat[n]=1 V=defaultdict(int) for j in range(m): V[S[3+j]]+=1 order[n]=V elif S[0]=="2": n=int(S[1]) seat[n]=0 else: b=S[1] po(b)