import java.io.*; import java.util.*; class Main { public static void out (Object o) { System.out.println(o); } public static int getNum (char c) { return c == 'A' ? 1 : c == 'T' ? 10 : c == 'J' ? 11 : c == 'Q' ? 12 : c == 'K' ? 13 : c - '0'; } public static int getSuitNum (char c) { return c == 'D' ? 0 : c == 'C' ? 1 : c == 'H' ? 2 : 3; } public static boolean comp (String s , String t) { char s0 = s.charAt(0); char t0 = t.charAt(0); if (s0 != t0) { return getSuitNum(s0) > getSuitNum(t0); } char s1 = s.charAt(1); char t1 = t.charAt(1); return getNum(s1) > getNum(t1); } public static void sort (String[] cards) { int len = cards.length; String[] ret = new String[len]; for (int i = 0; i < len; i++) { String min = ""; int minNum = -1; for (int j = 0; j < len; j++) { if (cards[j].equals("none")) continue; if (minNum == -1 || comp(min , cards[j])) { min = cards[j]; minNum = j; } } ret[i] = min; cards[minNum] = "none"; //out(Arrays.toString(cards)); } System.out.print(ret[0]); for (int i = 1; i < len; i++) System.out.print(" " + ret[i]); out(""); } public static void main (String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String[] cards = br.readLine().split(" "); sort(cards); } }