Fridge Lock

No attempts yetTime limit1sMemory limit128 MB

Problem

A locksmith is called to open an unusual lock shown on a screen mounted on a fridge door.

The lock has $K$ concentric rings. Each ring displays a set of positive integers. The door opens when you pick exactly one number from every ring so that the $K$ chosen numbers satisfy $K$ clues.

Number the rings $R_1$ (outer) through $R_K$ (inner), and let $R_i$ also denote the number picked from ring $i$. Each clue is a linear equation in those picked values:

$$c_1 R_1 + c_2 R_2 + \cdots + c_K R_K = b$$

Each clue is given already parsed into its integer coefficients $c_1, c_2, \ldots, c_K$ and its right-hand side $b$, listed from the outer ring to the inner ring. For example (with three rings), the clue "the outer ring minus twice the middle ring equals $0$" is written as the coefficient line 1 -2 0 = 0.

Coefficients may be negative, zero, or positive. The clues always pin down exactly one valid selection.

Read the rings and the clues, then output the one selection that opens the lock, from the outer ring to the inner ring.

Input

The input contains several test cases. Each test case is given as:

  • A line with the integer $K$, the number of rings (and the number of clues), where $3 \le K \le 9$.
  • $K$ lines follow, one per ring from the outer ring to the inner ring. Each line lists the positive integers shown on that ring, separated by spaces. A ring shows between $3$ and $49$ numbers, and different rings may show different amounts.
  • $K$ more lines follow, one per clue. Each clue line contains $K$ integer coefficients, then the token =, then the integer right-hand side: c_1 c_2 ... c_K = b.

Every displayed ring number is between $1$ and $99$. A line containing a single 0 marks the end of the input and must not be processed.

Output

For each test case, print the selected numbers on one line, separated by single spaces, listed from the outer ring to the inner ring.