N = int(input()) S = list(input().split()) a = "DCHS" b = "A23456789TJQK" c = [] for i in range(N): u = a.index(S[i][0]) v = b.index(S[i][1]) c.append((u, v, S[i])) c = sorted(c) print(" ".join([c[i][2] for i in range(N)]))