import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines N = int(readline()) query = readlines() hope = [[] for _ in range(21)] def query_0(q): n,m,*A = q hope[int(n)] = A def query_1(q): B = q[0] for i in range(1,21): if B in hope[i]: hope[i].remove(B) print(i) return print(-1) def query_2(q): C = q[0] hope[int(C)] = [] for q in query: t,*q = q.split() t = int(t) if t == 0: query_0(q) elif t == 1: query_1(q) elif t == 2: query_2(q)