from collections import defaultdict N = int(input()) S = [defaultdict(int) for _ in range(20)] for _ in range(N): u = input() if u[0] == '0': _, n, m, *A = u.split() n = int(n) - 1 m = int(m) for i in range(m): S[n][A[i]] += 1 elif u[0] == '1': _, b = u.split() for i in range(20): if b in S[i] and S[i][b] > 0: S[i][b] -= 1 print(i + 1) break else: print(-1) else: _, c = u.split() c = int(c) - 1 S[c].clear()