N = int(input())
A = [[] for _ in range(20+1)]
for _ in range(N):
    U = list(input().split())
    if U[0]=="0":
        seki = int(U[1])
        A[seki] = U[3:]
    elif U[0]=="1":
        neta = U[1]
        flag = -1
        for i in range(1,20+1):
            if len(A[i])==0:continue
            if neta not in A[i]:continue
            ind = A[i].index(neta)
            A[i].pop(ind)
            flag = i
            break
        print(flag)
    else:
        seki = int(U[1])
        A[seki] = []