Problem

18 /19


Abbreviation

Problem

An email signed with an abbreviation (the first letters of the sender's last name, first name and patronymic (hereinafter referred to as the full name)) has been received by e-mail. The abbreviation was unfamiliar. There is a list of all the alleged senders, taken from previously received letters, among which there are no more than 10 different people with this abbreviation.
It is proposed to write an efficient program, including memory usage, that will determine all possible recipients of – people whose full name can be reduced to the desired abbreviation. Full names should be given in descending order of their frequency of occurrence in the list.
An abbreviation (a string consisting of three uppercase Latin letters) is given as input to the program in the first line. The second line contains the number N – the number of full names received as a result of mail analysis, not all of them fit the specified abbreviation. The N value can be very large. Each of the following N lines contains three words: last name, first name, patronymic of the corresponding person. Words are separated by a single space. There are no spaces at the end or beginning of the string. All words are written in capital Latin letters. The length of the full name does not exceed 100 characters. It is guaranteed that there is at least one person with the required abbreviation.

Sample input
IPI
4
IVANOV PETR IVANOVICH
PETROV IVAN IVANOVICH
IVANOV PETR IVANOVICH
ILYIN PETR ILYICH

The program should display the intended senders of the email, indicating the frequency of their occurrence in the list (in descending order of frequency).

Sample output for the example input above
IVANOV PETR IVANOVICH 2
ILYIN PETR ILYICH 1