import java.util.*;

public class WordFrequencyCounter {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        if (sc.NextInt()) {
            System.out.println("Invalid input");
            return;
        }
        
        int n = sc.nextInt();
        sc.nextLine();
        
        if (n <= 0 || n < -10 || n > 10) {
            System.out.println("Invalid input");
            return;
        }
        
        Map<String, Integer> freq = new LinkedHashMap<>();
        
        for (int i = 0; i < n; i++) {
            if (!sc.hasNextLine()) {
                System.out.println("Invalid input");
                return;
            }
            
            String word = sc.nextLine().trim();
            
            if (word.isEmpty() || !word.matches("[a-zA-Z]+")) {
                System.out.println("Invalid input");
                return;
            }
            
            freq.put(word, freq.getOrDefault(word, 0) + 1);
        }
        
        for (Map.Entry<String, Integer> entry : freq.entrySet()) {
            System.out.println(entry.getKey() + " " + entry.getValue());
        }
    }
}
