#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
using namespace std;
//英语 抄博友程序 next_permutation
string a[108];
int main()
{
while(1)
{
int n;
cin>>n;
if(n==0)
{
break;
}
for(int i=0;i<n;i++)
{
cin>>a[i];
}
string s="ABCDE";
string jg="ABCDE";
int js=1000;
do{
int count=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<5;j++)
{
for(int k=j+1;k<5;k++)
{
char x=a[i][j];
char y=a[i][k];
int tj,tk;
for(int h=0;h<5;h++)//抄博友
{
if(s[h]==x)
{
tj=h;
}
if(s[h]==y)
{
tk=h;
}
}
if(tj>tk)
{
count++;
}
}
}
//cout<<s<<" "<<a[i]<<" "<<count<<endl;
}
if(count<js)
{
js=count;
jg=s;
//cout<<jg<<" "<<js<<endl;
}
}while(next_permutation(s.begin(),s.end()));
cout<<jg<<" is the median ranking with value "<<js<<"."<<endl;
}
return 0;
}