def n2p(x): if x in pictures: return pictures[x] else: return int(x) def m2p(x): return {'D': 0, 'C': 100, 'H': 200, 'S': 300}[x[0]] + n2p(x[1]) pictures = {'T': 10, 'J': 11, 'Q': 12, 'K': 13, 'A': 1} _ = input() ans = sorted(raw_input().split(), key=m2p) print(' '.join(map(str, ans)))