// Arup Guha
// 6/5/2014
// Solution to 2008 UCF High School Programming Contest Problem: Sorting Student Presentations
/*** Edited on 4/13/2026 to split the data into separate files per case. ***/
// Used to illustrate output files in Java.

import java.util.*;
import java.io.*;

public class sorting {
	
	// My base file name.
	final public static String base = "sorting";

	public static void main(String[] args) throws IOException {

		Scanner fin = new Scanner(new File("sorting.in"));
		int numCases = fin.nextInt();

		// Process each case.
		for (int loop=1; loop<=numCases; loop++) {
			
			// Creeate two PrintWriters for the two files we generate for this case.
			String fName = base + format(loop, 2);
			PrintWriter inpFile = new PrintWriter(new FileWriter(fName+".in"));
			PrintWriter outFile = new PrintWriter(new FileWriter(fName+".out"));

			// Read in names.
			int n = fin.nextInt();
			
			// Echo this to the split input file for this case.
			inpFile.println(n);
			
			student[] group = new student[n];
			for (int i=0; i<n; i++) {
				String myname = fin.next();
				
				// Echo this to the split input file for this case.
				inpFile.println(myname);
				group[i] = new student(myname);
			}
			
			// Input file is done.
			inpFile.close();

			// Sort.
			Arrays.sort(group);

			// Print result - not part of output for single case input.
			System.out.println("Class #"+loop+" ordering");
			
			for (int i=0; i<n; i++) {
				System.out.println(group[i]);
				
				// Echo this to the output file for this case.
				outFile.println(group[i]);
			}
			
			// Close the output file for this case.
			outFile.close();
			System.out.println();
		}

		fin.close();
	}
	
	// Returns a string version of number using exactly numDigits digits, padding
	// with 0s, assumes the user passes it a number with <= numDigits digits.
	public static String format(int number, int numDigits) {
		String res = ""+number;
		while ( res.length() < numDigits)
			res = "0" + res;
		return res;
	}
}

class student implements Comparable<student> {

	private String name;
	private int[] freq;

	// Key is to store frequency array.
	public student(String s) {
		name = s;
		freq = new int[26];
		for (int i=0; i<s.length(); i++)
			freq[s.charAt(i)-'A']++;
	}

	// Just follow the rules.
	public int compareTo(student other) {

		// We break ties by frequency of letters in order.
		for (int i=0; i<freq.length; i++)
			if (freq[i] != other.freq[i])
				return other.freq[i] - freq[i];

		// Java needs this for a complete definition.
		return 0;
	}

	// For printing.
	public String toString() {
		return name;
	}
}