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