import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); final int SIZE = 20; ArrayList[] seats = new ArrayList[SIZE]; StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { int type = sc.nextInt(); if (type == 0) { int idx = sc.nextInt() - 1; int m = sc.nextInt(); ArrayList list = new ArrayList<>(); for (int j = 0; j < m; j++) { list.add(sc.next()); } seats[idx] = list; } else if (type == 1) { int idx = -1; String dish = sc.next(); for (int j = 0; j < SIZE && idx < 0; j++) { if (seats[j] == null) { continue; } if (seats[j].remove(dish)) { idx = j + 1; break; } } sb.append(idx).append("\n"); } else { seats[sc.nextInt() - 1] = null; } } System.out.print(sb); } }