Problem: The dimension of quantity is a concept that ignores concrete numerical values ​​from the relational expressions between different quantities and focuses only on the type of quantity and its power. Specifically, the relational expression of the dimension is expressed as an equation ignoring the constant coefficient. That is, if the dimension of the quantity q is expressed as [q], the following relational expressions of several dimensions can be exemplified.

* [Area] = [Length] 2
* [Volume] = [Length] 3
* [Speed] = [Length] [Time] -1
* [Acceleration] = [Length] [Time] -2
* [Force] = [Mass] [Length] [Time] -2
* [Work] = [Mass] [Length] 2 [Time] -2



(Excerpt from Wikipedia "Quantity" URL http://ja.wikipedia.org/wiki/%E9%87%8F)

The amount is classified into the basic amount and the assembled amount. In this problem, n basic quantities and m assembly quantities are given. The dimensions of a given m assembly can all be represented by the product of n base unit dimensions. Also, when two dimensions are represented by the product of the dimensions of the basic quantities, it can be said that the two dimensions are equal if all the indices of the basic quantities match.

Given information about the quantity of expressions and variables and the dimension of the quantity, analyze the equation and output the name of the quantity represented by that dimension. If the name is not defined, output undefined. In addition, an operation that adds or subtracts two different dimensions in the calculation process is defined as an "illegal operation". If there is an "illegal operation", output error. The given equation satisfies:

1. All variables are assigned to the assembly dimension.
2. The expression contains only four arithmetic operations +,-, /, *, parentheses () and variables.
3. The four arithmetic operations are well known and shall be described correctly. Strictly speaking, it follows the following BNF. Also, unary operators are not used. There is no such thing as (x * (-a)).




<formula> :: = <term> | <formula> + <term> | <formula>-<term>
<term> :: = <factor> | <term> * <factor> | <term> / <factor>
<factor> :: = <variable> | (<formula>)
<variable> :: = <string>
<string> :: = <char> | <string> <char>
<char> :: = a ~ z | A ~ Z
<formula> represents an expression and <variable> represents a variable name.




Input

The input consists of multiple datasets. The number of datasets does not exceed 1,000. The dataset is given in the following format.


n m p
derived1
d1,1 d1,2 d1,3 .... d1, n
derived2
d2,1 d2,2 d2,3 .... d2, n
...
derivedm
dm, 1 dm, 2 dm, 3 .... dm, n
formula
v1 D1
v2 D2
...
vp Dp


n (1 ≤ n ≤ 5) is the number of basic quantities m (1 ≤ m ≤ 10) is the number of assemblies p (1 ≤ p ≤ 15) is the number of variable types. derivedi is the name of the i-th assembly. It is composed of uppercase and lowercase letters and does not exceed 20 characters in length. di, j (-10 ≤ di, j ≤ 10) represents the dimension of the j-th basic quantity of the i-th assembly (1 ≤ i ≤ m, 1 ≤ j ≤ n). The name of the same assembly does not appear multiple times. The assembly quantity of the same dimension may appear multiple times, in which case the last defined name will be the name of the assembly quantity. formula represents the formula to be analyzed. The length does not exceed 100 characters. vi represents a variable name. It is composed of uppercase and lowercase letters and does not exceed 20 characters in length. Di represents the name of the dimension of vi. It is guaranteed to be the name of the assembly already given. The numbers you are typing (n, m, p, d) are all integers. The end of the input is indicated by a line where each of the three 0s is separated by a single character space.

Output

Analyze the formula in one line and output the name of the dimension. If the name is not defined, output undefined. Also, if an invalid operation is included, output error.

Example

Input

2 3 2
length
1 0
time
0 1
speed
1 -1
a/b
a length
b time
2 3 3
length
1 0
time
0 1
speed
1 -1
a/b+c
a length
b time
c speed
2 3 3
length
1 0
time
0 1
speed
1 -1
a/b+c
a length
b time
c time
2 3 2
length
1 0
time
0 1
speed
1 -1
a/b/b
a length
b time
3 6 6
speed
1 -1 0
acceleration
1 -2 0
force
1 -2 1
length
1 0 0
time
0 1 0
weight
0 0 1
((v+l/t)+a*t)/(t*t)*w*t+f+w*a
v speed
l length
a acceleration
t time
w weight
f force
0 0 0


Output

speed
speed
error
undefined
force

Solution (in JAVA):

Solution:
import java.util.*;
class Main{
	static int[][] dim;
	static HashMap<String,Integer> map;
	static String fact;
	static int[]dame;
	static int[] solve(int a,int b){
		int kakko=0;
		int last=0;
		int N[][]=new int[100][dim[0].length];
		int K[]=new int[100];
		int at=0;
		String q="";
		for(int i=a;i<b;i++){
			if(fact.charAt(i)=='('){
				if(kakko==0)last=i;
				kakko++;
			}else if(fact.charAt(i)==')'){
				kakko--;
				if(kakko==0){
					N[at]=solve(last+1,i);
					if(N[at][0]==-99999999)return dame;
				}
			}else if(kakko==0){
				if(fact.charAt(i)=='+'){
		//	System.out.println(q);
					if(q.length()>0){
						N[at]=dim[map.get(q)];
					}
					K[at++]=1;
					q="";
				}
				else if(fact.charAt(i)=='-'){
		//	System.out.println(q);
					if(q.length()>0){
						N[at]=dim[map.get(q)];
					}
					K[at++]=2;
					q="";
				}
				else if(fact.charAt(i)=='*'){
	//		System.out.println(q);
					if(q.length()>0){
						N[at]=dim[map.get(q)];
					}
					K[at++]=3;
					q="";
				}
				else if(fact.charAt(i)=='/'){
		//	System.out.println(q);
					if(q.length()>0){
						N[at]=dim[map.get(q)];
					}
					K[at++]=4;
					q="";
				}else{
					q+=fact.charAt(i);
				}
			}
		}
		boolean OK=true;
	//	System.out.println(q);
		if(q.length()>0)N[at]=dim[map.get(q)];
		int []now=new int[dim[0].length];
		int []val=new int[dim[0].length];
		for(int i=0;i<dim[0].length;i++)now[i]=N[0][i];
		int Last=0;
	//	System.out.println(a+" "+b);
	//	System.out.println(Arrays.toString(now));
		for(int i=0;i<at;i++){
			if(K[i]<3&&Last>0){
				for(int j=0;j<dim[0].length;j++){
					if(now[j]!=val[j])return dame;
					now[j]=N[i+1][j];
				}
	//	System.out.println(i+" "+K[i]+" "+Arrays.toString(now));
			}else if(K[i]<3){
				for(int j=0;j<dim[0].length;j++){
					val[j]=now[j];
					now[j]=N[i+1][j];
				}
	//	System.out.println(i+" "+K[i]+" "+Arrays.toString(now));
				Last=1;
			}else if(K[i]==3){
				for(int j=0;j<dim[0].length;j++)now[j]+=N[i+1][j];
	//	System.out.println(i+" "+K[i]+" "+Arrays.toString(now));
			}else{
				for(int j=0;j<dim[0].length;j++)now[j]-=N[i+1][j];
	//	System.out.println(i+" "+K[i]+" "+Arrays.toString(now));
			}
		}
		if(Last>0){
	//	System.out.println(Arrays.toString(now));
			for(int j=0;j<dim[0].length;j++){
				if(now[j]!=val[j])return dame;
			}
		}else{
	//	System.out.println(Arrays.toString(now));
			for(int j=0;j<dim[0].length;j++){
				val[j]=now[j];
			}
		}
		return val;
	}
	public static void main(String[] args){
		Scanner s=new Scanner(System.in);
		while(s.hasNext()){
			int a=s.nextInt();
			int b=s.nextInt();
			int c=s.nextInt();
			if(a+b+c==0)System.exit(0);
			dim=new int[b][a];
			dame=new int[a];
			dame[0]=-99999999;
			String name[]=new String[b];
			for(int i=0;i<b;i++){
				name[i]=s.next();
				for(int j=0;j<a;j++)dim[i][j]=s.nextInt();
			}
			fact=s.next();
			map=new HashMap<String,Integer>();
			for(int i=0;i<c;i++){
				String v=s.next();
				String w=s.next();
				for(int j=0;j<b;j++){
					if(name[j].equals(w)){
						map.put(v,j);
					}
				}
			}
			int []ans=solve(0,fact.length());
	//		for(int i=0;i<ans.length;i++)System.out.print(ans[i]+" ");
			if(ans[0]==-99999999){
				System.out.println("error");
			}else{
				boolean OK=false;
				for(int i=b-1;i>=0;i--){
					boolean ok=true;
					for(int j=0;j<a;j++)if(ans[j]!=dim[i][j])ok=false;
					if(ok){
						System.out.println(name[i]);
						OK=true;
						break;
					}
				}
				if(!OK)System.out.println("undefined");
			}
		}
	}
}