IntermediateLists & Dictionaries25 points

Top K trending hashtags of the match

During a cricket final watched by millions, a sports network reads a stream of hashtags posted by fans and wants to display the K most used hashtags on a live scoreboard.

Task: the first line holds n and K (1 ≤ K ≤ 20, 1 ≤ n ≤ 400). Each of the next n lines holds one hashtag (lowercase letters, no spaces). Rank hashtags by number of posts, highest first; if two hashtags have the same count, the one that comes first alphabetically ranks first. Print the top K as lines hashtag count. If there are fewer than K distinct hashtags, print all of them.

Sample test

Input

8 2
six
wicket
six
runout
wicket
six
runout
four

Expected output

six 3
runout 2

Your answer is also checked against 4 hidden tests.

Need a hint?

Count with a dictionary, then sort by a key that puts bigger counts first and breaks ties by name.