Suppose you have to evaluate an expression like $A*B*C*D*E$, where A, B, C, D and E are matrices.
Since matrix multiplication is associative, the order in which the multiplications are performed is arbitrary. However, the number of elementary multiplications needed depends strongly on the evaluation order you choose.
For example, let $A$ be a $50 \times 10$ matrix, $B$ a $10 \times 20$ matrix, and $C$ a $20 \times 5$ matrix.
There are two different strategies to compute $A*B*C$, namely $(A*B)*C$ and $A*(B*C)$.
The first one takes 15000 elementary multiplications, but the second one only 3500.
Your job is to write a program that determines the number of elementary multiplications needed for a given evaluation strategy.
The input consists of two parts: a list of matrices and a list of expressions.
The first line contains one integer $n$ ($1 \le n \le 26$), the number of matrices. Each of the next $n$ lines contains one capital letter (the name of a matrix) and two integers (its number of rows and columns).
The second part strictly adheres to the following syntax, given in EBNF.
SecondPart = Line { Line } <EOF>
Line = Expression <CR>
Expression = Matrix | "(" Expression Expression ")"
Matrix = "A" | "B" | "C" | ... | "X" | "Y" | "Z"
Each line holds one expression, and expressions continue until the end of the file.
For each expression in the second part of the input, print one line containing the word "error" if evaluation of the expression leads to an error due to non-matching matrices. Otherwise print one line containing the number of elementary multiplications needed to evaluate the expression in the way specified by the parentheses.