import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Card[] cards = new Card[n]; for (int i = 0; i < n; i++) { cards[i] = new Card(sc.next()); } Arrays.sort(cards); StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { if (i > 0) { sb.append(" "); } sb.append(cards[i]); } System.out.println(sb); } static class Card implements Comparable { String name; char suite; int mark; int value; static char[] marks = new char[]{'D', 'C', 'H', 'S'}; public Card(String name) { this.name = name; suite = name.charAt(0); for (int i = 0; i < marks.length; i++) { if (marks[i] == suite) { mark = i; break; } } char tmp = name.charAt(1); if (tmp == 'A') { value = 1; } else if (tmp == 'J') { value = 11; } else if (tmp == 'T') { value = 10; } else if (tmp == 'Q') { value = 12; } else if (tmp == 'K') { value = 13; } else { value = tmp - '0'; } } public int compareTo(Card another) { if (mark == another.mark) { return value - another.value; } else { return mark - another.mark; } } public String toString() { return name; } } }