import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); ArrayList[] list = new ArrayList[21]; for(int i = 0; i < 21; i++) { list[i] = new ArrayList(); } for(int i = 0; i < n; i++) { int d = sc.nextInt(); if(d == 0) { int seki = sc.nextInt(); int m = sc.nextInt(); for(int j = 0; j < m; j++) { String a = sc.next(); list[seki].add(a); } } else if(d == 1) { String neta = sc.next(); int ans = -1; for(int j = 1; j < 21; j++) { if(list[j].contains(neta)) { ans = j; list[j].remove(neta); break; } } System.out.println(ans); } else { int c = sc.nextInt(); list[c] = new ArrayList(); } } } }