// Arup Guha
// 8/18/2026
// Example for COP 3330 using a TreeMap
// This example processes votes and prints out the current standings (sorted in
// alphabetical order by name) of the votes currently received.

/*** Edited in class on 11/2/2026 to show a try-catch of an
     InputFormatException and also to print out all winners at the
	 end via the TreeMap in sorted alpha order.
***/

import java.util.*;

public class votes_inclass {

	public static void main(String[] args) {
	
		// Get number of commands.
		Scanner stdin = new Scanner(System.in);
		int numCmd = stdin.nextInt();

		// Added to store winning # of votes.
		int maxVotes = 0;
		
		// Will store all votes here.
		TreeMap<String,Integer> myMap = new TreeMap<String,Integer>();
	
		// Process commands.
		int loop = 0;
		while (loop < numCmd) {
		
			// Get type.
			int type = -1;
			
			// Here we put statments that might cause an issue.
			try {
				type = stdin.nextInt();
			}
			
			// Here we list the exception that we are trying to handle.
			catch (InputMismatchException e) {
				
				// How it prints.
				System.out.println(e);
				
				// This is more detailed, what you get when your code crashes.
				e.printStackTrace();
				
				// Just showing I can do this.
				System.out.println("Try again");
				
				// This is weird, we still need to consume the token.
				stdin.next();
				
				// Skip stuff below.
				continue;
			}
			
			// Processing adding a vote.
			if (type == 1) {
			
				// Get the name.
				String name = stdin.next();
				
				// Just add one vote.
				if (myMap.containsKey(name))
					myMap.put(name, myMap.get(name)+1);
				
				// First vote, just place it!
				else	
					myMap.put(name, 1);
				
				// Update best...
				maxVotes = Math.max(maxVotes, myMap.get(name));
			}
			
			// Print status.
			else {
				for (String candidate: myMap.keySet())
					System.out.println(candidate+" "+myMap.get(candidate));
				System.out.println();
			}
			
			// For proper # of loop useful loop iterations.
			loop++;
		}
		
		// Print Winners.
		System.out.println("Winning # of votes = "+maxVotes);
		System.out.println("Winner(s) are:");
		
		// Go through all, screen the ones that have maxVotes # of votes.
		for (String candidate: myMap.keySet())
			if (myMap.get(candidate)  == maxVotes)
				System.out.println(candidate);
	}
}