#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <cctype>

using namespace std;

typedef vector<int> Val;

map<string, Val> names;
map<string, int> pri;
map<string, Val> vars;
int N, M, P;
string expr;

struct Res {
  int idx;
  Val v;
  bool valid;
  Res(int idx, Val v, bool valid) : idx(idx), v(v), valid(valid) {}
  Res () {}
  void print() {
	for (int i = 0; i < N; i++) {
	  cout << v[i] << " ";
	}
	cout << endl;
  }
};


void init() {
  names.clear();
  pri.clear();
  vars.clear();
}

bool isSame(const Val& a, const Val& b) {
  for (int i = 0; i < N; i++) {
	if (a[i] != b[i]) {
	  return false;
	}
  }
  return true;
}

Val merge(const Val & l, const Val & r, char op) {
  Val res(N);
  //cout << op << endl;
  for (int i = 0; i < N; i++) {
	if (op == '*') {
	  res[i] = l[i] + r[i];
	} else if (op == '/') {
	  res[i] = l[i] - r[i];
	}
  }
  return res;
}

Res formula(int);

Res variable(int idx) {
  //cout << "variable " << idx << endl;
  string v;
  while (isalpha(expr[idx])) {
	v += expr[idx];
	idx++;
  }
  Res r;
  r.idx = idx;
  r.v = vars[v];
  r.valid = true;
  //r.print();
  return r;
}

Res term(int idx) {
  //cout << "term " << idx << endl;
  if (expr[idx] == '(') {
	Res r = formula(idx + 1);
	r.idx++;
	//r.print();
	return r;
  } else {
	Res r = variable(idx);
	//r.print();
	return r;
  }
}

Res factor(int idx) {
  //cout << "factor " << idx << endl;
  Res lhs = term(idx);
  idx = lhs.idx;
  if (!lhs.valid) {
	return lhs;
  }
  while (idx < expr.size() &&
		 (expr[idx] == '*' || expr[idx] == '/')) {
	Res rhs = term(idx + 1);

	if (!rhs.valid) {
	  return rhs;
	}
	
	lhs.idx = rhs.idx;
	
	Val l = lhs.v;
	Val r = rhs.v;
	
	lhs.v = merge(l, r, expr[idx]);
	idx = lhs.idx;

  }
  //lhs.print();
  return lhs;
}

Res formula(int idx) {
  //cout << "formula " << idx << endl;
  Res lhs = factor(idx);
  idx = lhs.idx;
  if (!lhs.valid) {
	return lhs;
  }
  while (idx < expr.size() &&
		 (expr[idx] == '+' || expr[idx] == '-')) {
	Res rhs = formula(idx + 1);

	if (!rhs.valid) {
	  return rhs;
	}
	
	lhs.idx = rhs.idx;
	idx = lhs.idx;
	
	Val l = lhs.v;
	Val r = rhs.v;
	
	if (isSame(l, r)) {
	  lhs.idx = rhs.idx;
	  lhs.v = l;
	} else {
	  lhs.valid = false;
	  return lhs;
	}
  }
  //lhs.print();
  return lhs;
}

void solve() {
  Res res = formula(0);

  if (!res.valid) {
	cout << "error" << endl;
	return;
  }

  vector<string> kouho;
  for (map<string, Val>::iterator it = names.begin();
	   it != names.end(); it++) {
	if (isSame(it->second, res.v)) {
	  kouho.push_back(it->first);
	}
  }
  if (kouho.empty()) {
	cout << "undefined" << endl;
	return;
  } else {
	int m = -1;
	string ans;
	for (int i = 0; i < kouho.size(); i++) {
	  if (m < pri[kouho[i]]) {
		m = pri[kouho[i]];
		ans = kouho[i];
	  }
	}
	cout << ans << endl;
  }
}

int main() {
  while (cin >> N >> M >> P) {
	if (N == 0 && M == 0 && P == 0) {
	  break;
	}
	init();
	int n = 0;
	for (int i = 0; i < M; i++) {
	  string name;
	  cin >> name;
	  vector<int> v(N);
	  for (int i = 0; i < N; i++) {
		cin >> v[i];
	  }
	  names[name] = v;
	  pri[name] = n;
	  n++;
	}
	
	cin >> expr;

	for (int i = 0; i < P; i++) {
	  string var;
	  cin >> var;
	  string v;
	  cin >> v;
	  vars[var] = names[v];
	}

	solve();
  }
}

Measured memory_bytes: 960