#K61817. Library Book Management

    ID: 31394 Type: Default 1000ms 256MiB

Library Book Management

Library Book Management

You are given a simple library system that supports three operations:

  • ADD: Add a new book with a unique id and a list of topics.
  • REMOVE: Remove the book with the given id from the system. If the book does not exist, ignore the operation.
  • QUERY: Given a list of topics, print a sorted list (in ascending order) of book ids that cover all the given topics. If no books satisfy the query, print an empty line.

The input begins with an integer, n, representing the total number of operations. The next n lines each contain an operation in one of the following formats:

  • ADD book_id topic1 topic2 ...
  • REMOVE book_id
  • QUERY topic1 topic2 ... (It is possible for a QUERY command to have no topics, in which case simply output an empty line.)

For each QUERY operation, print the output on its own line. The book ids must be printed in increasing order separated by a space. If no books satisfy the query, print a blank line.

Note: The library initially is empty and book ids are guaranteed to be unique when added.

inputFormat

The first line of input contains an integer n, the number of operations.

The following n lines each contain an operation:

  • ADD book_id topic1 topic2 ...
  • REMOVE book_id
  • QUERY topic1 topic2 ...

Read from standard input.

outputFormat

For every QUERY operation in the input, print one line that contains the sorted list of book ids (in ascending order) which have all the specified topics. The ids should be separated by a single space. If no book matches, print an empty line.

Write to standard output.

## sample
9
ADD 1 science physics
ADD 2 math physics
ADD 3 science chemistry
ADD 4 history
QUERY physics
QUERY physics math
REMOVE 2
QUERY physics
QUERY physics math
1 2

2 1

</p>