#define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair i_i; typedef pair ll_i; typedef pair d_i; typedef pair ll_ll; typedef pair d_d; struct edge { int u, v; ll w; }; ll MOD = 1000000007; ll _MOD = 1000000009; double EPS = 1e-10; int INF = INT_MAX / 10; int main() { int N; cin >> N; vector s(N); for (int i = 0; i < N; i++) { cin >> s[i]; if (s[i][0] == 'D') s[i][0] = 'A'; if (s[i][1] == 'J') s[i][1] = 'a'; if (s[i][1] == 'Q') s[i][1] = 'b'; } sort(s.begin(), s.end()); for (int i = 0; i < N; i++) { if (s[i][0] == 'A') s[i][0] = 'D'; if (s[i][1] == 'a') s[i][1] = 'J'; if (s[i][1] == 'b') s[i][1] = 'Q'; cout << s[i] << (i + 1 < N ? ' ' : '\n'); } }