// Arup Guha
// 7/15/2025
// Some Code for SI@UCF CP Lecture #1: Data Structures

import java.util.*;

public class datastruct {

	public static void main(String[] args) {
	
		votes();
	}
	
	// Test for stack.
	public static void runStack() {
	
		// Opposite process that was shown for Smashing Hit.
		//1, 3, 5, 6, 8, 12
		
		int[] arr = {3, 2, 9, 6, 7, 12, 11, 13, 6, 5, 2};
		
		ArrayDeque<Integer> stack = new ArrayDeque<Integer>();
		
		// Go through each item.
		for (int i=0; i<arr.length; i++) {
		
			// If stack is empty OR I am bigger than top add me.
			if (stack.size() == 0 || stack.peekFirst() < arr[i])
				stack.push(arr[i]);
		}
		
		// Pop off in reverse order.
		while (stack.size() > 0) 
			System.out.print(stack.pop()+" ");
		System.out.println();
	
	}
	
	// Test for Queue.
	public static void runQueue() {
		
		// Numbers on our calculator we've reached.
		ArrayDeque<Integer> q = new ArrayDeque<Integer>();
		q.offer(1);
		
		// Number of button presses to get to this number.
		int[] dist = new int[1025];
		Arrays.fill(dist, -1);
		dist[1] = 0;
		
		// Keep going until we get a distance for 100.
		while (dist[100] == -1) {
			
			int cur = q.poll();
			
			// Try adding 1.
			if (cur+1 < dist.length && dist[cur+1] == -1) {
				dist[cur+1] = dist[cur] + 1;
				q.offer(cur+1);
				System.out.println("Distance to "+(cur+1)+" is "+dist[cur+1]);
			}
			
			// Try multiplying by 2.
			if (2*cur < dist.length && dist[2*cur] == -1) {
				dist[2*cur]= dist[cur] + 1;
				q.offer(2*cur);
				System.out.println("Distance to "+(2*cur)+" is "+dist[2*cur]);
			}
			
		}
	}
	
	// Test for PriorityQueue.
	public static void runPQ() {
		
		int[] arr = {2,6,7,15,20,26};
		
		PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
		
		// Add everything to queue.
		for (int i=0; i<arr.length; i++)
			pq.offer(arr[i]);
		
		int res = 0;
		
		while (pq.size() > 1) {
			
			int x = pq.remove();
			int y = pq.remove();
			System.out.println("merging "+x+" and "+y);
			res += (x+y);
			pq.offer(x+y);
		}
		
		System.out.println("total cost was "+res);
		
		/*** For max queue, do this ***/
		//PriorityQueue<Integer> maxPQ = new PriorityQueue<>(Collections.reverseOrder());
		
	}
	
	// Test for HashSet.
	public static void everywhere() {
		
		Scanner stdin = new Scanner(System.in);
		int nC = stdin.nextInt();
		
		// Process cases.
		for (int loop=0; loop<nC; loop++) {
			
			int n = stdin.nextInt();
			HashSet<String> items = new HashSet<String>();
			
			for (int i=0; i<n; i++) {
				String place = stdin.next();
				items.add(place);
			}
			
			System.out.println(items.size());
		}
	}
	
	// Test for HashMap, Custom Sorting
	public static void votes() {
		
		Scanner stdin = new Scanner(System.in);
		int n = stdin.nextInt();
		
		// Creates the hash map.
		HashMap<String,Integer> myvotes = new HashMap<String,Integer>();
		
		// Read votes.
		for (int i=0; i<n; i++) {
			
			// Get this vote.
			String name = stdin.next();
			
			// This person previously got a vote.
			if (myvotes.containsKey(name))
				myvotes.put(name, myvotes.get(name)+1);
			else
				myvotes.put(name, 1);
		}
		
		// Print out everyone's votes.
		for (String name: myvotes.keySet())
			System.out.println(name+" "+myvotes.get(name));
		
		// Copy all mappings into an array.
		int idx = 0;
		person[] peeps = new person[myvotes.size()];
		for (String name: myvotes.keySet())
			peeps[idx++] = new person(name, myvotes.get(name));
		
		// Sort it!
		Arrays.sort(peeps);
		
		// Print to prove in sorted order.
		for (int i=0; i<peeps.length; i++)
			System.out.println(peeps[i]);
		
	}
}

class person implements Comparable<person> {
	
	public String name;
	public int votes;
	
	public person(String n, int v) {
		name = n;
		votes = v;
	}
	
	// Returns a negative integer if this comes before other.
	// Returns 0 if equal.
	// Returns a positive integer if this comes after other.
	public int compareTo(person other) {
		
		// First look at votes.
		if (this.votes != other.votes)
			return other.votes - this.votes;
		
		// Then name.
		return this.name.compareTo(other.name);
	}
	
	// String representation of the object.
	public String toString() {
		return name+" : "+votes;
	}
	
}