m = "DCHS" n = "A23456789TJQK" input() print " ".join(sorted(raw_input().split(), key = lambda x:(m.index(x[0]),n.index(x[1]))))