#K74682. Restock Order Determination

    ID: 34252 Type: Default 1000ms 256MiB

Restock Order Determination

Restock Order Determination

You are given a list of product IDs that require restocking, along with detailed genre information. Each genre has a name and a list of product IDs associated with it. Additionally, you are provided a priority list of genres. Your task is to determine the order in which the products should be restocked.

For each genre in the given priority (from highest to lowest), select the product IDs (from the provided list) that belong to the genre. Each selected group should be sorted in ascending order. Then, concatenate the groups following the order of genres in the priority list. If the priority list is empty, the result should be empty.

More formally, let \(P\) be the set of product IDs, and for a given genre \(g\) (with product set \(S_g\)), output the sorted list of \(P \cap S_g\). Process each genre in the order given by the priority list.

inputFormat

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

  1. An integer \(n\) representing the number of products.
  2. \(n\) space-separated integers representing the product IDs.
  3. An integer \(m\) representing the number of genres.
  4. \(m\) lines follow, each line describes a genre. The line starts with the genre name followed by one or more space-separated product IDs associated with that genre.
  5. An integer \(p\) representing the number of genres in the priority list.
  6. \(p\) lines follow, each line contains a genre name, indicating its priority order.

If \(p = 0\), then the output should be empty.

outputFormat

Output the final restock order as a sequence of space-separated product IDs in one line on standard output (stdout). If no product is to be restocked, output an empty line.

## sample
5
1002 1003 1001 1005 1004
3
Electronics 1001 1002
Clothing 1003 1004 1005
Sports 1006 1007
3
Electronics
Clothing
Sports
1001 1002 1003 1004 1005

</p>