#C4124. Counting Books by Genre

    ID: 47628 Type: Default 1000ms 256MiB

Counting Books by Genre

Counting Books by Genre

Emma is organizing a library of her favorite books and wants to track how many books she owns for each genre. Each book belongs to exactly one genre. You are given several test cases. For each test case, you will be provided with a list of recognized genres and a list of book genres. Count only the books that belong to the given genres and output the count for each genre (only if at least one book is recorded) in alphabetical order. Each test case result should be separated by a blank line.

Note: The input is read from standard input (stdin) and the output should be printed to standard output (stdout). If a test case has no valid books, output an empty result for that test case.

The problem may involve string manipulation and basic counting, and it is recommended to use appropriate data structures for tracking counts.

inputFormat

The input begins with an integer T, indicating the number of test cases. For each test case, the following is provided:

  • An integer G representing the number of genres.
  • A single line containing G space-separated strings, the names of the genres.
  • An integer B representing the number of books.
  • A single line containing B space-separated strings, each representing the genre of a book. (If B is 0, this line will be empty.)

All input is provided via standard input (stdin).

outputFormat

For each test case, output the genres (that had at least one matching book) and their counts in alphabetical order. Each genre and count should be printed on its own line in the format: Genre: count. Separate results of different test cases with an empty line. The output should be printed to standard output (stdout).

## sample
2
3
Fantasy ScienceFiction Horror
5
Fantasy Horror Fantasy Fantasy Horror
2
Romance Thriller
4
Romance Romance Thriller Romance
Fantasy: 3

Horror: 2

Romance: 3 Thriller: 1

</p>