#include "iostream"
#include "string"
using namespace std;
int main()
{
int n;
while (cin >> n)
{
if (n == 0) break;
string *str = new string [n];
for (int i = 0; i < n; i++)//字符串的输入
{
if (i == 0) cin.get();
getline(cin, str[i]);
}
string *str1 = new string [n*2];
int j = 0;
for (int i = 0; i < n; i++)//将字符串拆为两部分,前部分为字符,后部分为数字
{
j = i * 2;
int length = str[i].find(" ");
for (int m = 0; m < length; m++)
{
str1[j].push_back(str[i][m]);
}
for (int q = str[i].find(" ")+1; q < str[i].size(); q++)
{
str1[j+1].push_back(str[i][q]);
}
}
string *s = new string [n];
int q = 0;
for (int m = 1; m < n * 2; m++)//对字符串的比较,对相同的字符串进行整理
for (int i = m + 2; i < n * 2; i += 2)
if (str1[m] == str1[i])
{
s[q] = str1[i-1];
s[q+1] = str1[m-1];
q = q + 2;
}
int a, k;
for (a = 1; a < n; a += 2)//对字符串的比较,然后定最后的排列顺序
for ( k = a + 2; k < n; k += 2)
{
if (s[a] > s[k])
{
swap(s[a], s[k]);
swap(s[a-1], s[k-1]);
}
if (k == n-1)
cout << s[a-1] << " is the MaJia of " << s[a] << endl;
}
cout << s[n-2] << " is the MaJia of " << s[n-1] << endl;
cout << endl;
}
}