l = [[input().split(),str] for str in "ABC"] l = sorted(l,key=lambda x: x[0][1]) l = sorted(l,key=lambda x: x[0][0],reverse=True) for x in l: print(x[1])