import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Customer[] customers = new Customer[20]; StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { int type = sc.nextInt(); if (type == 1) { boolean eated = false; String sushi = sc.next(); for (int j = 0; j < 20; j++) { if (customers[j] != null && customers[j].hasEat(sushi)) { sb.append(j + 1).append("\n"); eated = true; break; } } if (!eated) { sb.append(-1).append("\n"); } } else if (type == 0) { int idx = sc.nextInt() - 1; customers[idx] = new Customer(); int count = sc.nextInt(); for (int j = 0; j < count; j++) { customers[idx].add(sc.next()); } } else { customers[sc.nextInt() - 1] = null; } } System.out.print(sb); } static class Customer { HashMap map = new HashMap<>(); public void add(String sushi) { map.put(sushi, map.getOrDefault(sushi, 0) + 1); } public boolean hasEat(String sushi) { if (map.containsKey(sushi)) { if (map.get(sushi) == 1) { map.remove(sushi); } else { map.put(sushi, map.get(sushi) - 1); } return true; } else { return false; } } } }