import java.util.*; import java.io.*; public class lastdance_travis { public static void main(String[] Args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-->0) { int n = sc.nextInt(); int q = sc.nextInt(); // queries int g = sc.nextInt(); // genres HashMap personToGenre = new HashMap(); HashMap genreToCount = new HashMap(); while(q-->0) { char type = sc.next().charAt(0); if (type == 'Q') { int curG = sc.nextInt(); int ans = 0; if (genreToCount.containsKey(curG)) ans = genreToCount.get(curG); System.out.println(ans); } else { int curG = sc.nextInt(); if (!genreToCount.containsKey(curG)) genreToCount.put(curG, 0); int count = sc.nextInt(); while (count-->0) { int curPerson = sc.nextInt(); if (personToGenre.containsKey(curPerson)) { int oldGenre = personToGenre.get(curPerson); genreToCount.put(oldGenre, genreToCount.get(oldGenre) - 1); } genreToCount.put(curG, genreToCount.get(curG) + 1); personToGenre.put(curPerson, curG); } } } } } }