#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
char C1[4] = {'D','C','H','S'};
char C2[13] = {'A','2','3','4','5','6','7','8','9','T','J','Q','K'};
bool comp(const string&a,string&b){
    for(int i = 0; 4 > i; i++){
        if(C1[i] == a[0] && C1[i] == b[0]){
            for(int j = 0; 13 > j; j++){
                if(C2[j] == a[1]){
                    return 1;
                }else if(C2[j] == b[1]){
                    return 0;
                }
            }
        }else if(C1[i] == a[0]){
            return 1;
        }else if(C1[i] == b[0]){
            return 0;
        }
    }
}
int main(){
    int n;cin>>n;
    vector<string> s(n);
    for(int i = 0; n > i; i++){
        cin>>s[i];
    }
    sort(s.begin(),s.end(),comp);
    for(int i = 0; n > i; i++){
        cout << s[i];
        if(i+1 != n)cout << " ";
    }
    cout << endl;
}