Under construction

Still being built. Things may change

Grocery Shopping

Interview

Time limit1sMemory limit128 MB

Summary
For each grocery item, find its matching store item by case-insensitive name and add the club-card saving on the units actually bought.
Level

Easy3 of 10

Topics
Hash map, String, Simulation
Solved
No attempts yet

Problem

Vint Cerf and Bob Kahn, curious about the users of their network, decide to invite them all to a barbecue. Judging by the amount of traffic on the network, they are going to need a lot of food.

You are given the grocery store's inventory and their grocery list. The store's club card lets them buy some items at a cheaper price. Determine how much money they save in total by using the club card.

For each item on the grocery list, if the store carries it, they save (normal price - club price) on every unit they actually buy. The number of units bought is the smaller of the quantity desired mjm_j and the quantity in stock nin_i. In other words, the amount saved on that item is min⁡(mj,ni)×(pi−ci)\min(m_j, n_i) \times (p_i - c_i). An item on the list that the store does not carry cannot be bought and contributes nothing. The total saving is the sum over all items.

Input

The first line contains the number KK of data sets. Then KK data sets follow, each in the form below.

The first line of each data set contains two integers NN and MM (1≤N,M≤1001 \le N, M \le 100), where NN is the number of different items in the store and MM is the number of different items on the grocery list.

Then NN lines follow, each describing one store item: an integer nin_i (the quantity currently in stock), then the normal price pip_i and the club card price cic_i, then a single space followed by the item name sis_i. Prices range from $0.00 to $99.99 and are always written with two decimal places (for example, $3.00). A name may contain upper- and lower-case letters and spaces.

Then MM lines follow, each describing one item on the list: an integer mjm_j (the quantity desired), then a single space followed by the item name sjs_j.

An item on the list is the same as a store item if their names are equal, ignoring case. The names in the input have no extra leading or trailing whitespace.

Output

For each data set, output Data Set x: on a line by itself, where xx is the data set's number. On the next line, output the exact amount saved by using the club card, written with a leading $ and two decimal places (no commas). Separate consecutive data sets with a blank line.

Examples4

  1. Example 1

    Input
    1
    2 3
    3 $3.00 $3.00 Mango Sorbet
    2 $6.00 $4.50 TV dinner
    1 Mango Sorbet
    3 tv dinner
    1 nacho cheese
    
    Expected output
    Data Set 1:
    $3.00
    
  2. Example 2

    Input
    2
    1 1
    5 $10.00 $8.50 Apple
    2 apple
    2 2
    10 $5.00 $4.00 Bread
    3 $2.00 $1.00 Milk
    4 BREAD
    5 milk
    
    Expected output
    Data Set 1:
    $3.00
    
    Data Set 2:
    $7.00
    
  3. Example 3

    Input
    1
    1 1
    5 $2.50 $2.50 Water
    3 water
    
    Expected output
    Data Set 1:
    $0.00
    
  4. Example 4

    Input
    1
    1 2
    5 $9.99 $8.88 Cheese
    2 milk
    1 cheese
    
    Expected output
    Data Set 1:
    $1.11