#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for(int(i)=0;(i)<(n);++(i))
#define in(T,V) T V;cin>>V;

int f0(char c){
    switch(c){
    case 'D': return 0;
    case 'C': return 1;
    case 'H': return 2;
    case 'S': return 3;
    }
    return -1;
}
int f1(char c){
    switch(c){
    case 'A': return 1;
    case 'T': return 10;
    case 'J': return 11;
    case 'Q': return 12;
    case 'K': return 13;
    }
    return c - '0';
}

int main(){
    vector<string> v;
    in(int,N);
    REP(i,N){
        in(string,s); v.push_back(s);
    }
    sort(v.begin(), v.end(), [](const string &s, const string &t){
        int s0=f0(s[0]), t0=f0(t[0]);
        if(s0!=t0)return s0<t0;
        int s1=f1(s[1]), t1=f1(t[1]);
        return s1<t1;
    });
    REP(i,N){
        cout << v[i] << (i<N-1?" ":"\n");
    }
}