import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); Customer[] customers = new Customer[20]; StringBuilder sb = new StringBuilder(); while (n-- > 0) { int type = sc.nextInt(); if (type == 0) { int idx = sc.nextInt() - 1; Customer tmp = new Customer(idx); int count = sc.nextInt(); while (count-- > 0) { tmp.add(sc.next()); } customers[idx] = tmp; } else if (type == 1) { int idx = -1; String dish = sc.next(); for (Customer x : customers) { if (x != null && x.canEat(dish)) { idx = x.eat(dish); break; } } sb.append(idx).append("\n"); } else { customers[sc.nextInt() - 1] = null; } } System.out.print(sb); } static class Customer { int idx; HashMap counts = new HashMap<>(); public Customer(int idx) { this.idx = idx; } public void add(String dish) { counts.put(dish, counts.getOrDefault(dish, 0) + 1); } public boolean canEat(String dish) { return counts.containsKey(dish); } public int eat(String dish) { if (counts.get(dish) == 1) { counts.remove(dish); } else { counts.put(dish, counts.get(dish) - 1); } return idx + 1; } } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }