Under construction

Still being built. Things may change

Programming Contests

Time limit1sMemory limit128 MB

Summary
Given problems that can each go to one of several named contests each needing a fixed count, find the maximum subset of contests that can be simultaneously fully staffed, requiring bitmask search combined with flow-style feasibility checks.
Level

Hard8 of 10

Topics
Bit manipulation, Graph, Brute force, Combinatorics
Solved
No attempts yet

Problem

Every programming contest features a different kind of problem. For example, TopCoder caps the maximum array size at 50. Also, IOI is a contest that children take part in, so Suffix Tree problems do not appear.

Woohyun writes problems for NN distinct programming contests. Each contest needs a different number of problems. For instance, an ACM-ICPC-style contest usually needs 10 problems, while a TopCoder SRM needs 5.

Woohyun has prepared MM problems in advance and, for each problem, has decided which contests it may be used in.

Assuming every contest is to be held at the same time, determine the maximum number of contests that can actually run. To hold a contest, all of its required problems must be filled, and each problem may be used in at most one contest.

Input

The input consists of several test cases.

The first line of each test case contains the number of contests NN and the number of prepared problems MM (1≤N≤151 \le N \le 15, 0≤M≤500 \le M \le 50).

Each of the next NN lines contains a contest name and the number of problems that contest needs to run. A contest name consists of upper- and lower-case letters and digits, is at most 100 characters long, and is case-sensitive. The required problem count is at most 100.

Each of the next MM lines lists, separated by spaces, the names of the contests in which that problem may be used. If a problem cannot be used in any contest, its line is empty.

The last line of the input contains two 0s and is not processed.

Output

For each test case, print on one line the maximum number of contests that can be held simultaneously, together with the case number. If the answer for the kk-th test case is cc, print it in the format Case #k: c.

Examples1

  1. Example 1

    Input
    4 5
    IOI 3
    IPSC 2
    TopCoder 2
    SEERC 10
    IOI
    IPSC TopCoder
    IOI IPSC
    IOI IPSC
    TopCoder SEERC
    1 1
    SampleContest 1
    SampleContest
    0 0
    
    Expected output
    Case #1: 2
    Case #2: 1