import math._ object Main { def compareSuit(a: Char, b: Char): Int = { val xs = Array('D', 'C', 'H', 'S') xs.indexOf(a).compareTo(xs.indexOf(b)) } def compareNum(a: Char, b: Char): Int = { def toNum(c: Char) = c match { case 'A' => 1 case 'T' => 10 case 'J' => 11 case 'Q' => 12 case 'K' => 13 case _ => c.toInt - '0'.toInt } toNum(a).compareTo(toNum(b)) } def main(args: Array[String]) { val sc = new java.util.Scanner(System.in) val n = sc.nextInt val xs = Array.fill(n)(sc.next) val ys = xs.sortWith((a, b) => { val s = compareSuit(a(0), b(0)) if (s == 0) compareNum(a(1), b(1)) <= 0 else s <= 0 }) println(ys.mkString(" ")) } }