This page is still under construction.

Parts of this page are still being built. What you see may change.

Reaction

Time limit8sMemory limit512 MB

Summary
Given counts of colored positive and negative spheres and a list of reactions, choose disjoint reacting pairs to maximize the total item sale value.
Level

Medium7 of 10

Topics
Dynamic programming, Graph, Greedy, Implementation
Solved
No attempts yet

Problem

You are the hero of a role playing game, and the king has asked you to defeat the monsters that threaten people's lives.

After a long journey with your companions, you have reached the town closest to the final dungeon, where the head of the monsters dwells. People have told you that the head monster strikes with powerful arms, casts strong spells, and has many special abilities, and that your party would be easily wiped out by the severe damage without powerful equipment. So you need to prepare equipment.

You also have a number of magical spheres collected during the journey. The spheres are useless on their own, but a reaction spell can turn them into special items. You can sell those special items to shops in the town for money, then use that money to buy equipment.

The reaction spell works as follows. Each sphere has a color and either a positive attribute or a negative attribute. You choose one sphere with a positive attribute and one with a negative attribute, and cast the spell on the two spheres. The spheres then react and produce a set of special items. The spheres disappear after the reaction. The set of items you obtain depends only on the colors of the two spheres. You can cast the spell as many times as you want, but of course you cannot cast it on spheres that have disappeared. Also, not every pair of sphere colors reacts.

Naturally, you want to earn as much money as possible. So you must choose the pairs of spheres carefully before casting the spell. You are also an excellent programmer, so writing a program that finds the best way should be an easy task.

Your task is now clear: write a program and get ready for the battle with the head monster!

Input

The input is a sequence of datasets. Each dataset is formatted as follows:

N- N+
Number of available spheres
Definition of items
Definition of reactions

The first line contains two integers N- and N+, the numbers of different colors of spheres with negative and positive attributes, respectively. The rest of the dataset is divided into three parts.

The first part describes the number of available spheres. This part has the following format:

K1- K2- ... KN--
K1+ K2+ ... KN++

K**i- is the number of spheres of the i-th color with negative attribute, and K**i+ is the number of spheres of the i-th color with positive attribute.

The second part contains the definition of items. This part is formatted as follows:

M
A1 P1
...
AM PM

Here, M is the number of items that can be produced. Each of the following M lines contains a string Ai and an integer Pi, the name and the selling price of the i-th item respectively.

The last part gives the details of reactions. This part has the following format:

L
I1- I1+ NJ1 J1,1 ... J1,NJ1
...
IL- IL+ NJL JL,1 ... JL,NJL

The first line contains an integer L, the number of pairs of sphere colors that can react. Each of the next L lines starts with two integers I**i- and Ii+, which denote the colors of the negative and positive spheres respectively. The next integer NJi is the number of items produced by the reaction between spheres I**i- and I**i+. The line is then followed by NJi strings, each an item name.

You may assume all the following: 1 ≤ N-, N+ ≤ 100; 1 ≤ K**i-, K**i+ ≤ 100; 1 ≤ M ≤ 100; 1 ≤ Pi ≤ 100; 1 ≤ L ≤ 100; 1 ≤ NJi ≤ 10. You may also assume that an item name consists only of alphanumeric characters and its length does not exceed ten.

The end of input is represented by a line with two zeros. This line is not part of any dataset.

Output

For each dataset, print a line containing the maximum possible total selling price.

Examples1

  1. Example 1

    Input
    2 2
    1 1
    1 1
    4
    A 10
    B 20
    C 30
    D 40
    4
    1 1 3 A A A
    1 2 2 B C
    2 1 1 D
    2 2 3 A A B
    2 2
    1 2
    2 1
    3
    Scroll 50
    Bastard 100
    Heal100 10
    3
    1 1 1 Scroll
    2 1 1 Bastard
    2 2 1 Heal100
    0 0
    
    Expected output
    90
    200