public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String line = "";
		StringBuilder out = new StringBuilder();
		while ((line = in.readLine()) != null && line.length() != 0) {
			int nSet = Integer.parseInt(line.trim());
			for (int i = 0; i < nSet; i++) {
				if (i == 0)
					in.readLine();
				int nNodes = in.readLine().trim().charAt(0) - 'A' + 1;
				WQU_PC G = new WQU_PC(nNodes);
				while ((line = in.readLine()) != null && line.length() != 0)
					G.union(line.charAt(0) - 'A', line.charAt(1) - 'A');
				if (i != 0)
					out.append("\n");
				out.append(G.getNumberConnectedComponents(nNodes) + "\n");
			}
		}
		System.out.print(out);
	}