// Arup Guha
// 6/5/2014
// Solution to 2008 UCF High School Programming Contest Problem: Sorting Student Presentations
/*** Edited on 4/13/2026 to illustrate file output. ***/

import java.util.*;
import java.io.*;

public class sortingoutfile {

	public static void main(String[] args) throws IOException {

		Scanner fin = new Scanner(new File("sorting.in"));
		int numCases = fin.nextInt();
		
		// Open the file for output. I am calling it a different name than in the notes
		// so that I don't lose the correct output file in my directory.
		PrintWriter out = new PrintWriter(new FileWriter("sortingtest.out"));
		
		// Process each case.
		for (int loop=1; loop<=numCases; loop++) {

			// Read in names.
			int n = fin.nextInt();
			student[] group = new student[n];
			for (int i=0; i<n; i++)
				group[i] = new student(fin.next());

			// Sort.
			Arrays.sort(group);

			// Write result to file.
			out.println("Class #"+loop+" ordering");
			for (int i=0; i<n; i++)
				out.println(group[i]);
			out.println();
		}
		
		// Close both files, input and output.
		out.close();
		fin.close();
	}
}

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;
	}
}