Clarke is a patient with multiple personality disorder. One day, Clarke turned into a coder and worked hard. He found type casting in c++ disgusting. If an expression is too long, an error will be extremely likely to be reported due to impossible casting, or a variable will be cast to a not expected type. One day the compiler malfunctioned(doge), so he wanted to complete the compiling work.
Then he left the arduous work to you!
Clarke's expressions are as followings. They are composed of 4 elements:types, variables, brackets and operators, where types and variables are strings consisting of lowercase letters, a bracket is either '(' or ')', and the only operator is '*'. When calculating, we should work from left to right, and deal with brackets first.
Expressions are followed two rules:
1. $type(a)$: meaning casting variable $a$ to type $type$, getting a new variable.($type$ must be a type and $a$ must be a variable. Types and brackets will only appear here in pairs.)
2. $a*b$: meaning casting variable $b$ to the type of variable $a$ and then calculating, getting a new variable of $a$'s type. ($a$ and $b$ must be variables when we are dealing with '*')
Now Clarke will give you an expression, variables and some rules of type casting, and need you to determine whether each expression is legal, and to tell him the type of the result of the expression if it is legal.
Illegal cases:
1. Distinct identifiers of the same name, or variables of nonexistent types.
2. A word neither a type nor a variable.
3. Impossible casting.
4. Expressions not following the rules listed above.
Some explanations and conventions:
1. A variable alone is a legal expression.
2. If $a$ in $type(a)$ is an expression, then get a new variable by calculating $a$ before performing the rule 1.
3. It is possible to cast type $type$ to type $type$.
4. The facts that type $C$ can be cast to type $B$ and that type $B$ can be cast to type $A$ do not mean that type $C$ can be cast to type $A$.
5. Expression $b*type(a)$ means first casting $a$ to type $type$, then casting it to the type of $b$ and finally getting a new variable of the type of $b$.