#include <bits/stdc++.h>
using namespace std;

#define dump(...) cout<<"# "<<#__VA_ARGS__<<'='<<(__VA_ARGS__)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define all(c) begin(c),end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}

template<typename Tuple>
void print_tuple(ostream&,const Tuple&){}
template<typename Car,typename... Cdr,typename Tuple>
void print_tuple(ostream& os,const Tuple& t){
	print_tuple<Cdr...>(os,t);
	os<<(sizeof...(Cdr)?",":"")<<get<sizeof...(Cdr)>(t);
}
template<typename... Args>
ostream& operator<<(ostream& os,const tuple<Args...>& t){
	print_tuple<Args...>(os<<'(',t);
	return os<<')';
}

template<typename Ch,typename Tr,typename C,typename=decltype(begin(C()))>
basic_ostream<Ch,Tr>& operator<<(basic_ostream<Ch,Tr>& os,const C& c){
	os<<'[';
	for(auto i=begin(c);i!=end(c);++i)
		os<<(i==begin(c)?"":" ")<<*i;
	return os<<']';
}

constexpr int INF=1e9;
constexpr int MOD=1e9+7;
constexpr double EPS=1e-9;

tuple<vi,int> expr(const string& s,int i,const map<string,vi>& utod,const map<string,string>& vtou);
	
tuple<vi,int> fact(const string& s,int i,const map<string,vi>& utod,const map<string,string>& vtou)
{
	if(s[i]=='('){
		vi ds; tie(ds,i)=expr(s,i+1,utod,vtou);
		return mt(ds,i+1);
	}
	else{
		string var;
		for(;i<s.size() && isalpha(s[i]);i++)
			var+=s[i];
		return mt(utod.at(vtou.at(var)),i);
	}
}
tuple<vi,int> term(const string& s,int i,const map<string,vi>& utod,const map<string,string>& vtou)
{
	vi ds; tie(ds,i)=fact(s,i,utod,vtou);
	while(i<s.size() && (s[i]=='*' || s[i]=='/')){
		//dump(mt(__func__,i,s[i]));
		assert(s[i]=='*' || s[i]=='/');
		char op=s[i];
		vi ds2; tie(ds2,i)=fact(s,i+1,utod,vtou);
		if(op=='*')
			rep(j,ds.size()) ds[j]+=ds2[j];
		else
			rep(j,ds.size()) ds[j]-=ds2[j];
	}
	return mt(ds,i);
}
tuple<vi,int> expr(const string& s,int i,const map<string,vi>& utod,const map<string,string>& vtou)
{
	vi ds; tie(ds,i)=term(s,i,utod,vtou);
	while(i<s.size() && s[i]!=')'){
		//dump(mt(__func__,i,s[i]));
		assert(s[i]=='+' || s[i]=='-');
		vi ds2; tie(ds2,i)=term(s,i+1,utod,vtou);
		if(ds!=ds2) throw "error";
	}
	return mt(ds,i);
}

int main()
{
	for(int n,m,p;cin>>n>>m>>p && n|m|p;){
		map<string,vi> utod; // unit -> dimension
		rep(i,m){
			string unit; cin>>unit;
			vi ds(n);
			rep(j,n) cin>>ds[j];
			utod[unit]=ds;
		}
		string s; cin>>s;
		map<string,string> vtou; // variable -> unit
		rep(i,p){
			string var,unit; cin>>var>>unit;
			vtou[var]=unit;
		}
		//dump(mt(n,m,p)); dump(s); dump(utod); dump(vtou);
		
		try{
			vi ds; tie(ds,ignore)=expr(s,0,utod,vtou);
			string res="undefined";
			for(auto p:utod)
				if(ds==p.second)
					res=p.first;
			cout<<res<<endl;
		}
		catch(const char* msg){
			cout<<msg<<endl;
		}
	}
}

Measured memory_bytes: 1308