Under construction

Still being built. Things may change

Choosing the Final Die's Face Values

Time limit1sMemory limit128 MB

Summary
Each test case fixes several dice and asks for the r face values of the last die so that m given sums appear with exactly the given counts, choosing the lexicographically smallest set.
Level

Medium7 of 10

Topics
Dynamic programming, Prefix sum, Brute force, Math
Solved
No attempts yet

Problem

Phil Kropotnik is a game designer, and one problem he often runs into is deciding which set of dice to use in a game. Many modern games call for non-traditional dice — dice with more or fewer sides than the usual 6-sided cube.

Phil usually fixes the face values of all but the last die, then works out what values to put on the last die so that certain sums can be rolled in certain numbers of ways. (Instead of dealing with probabilities, he works directly with the total number of different ways a given sum can be obtained by rolling all the dice.) He currently does this by hand; your task is to automate it.

When several dice are rolled together, the number of ways to obtain a sum is the number of distinct combinations of one face from each die. If the same value appears on several faces, those faces are treated as distinct.

For example, suppose Phil already has a 4-sided die with face values 1, 10, 15, and 20, and he wants to label a 5-sided die so that, using both dice, there are (a) 3 ways to obtain a sum of 2, (b) 1 way to obtain 3, (c) 3 ways to obtain 11, (d) 4 ways to obtain 16, and (e) 1 way to obtain 26. He should label the faces of the 5-sided die 1, 1, 1, 2, and 6. (For instance, the sum 16 can be made as 10 + 6 or as 15 + 1; since there are three '1' faces on the second die, this gives 4 different ways in total.)

Given the already-specified dice, the number of faces on the last die, and the desired (sum, count) requirements, determine the face values of the last die.

Input

The input consists of several input sets. Each input set begins with a single line containing an integer nn, the number of dice already specified. Each of the next nn lines describes one of these dice: the line starts with an integer ff (the number of faces on the die), followed by ff integers giving the value of each face.

The last line of each set has the form

r m v1 c1 v2 c2 v3 c3 ··· vm cm

where rr is the number of faces required on the unspecified die, mm is the number of sums of interest, v1,…,vmv_1, \dots, v_m are those sums, and c1,…,cmc_1, \dots, c_m are the required numbers of different ways to obtain each corresponding sum.

The input satisfies: 1≤n≤201 \le n \le 20, 3≤f≤203 \le f \le 20, 1≤m≤101 \le m \le 10, and 4≤r≤64 \le r \le 6. The face values of all dice, both the specified ones and the unknown die, are integers from 11 to 5050. The values viv_i and cic_i are all non-negative and strictly less than the maximum value of a 32-bit signed integer.

The last input set is followed by a line containing a single 00, which should not be processed.

Output

For each input set, print a single line containing either the phrase Final die face values are followed by the rr face values in non-descending order, or Impossible if no die meets the specification.

If several dice satisfy the requirements, choose the one whose smallest face value is smallest; if there is still a tie, choose the one whose second-smallest face value is smallest, and so on.

Examples4

  1. Example 1

    Input
    1
    4 1 10 15 20
    5 5 2 3 3 1 11 3 16 4 26 1
    1
    6 1 2 3 4 5 6
    6 3 7 6 2 1 13 1
    4
    6 1 2 3 4 5 6
    4 1 2 2 3
    3 3 7 9
    8 1 4 5 9 23 24 30 38
    4 4 48 57 51 37 56 31 63 11
    0
    
    Expected output
    Final die face values are 1 1 1 2 6
    Impossible
    Final die face values are 3 7 9 9
    
  2. Example 2

    Input
    1
    3 1 1 1
    4 1 5 6
    0
    
    Expected output
    Final die face values are 1 1 4 4
    
  3. Example 3

    Input
    2
    3 1 1 1
    3 2 2 2
    4 1 7 9
    0
    
    Expected output
    Final die face values are 1 1 1 4
    
  4. Example 4

    Input
    1
    3 1 1 1
    5 2 3 3 5 6
    0
    
    Expected output
    Final die face values are 1 1 2 4 4