# Arup Guha
# 4/1/2020
# Solution to COP 2930 Individual Program #6: Votes

def main():

    # Open the file.
    myFile = open("votes.txt", "r")
    numVotes = int(myFile.readline())

    # Set up the dictionary.
    votes = {}

    # Safe since at least one vote is guaranteed.
    maxVotes = 1

    # Process each vote.
    for i in range(numVotes):
        name = myFile.readline().strip()
        
        # Already got a vote, just add 1, also update maxVotes
        if name in votes:
            votes[name] = votes[name] + 1
            maxVotes = max(maxVotes, votes[name])

        # Create a new entry with 1 vote.
        else:
            votes[name] = 1

    # Will store all the candidates by # of votes they received.
    votelists = []
    for i in range(maxVotes+1):
        votelists.append([])

    # Add each candidate into the appropriate votelist.
    for name in votes:
        votelists[votes[name]].append(name)

    # Go through lists from most votes to least.
    for i in range(maxVotes, 0, -1):

        # Skip these.
        if len(votelists[i]) == 0:
            continue

        # Sort it.
        votelists[i].sort()

        # Print each name, followed by # of votes.
        for j in range(len(votelists[i])):
            print(votelists[i][j],i)

# Run it!                 
main()
    
