def sort_names(names):
"""Sorts names by length, then alphabetically.
Args:
names: A list of names.
Returns:
A list of names sorted by length, then alphabetically.
"""
names.sort(key=lambda x: (len(x), x)) # Sort by length, then alphabetically
return names
# Get the number of names
n = int(input())
# Get the names
names = []
for _ in range(n):
names.append(input())
# Sort the names
sorted_names = sort_names(names)
# Print the sorted names
for name in sorted_names:
print(name)