#C9930. Organizing Hobbies

    ID: 54078 Type: Default 1000ms 256MiB

Organizing Hobbies

Organizing Hobbies

You are given a list of hobbies and a list of people. Each person is described by a string in the format "name age gender hobby". Your task is to organize the people according to their hobbies. For each hobby (in the given order), list the people who practice that hobby sorted primarily by ascending age and, in case of ties, by lexicographical order of their names.

The sorting criteria can be expressed in LaTeX as: $$sort = (\text{age}, \text{name})$$

After organizing, your program should output each hobby on a separate line, followed by the details of each person (name, age, gender) on separate lines immediately after the hobby. If a hobby has no person associated with it, just output the hobby name.

inputFormat

The input is read from standard input (stdin) and the format is as follows:

  1. The first line contains an integer HH, the number of hobbies.
  2. The second line contains HH strings representing the hobbies, separated by spaces.
  3. The third line contains an integer PP, the number of people.
  4. The following PP lines each contain a person's details in the format: "name age gender hobby".

outputFormat

For each hobby (in the order provided in the input), print the hobby on a separate line. Then, for each person who practices that hobby, print their details in the format "name age gender" on a new line. The people should be listed in order of increasing age and, if ages are the same, in lexicographic order of their names. Output is to stdout.## sample

3
Running Swimming Chess
6
Alice 24 F Running
Bob 22 M Swimming
Charlie 23 M Running
Diana 24 F Chess
Eva 24 F Running
Frank 21 M Chess
Running

Charlie 23 M Alice 24 F Eva 24 F Swimming Bob 22 M Chess Frank 21 M Diana 24 F

</p>