Cocktails

No attempts yetTime limit1sMemory limit128 MB

Problem

Angelo, a famous Italian cocktail mixer, has finally opened a new bar in Rome and is about to list his company "Angelo's Cocktail Mix" (ACM) on the stock exchange. To give the speculators something to chew on, he wants to publish statistics about which cocktails he mixes most often.

Angelo collected data about the cocktails ordered on a typical evening. Your task is to compute the "top ten favourite cocktails" list, showing each cocktail together with its price.

The ranking is primarily by how often a cocktail is mixed. When two or more cocktails are mixed the same number of times, the one that appears earlier in Angelo's recipe book ranks higher. A cocktail may appear in the top ten only if it was mixed at least once.

Angelo processes the orders as follows:

  • If a customer orders a cocktail that is not in the recipe book, the order is ignored.
  • Angelo never mixes the same cocktail twice in a row. If the ordered cocktail is the one he has just mixed, he moves to the next cocktail in the book instead (after the last cocktail he wraps around to the first).
  • If a cocktail cannot be mixed because not enough of its ingredients are left, he again moves on to the next cocktail in the book (wrapping around when necessary), always skipping the cocktail he has just mixed.
  • If no cocktail in the book can be mixed for the current order, the order is ignored.

When a cocktail is mixed, the required amount of each of its ingredients is consumed.

The price of a cocktail is 3×W+100003 \times W + 10000 Lira, where WW is the weighted ingredient price: the sum, over all ingredients in the recipe, of the ingredient amount (in centi-liters) multiplied by that ingredient's price per centi-liter. The extra 10000 Lira covers mixing and service.

Input

The first line contains the number of scenarios. Scenarios are numbered starting from 1. Each scenario consists of three tables.

  1. Angelo's recipe book:

    • One line with the total number of cocktails in the book.
    • Then, for each cocktail, two lines: the first with the cocktail's name and its number of distinct ingredients; the second with the ingredient names and their amounts in centi-liters, separated by spaces.
  2. The table of available ingredients:

    • One line with the number of available ingredients.
    • Then, for each ingredient, one line with its name, the available amount in centi-liters, and its price in Lira per centi-liter.
  3. The table of orders:

    • One line with the number of orders.
    • Then, for each order, the name of the ordered cocktail on its own line.

Cocktail and ingredient names consist only of alphanumeric characters and underscores (no whitespace). All numeric values are integers of at most 10000.

Output

For each scenario ii, print a line "Scenario ii top cocktails:", followed by the top ten list in rank order from 1 to 10.

For each entry, print one line containing the rank, the cocktail name, the number of times it was mixed, and its price, each separated by a single space.