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