import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		new Main().run();
	}

	void run() {
		Scanner sc = new Scanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);
		int N = sc.nextInt();
		HashMap<String, Integer>[] hs = new HashMap[20];
		for (int i = 0; i < hs.length; ++i) {
			hs[i] = new HashMap<String, Integer>();
		}
		for (int i = 0; i < N; ++i) {
			int t = sc.nextInt();
			if (t == 0) {
				int ni = sc.nextInt() - 1;
				int mi = sc.nextInt();
				for (int j = 0; j < mi; ++j) {
					add(hs[ni], sc.next());
				}
			} else if (t == 1) {
				String s = sc.next();
				boolean f = false;
				for (int j = 0; j < 20; ++j) {
					if (hs[j].containsKey(s)) {
						pw.println(j + 1);
						rm(hs[j], s);
						f = true;
						break;
					}
				}
				if (!f)
					pw.println(-1);
			} else if (t == 2) {
				hs[sc.nextInt() - 1].clear();
			}

		}
		pw.close();
	}

	void add(HashMap<String, Integer> h, String s) {
		if (h.containsKey(s)) {
			h.put(s, h.get(s) + 1);
		} else {
			h.put(s, 1);
		}
	}

	void rm(HashMap<String, Integer> h, String s) {
		if (h.get(s) == 1) {
			h.remove(s);
		} else {
			h.put(s, h.get(s) - 1);
		}
	}

}