#include<bits/stdc++.h>
using namespace std;
struct STU
{
string s;
int y,m,d;
};
STU stu[105];
int n;
int main()
{
cin>>n;
for(int i=0;i<n;i++)
{
cin>>stu[i].s>>stu[i].y>>stu[i].m>>stu[i].d;
}
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
if(stu[i].y>stu[j].y)
{
swap(stu[i],stu[j]);
cout<<"Y";
}
if(stu[i].y==stu[j].y)
{
if(stu[i].m>stu[j].m)
{
swap(stu[i],stu[j]);
cout<<"M";
}
if(stu[i].m==stu[j].m)
{
if(stu[i].d>stu[j].d)
{
swap(stu[i],stu[j]);
cout<<"D";
}
}
}
}
}
for(int i=0;i<n;i++)
{
cout<<stu[i].s<<endl;
}
return 0;
}