import java.util.*; import java.io.*; import java.math.*; public class Main { public static void main(String[] args) { char[] M = new char[] {'D','C','H','S'}; Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Data[] list = new Data[n]; for(int i = 0; i < n; i++) { String in = sc.next(); int m = 0; char c = in.charAt(0); switch(c) { case 'D': m = 0; break; case 'C': m = 1; break; case 'H': m = 2; break; case 'S': m = 3; break; } int num = 0; if(in.substring(1).equals("A")) num = 1; else if(in.substring(1).equals("T")) num = 10; else if(in.substring(1).equals("J")) num = 11; else if(in.substring(1).equals("Q")) num = 12; else if(in.substring(1).equals("K")) num = 13; else num = Integer.parseInt(in.substring(1)); list[i] = new Data(m,num,in); } Arrays.sort(list); System.out.print(list[0].out); for(int i = 1; i < n; i++) { System.out.print(" " + list[i].out); } System.out.println(); } static class Data implements Comparable { int mark; int number; String out; Data(int a, int b,String c) { mark = a; number = b; out = c; } @Override public int compareTo(Data o) { if(o.mark == this.mark) return this.number - o.number; return this.mark - o.mark; } } }