# Arup Guha
# 12/7/2024
# Cups...custom sorting, sort of!

def islowercase(s):
    for ch in s:
        if ch < 'a' or ch > 'z':
            return False
    return True

# Number of items.
n = int(input())

# Put cups in here.
# will be stored as [diameter, color]
cups = []

for i in range(n):

    toks = input().split()

    # Color first
    if islowercase(toks[0]):
        cups.append([2*int(toks[1]), toks[0]])

    # Diameter first
    else:
        cups.append([int(toks[0]), toks[1]])

# Sort
cups.sort()

# Output colors only.
for x in cups:
    print(x[1])
                    

