N = int(input()) S = [[]] * 20 for _ in range(N): U = input().split() if U[0] == "0": S[int(U[1]) - 1] = U[3:] elif U[0] == "1": for i in range(20): if U[1] in S[i]: print(i + 1) S[i].pop(S[i].index(U[1])) break else: print(-1) else: S[int(U[1]) - 1].clear()