Cafebazaar

Assign each full-time developer and each critical application to a matching partner while maximizing total payoff, or report -1 if impossible.

Medium7GraphDynamic programmingBit manipulationNo attempts yetTime limit2sMemory limit512 MB

Problem

You are the product manager of Cafebazaar, an Android marketplace in Iran. As your one-year plan you have a list of applications to develop, and you have to assign a suitable developer to each application on the list. Cafebazaar has nn developers, some of them full time and the rest part time. Your list holds mm applications, some of them business critical and the rest ordinary applications.

Developers have different skills. A developer handles some applications well and cannot develop others at all. If developer ii is capable of developing application jj, then assigning her to that application in your plan gains you a payoff of xi,jx_{i,j}. You cannot assign a developer to an application she is not capable of developing. To save time and cost you assign each developer to at most one application, and at most one developer to each application.

A development plan is proper when both of the following hold. First, every full-time developer takes part in the plan, so nobody gets disappointed. Second, every critical application has a developer assigned to it, so you do not lose clients. In other words, each full-time developer is assigned to exactly one application, and each critical application has exactly one developer assigned to it. A proper plan may also assign part-time developers to applications, and it may assign developers to ordinary applications. Find a proper plan whose total payoff is maximum.

Input

The input holds several test cases. The first line of each test case has two space-separated integers nn and mm, the number of developers and the number of applications (1n,m1001 \le n, m \le 100). The next line starts with the number of full-time developers tt (0tn0 \le t \le n), followed by the indices of the tt full-time developers. Developers are indexed from 1 to nn. The next line starts with the number of critical applications ss (0sm0 \le s \le m), followed by the indices of the ss critical applications. Applications are indexed from 1 to mm. The next nn lines hold one line of information per developer. Line ii (1in1 \le i \le n) starts with the number of applications developer ii is capable of developing, did_i (0dim0 \le d_i \le m), followed by did_i pairs of integers ai,ja_{i,j} and xi,jx_{i,j} (1jdi1 \le j \le d_i, 1ai,jm1 \le a_{i,j} \le m, 1xi,j1061 \le x_{i,j} \le 10^6). Here ai,ja_{i,j} is the index of an application developer ii is capable of developing, and xi,jx_{i,j} is the payoff gained when developer ii develops application ai,ja_{i,j}. The same application index never appears twice in the list of one developer. The last line of the input is 0 0 and is not processed. The input holds at most 20 test cases.

Output

For each test case print on one line the maximum total payoff you can obtain with a proper plan. If there is no proper plan, print -1 on that line.