import sys input=lambda: sys.stdin.readline().rstrip() n=int(input()) L=[[] for i in range(20)] for _ in range(n): U=[str(i) for i in input().split()] if U[0]=="0": n,m=int(U[1]),int(U[2]) for i in range(3,3+m): L[n-1].append(U[i]) elif U[0]=="1": b=U[1] for i in range(20): if b in L[i]: print(i+1) L[i].remove(b) break else: print(-1) else: c=int(U[1]) L[c-1]=[]