#!/usr/bin/python s = 'DCHS' t = 'A23456789TJQK' calc = lambda x: 13 * s.index(x[0]) + t.index(x[1]) n = int(raw_input()) arr = raw_input().split() res = sorted(map(lambda x: (calc(x), x), arr)) print ' '.join(e[1] for e in res)