#K65432. Library Inventory Sorting

    ID: 32196 Type: Default 1000ms 256MiB

Library Inventory Sorting

Library Inventory Sorting

You are given a library inventory consisting of a set of genres and a list of book donations. Each donation provides the genre and the title of the donated book. Your task is to sort and display the library inventory in the following order:

  • Sort the genres in lexicographical order (alphabetically).
  • For each genre, sort the list of donated book titles in lexicographical order.

The output should list each genre followed by its associated sorted book titles, each on a new line.

Note: The input is read from stdin and the output is printed to stdout. Use LaTeX for any mathematical formulas (if needed).

inputFormat

The input is given in the following format:

N M
G1 G2 ... GN
genre1 book_title1
genre2 book_title2
... (M lines in total)

Where:

  • N is an integer representing the number of unique genres.
  • M is an integer representing the number of book donations.
  • The second line contains N genre names separated by spaces.
  • Each of the following M lines contains two strings: the genre name and the book title donated (titles will not contain spaces, but may contain hyphens).

outputFormat

Output the sorted inventory to stdout, where each line contains a genre or a book title. The genre is printed first, followed by its sorted book titles in subsequent lines. There should be no extra spaces or blank lines in the output.

## sample
3 4
fantasy science-fiction mystery
fantasy eragon
science-fiction dune
mystery sherlock
fantasy harry-potter
fantasy

eragon harry-potter mystery sherlock science-fiction dune

</p>