// Rankings problem
// 2004 ECNA ACM Regional 
// t feil

#include <iostream>
using namespace std;

const bool Debug=false;

char Median[5], P[5], ballot[5];
int votes[1000][5];
int n,minimum;

void FindMedian(int k){
  int thiscount;

if(Debug){
  for(int i=0;i<n;i++){
    for(int j=0;j<5;j++)
      cout<<(char)('A'+votes[i][j]);
    cout<<endl;
  }
  cout<<endl;
}

for(int i1=0;i1<5;i1++)
  for(int i2=0;i2<5;i2++)
    if(i2!=i1)
      for(int i3=0;i3<5;i3++)
        if(i3!=i1 && i3!=i2)
          for(int i4=0;i4<5;i4++)
            if(i4!=i1 && i4!=i2 && i4!=i3) 
              for(int i5=0;i5<5;i5++)
                if(i5!=i1 && i5!=i2 && i3!=i5 && i5 !=i4){
                   P[0]=i1; P[1]=i2; P[2]=i3; P[3]=i4; P[4]=i5;
                   thiscount = 0;
                  for(int i=0;i<n;i++)
                   //find distance from P[] to votes[i][]
                   for(int j=0;j<4;j++)
                     for(int k=j+1;k<5;k++)
                       if((P[j]<P[k] && votes[i][j]>votes[i][k]) ||
                          (P[j]>P[k] && votes[i][j]<votes[i][k]))
                       thiscount++;
if (Debug){
    cout<<"value: "<<thiscount<<' ';
    for(int i=0;i<5;i++)
      cout<<(char)('A'+P[i]);
    cout<<endl;
}
                  //thiscount = total distances
                  if(thiscount<minimum){
                    minimum=thiscount;
                    for(int i=0;i<5;i++)
                      Median[i]=P[i];
if (Debug){
    cout<<"update: "<<minimum<<' ';
    for(int i=0;i<5;i++)
      cout<<(char)('A'+Median[i]);
    cout<<endl;
}
                  } 
                 }
}

int main(){

  cin>>n;
  while(n>0){
    for(int i=0;i<n;i++){  //get votes
      for(int j=0;j<5;j++)
        cin>>ballot[j];
      //convert
      for(int j=0;j<5;j++)
        votes[i][(int) (ballot[j]-'A')] = j;
    } 
if(Debug){
  for(int i=0;i<n;i++){
    for(int j=0;j<5;j++)
      cout<<(char)('A'+votes[i][j]);
    cout<<endl;
  }
  cout<<endl;
}

    minimum = n*120;
    FindMedian(1);
    
    //print result
    for(int i=0;i<5;i++)
     for(int j=0;j<5;j++) //search for letter in position i
       if(Median[j]==i) cout<<(char)('A'+j);
    cout<<" is the median ranking with value "<<minimum<<'.'<<endl;

    cin>>n;
  }
return 0;
}
