// Arup Guha
// 6/5/2014
// Solution to 2008 UCF High School Programming Contest Problem: Sorting Student Presentations

import java.util.*;
import java.io.*;

public class 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++) {

			// 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);

			// Print result.
			System.out.println("Class #"+loop+" ordering");
			for (int i=0; i<n; i++)
				System.out.println(group[i]);
			System.out.println();
		}

		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;
	}
}