from collections import Counter N = int(input()) table = [Counter() for _ in range(20)] for _ in range(N): data = input().split() if data[0] == '0': n = int(data[1]) - 1 table[n] = Counter(data[3:]) elif data[0] == '1': for i in range(N): if table[i][data[1]] > 0: print(i+1) table[i][data[1]] -= 1 break else: print(-1) else: table[int(data[1])] = Counter()