# Arup Guha
# 7/15/2025
# Data Struct Examples

from collections import deque
import heapq
import random

# Used for custom sorting.
class person:
    def __init__(self, n, v):
        self.name = n
        self.votes = v

    def __str__(self):
        return self.name+" : "+str(self.votes)

    # Makes more votes come first, then ties broken by alpha name.
    def __lt__(self, other):
        return self.votes > other.votes or (self.votes == other.votes and self.name < other.name)
        

# Use of stack.
def runStack():
    mystack = []
    mystack.append(3)
    mystack.append(4)
    print(mystack)
    x = mystack[-1]
    print("top of stack",x)
    x = mystack.pop()
    print("popped",x)
    print("size is",len(mystack))

def runQueue():

    # Create a queue.
    myq = deque()

    for i in range(20):

        temp = random.randint(1,10)

        # Enqueue step
        if len(myq) == 0 or temp < 7:

            x = random.randint(1, 100)
            print("enqueue",x)
            myq.append(x)

        # Dequeue step
        else:
            x = myq.popleft()
            print("dequeued",x)

    print(myq)

def runPQ():

    # Stacks of paper to merge into one.
    arr = [2,6,7,15,20,26]

    # This is an empty heap.
    myheap = []

    # Add everything into the heap.
    for x in arr:
        heapq.heappush(myheap, x)

    # Total cost of merging.
    res = 0
    
    # Keep going until 1 stack of papers.
    while len(myheap) > 1:

        # Two smallest stacks.
        x = heapq.heappop(myheap)
        y = heapq.heappop(myheap)
        print("merging",x,y)
        
        # Cost of merge.
        res += (x+y)

        # Add the merged stack back to the group.
        heapq.heappush(myheap, x+y)

    print("total cost is", res)

# Solves everywhere problem from Kattis, uses set.
def everywhere():

    # get number of cases.
    nC = int(input())

    # Process cases.
    for loop in range(nC):

        # Get number of items to read and create set.
        n = int(input())
        items = set()

        # Just add each item to the set.
        for i in range(n):
            x = input().strip()
            items.add(x)

        # This is what we want.
        print(len(items))

# Test of dictionary, custom sorting.
def votes():

    # Read number of votes.
    n = int(input())

    # Create dictionary.
    votes = {}

    # Loop through votes.
    for i in range(n):

        # Get vote.
        name =input().strip()

        # We've seen this before, so add 1.
        if name in votes:
            votes[name] += 1

        # New entry set it to 1 vote.
        else:
            votes[name] = 1

    # Prints out tally
    for x in votes:
        print(x, votes[x])

    # Make a list out of the people votes pairs.
    mylist = []
    for x in votes:
        mylist.append(person(x, votes[x]))

    # Sort it.
    mylist.sort()

    # Print.
    for item in mylist:
        print(item)
                      

# Run votes.   
votes()
