import java.util.*;

public class rating {

	// Constants that are nice to have for this problem.
	final public static int MAX_SCORE = 10;
	final public static int PASS_SCORE = 6;

	final public static int[] LIMITS = {10, 30, 100};
	final public static String[] HEADERS = {"SMALL CLASS RANKING", "MEDIUM CLASS RANKING", "LARGE CLASS RANKING"};

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int numCases = stdin.nextInt();

		// Process each case.
		for (int loop=0; loop<numCases; loop++) {

			String county = stdin.next();
			int n = stdin.nextInt();

			// Create three lists for the three types of teachers.
			ArrayList[] groups = new ArrayList[LIMITS.length];
			for (int i=0; i<groups.length; i++) groups[i] = new ArrayList<teacher>();

			// Read in each teacher.
			for (int i=0; i<n; i++) {

				// Get data for this teacher.
				String name = stdin.next();
				int[] scores = new int[MAX_SCORE+1];
				int total = 0;
				for (int j=1; j<=MAX_SCORE; j++) {
					scores[j] = stdin.nextInt();
					total += scores[j];
				}

				// Figure out what type of teacher and add to that group.
				for (int j=0; j<LIMITS.length; j++) {
					if (total <= LIMITS[j]) {
						groups[j].add(new teacher(name, scores));
						break;
					}
				}
			}

			// Sort each list.
			for (int i=0; i<LIMITS.length; i++) Collections.sort(groups[i]);

			// Need this for each case.
			System.out.println(county+" COUNTY");

			// Print output - for each group.
			for (int i=0; i<groups.length; i++) {

				// For each group, we print the header and the teachers in order.
				System.out.println(HEADERS[i]);
				for (int j=0; j<groups[i].size(); j++)
					System.out.println(groups[i].get(j));
				System.out.println();
			}

		}
	}
}

class teacher implements Comparable<teacher> {

	public String name;
	public int[] scores;
	public int total;
	public int numPass;
	public int sumScores;

	public teacher(String n, int[] myscores) {
		name = n;
		scores = myscores;

		// Calculate relevant statistics abouut this teacher here.
		total = 0;
		numPass = 0;
		sumScores = 0;
		for (int i=1; i<scores.length; i++) {
			total += scores[i];
			if (i >= rating.PASS_SCORE) numPass += scores[i];
			sumScores += (i*scores[i]);
		}
	}

	public String toString() {
		return name;
	}

	public int compareTo(teacher other) {

		// First look at difference in passing percentage.
		int passperc = comparePassPerc(other);
		if (passperc != 0) return passperc;

		// Guaranteed that this will break the tie.
		return compareAvgScore(other);
	}

	// This avoids precision issues. Also a tolerance of 1e-9 can be used.
	// No overflow because all values are <= 100.
	public int comparePassPerc(teacher other) {
		return other.numPass*this.total - this.numPass*other.total;
	}

	// Same issue as stated above. Again, no fear of overflow due to the bounds.
	public int compareAvgScore(teacher other) {
		return other.sumScores*this.total - this.sumScores*other.total;
	}
}