#C8736. Book Rental Service Simulation

    ID: 52751 Type: Default 1000ms 256MiB

Book Rental Service Simulation

Book Rental Service Simulation

You are asked to simulate a book rental service. In this service, each book has a rental period and can only be rented when it is available. When a request is made, if a book is available at the request time, it will be rented immediately; otherwise, the book will be rented after its current rental period is extended by its own period.

For each dataset, the first line contains two integers N and M, where N is the number of books and M is the number of requests. The next N lines each provide a book's name and its rental period. Each of the following M lines describes a request with a request time, the number of books requested K, followed by K book names.

The fulfillment time for a request is defined as the maximum time at which any of the requested books is returned. Formally, if we denote the rental period of a book as \(T\), then the return time will be updated as follows: \[ available\_at = \begin{cases} request\_time + T & \text{if } available\_at \le request\_time, \\ available\_at + T & \text{otherwise.} \end{cases} \]

The input terminates with a line containing "0 0".

inputFormat

The input is read from stdin and consists of one or more datasets:

  1. The first line of each dataset contains two integers N and M separated by a space.
  2. The next N lines each contain a book's name (a string without spaces) and its rental period (an integer), separated by a space.
  3. The following M lines each describe a rental request. Each such line begins with an integer request_time and an integer K (the number of books in the request), followed by K book names.
  4. The end of the input is marked by a line with "0 0".

All input should be read from standard input.

outputFormat

For each request in a dataset, output a single line containing the fulfillment time (an integer) when the request will be fulfilled. If there are multiple datasets, separate the output of each dataset by a blank line. All output should be written to standard output.

## sample
5 4
HarryPotter 15
PrideAndPrejudice 10
TheGreatGatsby 8
ToKillAMockingbird 12
MobyDick 7
5 2 HarryPotter TheGreatGatsby
15 2 PrideAndPrejudice MobyDick
20 1 TheGreatGatsby
25 1 HarryPotter
0 0
20

25 28 40

</p>