import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines N = int(readline()) cards = read().decode('utf-8').split() suit = list('DCHS') suit_rank = {x:i for i,x in enumerate(suit)} num = list('A23456789TJQK') num_rank = {x:i for i,x in enumerate(num)} cards.sort(key = lambda x: suit_rank[x[0]] * 100 + num_rank[x[1]]) print(' '.join(cards))