Triangular Matrices

No attempts yetTime limit1sMemory limit128 MB

Problem

A triangular matrix is a set of numbers laid out as an equilateral triangle.

A triangular matrix of size LL has LL rows, and the iith row from the top holds ii numbers.

Addition of triangular matrices is defined like this.

Only two triangular matrices of the same size can be added, and numbers in matching positions are added together.

Multiplication of triangular matrices is defined like this.

Multiplication is not commutative. In A * B the size of the first matrix AA must be less than or equal to the size of BB. Find every triangle inside BB that has the same size as AA, and for each one multiply the overlapping numbers and add the products. That sum fills one position of the result. The triangle whose apex sits at row rr, column cc of BB gives the value at row rr, column cc of the result, so the result has size (size of BB) minus (size of AA) plus 1.

In the picture above, the triangles inside BB that have the same size as AA are {b1,b2,b3}\{b_1, b_2, b_3\}, {b2,b4,b5}\{b_2, b_4, b_5\} and {b3,b5,b6}\{b_3, b_5, b_6\}. Multiplying the corresponding numbers and adding them gives c=a1b1+a2b2+a3b3c = a_1 b_1 + a_2 b_2 + a_3 b_3, d=a1b2+a2b4+a3b5d = a_1 b_2 + a_2 b_4 + a_3 b_5 and e=a1b3+a2b5+a3b6e = a_1 b_3 + a_2 b_5 + a_3 b_6. The triangle behind cc is at the top of BB, so cc takes the top position of the result. dd and ee go on the next row with dd on the left and ee on the right, the way the original triangles were arranged.

The expression is given in postfix notation. For instance A B * A + means (A * B) + A. Reading from the left you get A, then B, then *, so A * B is computed first. Call that result CC and replace A B * with CC. What is left is C A +, which means C+AC + A.

Input

The input holds several data sets.

The first line of a data set has an integer NN, the number of triangular matrices used in the expression. The matrices follow, one after another. A matrix starts with a line holding a string identifier KK and the side length LL, then LL lines hold the values of the matrix separated by spaces. The first line has 1 value, the second line has 2 values, the third line has 3 values, and so on through the LLth line. The first line with one value is the top point of the triangle, and the LLth line with LL values is the bottom of the triangle.

After all matrices comes a line with the expression to evaluate. Operators and symbols in the expression are separated by spaces, and each matrix is written as its string identifier.

The input ends when NN is 0.

Output

For each data set, print the resulting triangular matrix in the same format the matrices were given in. The top value goes on the first line, then each row from left to right with values separated by single spaces, down to the bottom row on the last line. Do not print a blank line between data sets.

If the expression cannot be evaluated, print Invalid expression. An expression cannot be evaluated when it uses an identifier that was never defined, when the postfix notation is malformed, when addition gets two triangular matrices of different sizes, or when multiplication gets a first matrix larger than the second one.