// Count frequency of words on stdin.  Mostly just for some map practice.

package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"sort"
	"strings"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	frequency := make(map[string]int)
	for scanner.Scan() {
		line := scanner.Text()
		for _, word := range strings.Fields(line) {
			frequency[word]++
		}
	}

	if err := scanner.Err(); err != nil {
		log.Println(err)
		os.Exit(1)
	}

	wordsByFrequency := make(map[int][]string)
	for word, count := range frequency {
		wordsByFrequency[count] = append(wordsByFrequency[count], word)
	}

	// Build a slice of the counts
	counts := make([]int, 0, len(wordsByFrequency)) // This usually will overallocate, but not always
	for count := range wordsByFrequency {
		counts = append(counts, count)
	}

	// This mess is to sort in reverse order.
	sort.Sort(sort.Reverse(sort.IntSlice(counts)))

	for _, count := range counts {
		words := wordsByFrequency[count]
		sort.Strings(words)
		for _, word := range words {
			fmt.Println(count, word)
		}
	}
}