#include <bits/stdc++.h>
using namespace std;
string s[55];
string m[14]={"3","4","5","6","7","8","9","10","J","Q","K","A","2","Joker"};
bool cmp(string a,string b)
{
int am,bm;
for(int i=0;i<14;i++)
{
if(m[i]==a) am=i;
if(m[i]==b) bm=i;
}
return am<bm;
}
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++)
cin>>s[i];
sort(s,s+n,cmp);
for(int i=0;i<n;i++)
cout<<s[i]<<endl;
return 0;
}