import java.io.*; import java.util.*; public class Main { static HashMap change = new HashMap<>(); public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); String order = "A23456789TJQKDCHS"; int idx = 0; for (char c : order.toCharArray()) { change.put(c, idx++); } 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; int value; public Card(String name) { this.name = name; value = change.get(name.charAt(0)) * 20 + change.get(name.charAt(1)); } public int compareTo(Card another) { return value - another.value; } public String toString() { return name; } } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }