// Solution to "Team Rankings" by Bob Roos
//
// Basic idea: Just generate all 5! team rankings in alpha order and
// see which one has minimum value.

import java.io.*;
import java.util.*;

public class Team {
  public static BufferedReader in;
  public static String line; // one line of input

  public static int n; // number of rankings
  public static String ranking[]; // the rankings
  public static String best,perm; // best-so-far median candidate
  public static int bestSum; // value of best-so-far candidate

/////////////////////////////////////////////////////////////////

  // main -- loops over the input instances:
  public static void main(String[] args) throws IOException {

//    in = new BufferedReader(new FileReader("team.in"));
    in = new BufferedReader(new InputStreamReader(System.in));

   // Get first instance:
   line = in.readLine().trim();
   n = Integer.parseInt(line);

   // Loop over instances:
    while (n > 0) {
      ranking = new String[n];
      for (int i = 0; i < n; i++)
        ranking[i] = in.readLine().trim();

      process();

     // Get next instance:
      line = in.readLine().trim();
      n = Integer.parseInt(line);
    }
  }

//////////////////////////////////////////////////////////////////

  // Wrapper for the "findBest" method, which does all the work:
  public static void process() {
    perm = "ABCDE"; // starting ranking
    best = ""; // best ranking so far
    bestSum = Integer.MAX_VALUE; // value of best so far

    System.out.println(findBest()+" is the median ranking with value " 
         + bestSum + ".");
  }

//////////////////////////////////////////////////////////////////

  // Recursively finds the best ranking; each recursion level is a
  // new permutation.
  public static String findBest() {
    // See if we've circled back to the starting permutation:
    if (!best.equals("") && perm.equals("ABCDE"))
      return best;

    // Run through rankings and find sum of distances for current candidate:
    int sum = 0;
    for (int i = 0; i < n; i++) {
      sum += dist(perm,ranking[i]);
    }

    // See if this is better than previous rankings:
    if (sum < bestSum) {
      best = perm;
      bestSum = sum;
    }

    // Keep going with next ranking:
    perm = nextPerm(perm);
    return findBest();
  }

//////////////////////////////////////////////////////////////////

  // Finds the distance between two rankings:
  public static int dist(String a, String b) {        
    int sum = 0;
    int rank1[] = new int[5]; // for each letter, its rank in string a
    int rank2[] = new int[5]; // for each letter, its rank in string b
    for (int i = 0; i < 5; i++) {
      rank1[a.charAt(i)-'A'] = i;
      rank2[b.charAt(i)-'A'] = i;
    }

    // Try all pairs of distinct teams:
    for (int j = 0; j < 4; j++) {
      for (int k = j+1; k < 5; k++) {
        if ( (rank1[j] < rank1[k] && rank2[j] > rank2[k]) ||
             (rank1[j] > rank1[k] && rank2[j] < rank2[k]) )
          sum++;
      }
    }
    return sum;
  }

//////////////////////////////////////////////////////////////////

  // Generates next permutation given current permutation:
  public static String nextPerm(String a) {
    int n = a.length();
    if (n >= 3 && a.charAt(n-2) < a.charAt(n-1))
      a = swap(a,n-2,n-1);
    else {
      int i;
      for (i = n-3; i >= 0; i--)
        if (a.charAt(i) < a.charAt(i+1)) 
          break;
      if (i < 0) {
         a = reverse(a,0,n-1);
      }
      else {
        int j = n-1;
        while (j > i && a.charAt(j) < a.charAt(i)) j--;
        a = swap(a,i,j);
        a = reverse(a,i+1,n-1);
      }
    }
    return a;
  }

//////////////////////////////////////////////////////////////////

  // Swap two letters in a string:
  public static String swap(String a, int i, int j) {
    String temp = "";
    for (int k = 0; k < a.length(); k++) {
      if (k == i)
        temp = temp+a.charAt(j);
      else if (k == j)
        temp = temp+a.charAt(i);
      else
        temp = temp+a.charAt(k);
    }
    return temp;
  }

//////////////////////////////////////////////////////////////////

  // Reverse the letters in a string between positions i and j:
  public static String reverse(String a, int i, int j) {
    String temp = "";
    for (int k = 0; k < i; k++)
      temp = temp + a.charAt(k);
    for (int k = j; k >= i; k--)
      temp = temp + a.charAt(k);
    return temp;
  }

}

