package net.ipipip0129.yukicoder.no714; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; class Main { protected static int MAX_DATA_SIZE = 53; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); People[] peoples = new People[21]; IDSystem id_sys = new IDSystem(); int num = Integer.parseInt(scanner.nextLine()); for (int i = 0; i < num; i++) { String line = scanner.nextLine(); String[] string_data = line.split(" "); if (line.toCharArray()[0] == '0') { List data_list = new ArrayList<>(Arrays.asList(string_data).subList(3, 3 + Integer.parseInt(string_data[2]))); peoples[Integer.parseInt(string_data[1])] = new People(id_sys, data_list); } else if (line.toCharArray()[0] == '1') { int id = id_sys.getID(string_data[1]); int ans = -1; for (int j = 1; j < 21; j++) { if (peoples[j] != null) { if (peoples[j].req_process(id)){ ans = j; break; } } } System.out.println(ans); } else if (line.toCharArray()[0] == '2') { peoples[Integer.parseInt(string_data[1])] = null; } } } } class People { private int[] req_list = new int[Main.MAX_DATA_SIZE]; People(IDSystem id_sys, List data_list) { for (String data : data_list) { req_list[id_sys.getID(data)]++; } } boolean req_process(int id) { if (req_list[id] != 0) { req_list[id]--; return true; } return false; } } class IDSystem { private static String[] string_data = new String[Main.MAX_DATA_SIZE]; private static int next_id = 0; int getID(String data) { for (int i = 0; i < next_id; i++) { if (string_data[i].equals(data)) { return i; } } string_data[next_id] = data; next_id++; return next_id - 1; } }