#include <iostream>
#include <map>
#include <vector>
#include <cctype>
using namespace std;

#define ERROR vector<int>()

int n, idx;
string s;
map<string, vector<int> > d;
map<vector<int>, string > rd;
map<string, vector<int> > v;

vector<int> formula();

vector<int> factor(){
  if(s[idx] == '('){
    idx++;
    vector<int> res = formula();
    idx++;
    return res;
  }
  else{
    string str = "";
    while(isalpha(s[idx])){
      str += s[idx];
      idx++;
    }
    return v[str];
  }
}

vector<int> term(){
  vector<int> res = factor();
  if(res == ERROR) return ERROR;

  while(s[idx] == '*' || s[idx] == '/'){
    bool mult = (s[idx] == '*');
    idx++;
    vector<int> tmp = factor();
    if(tmp == ERROR) return ERROR;

    for(int i = 0; i < n; i++){
      if(mult) res[i] += tmp[i];
      else     res[i] -= tmp[i];
    }
  }

  return res;
}

vector<int> formula(){
  vector<int> res = term();
  if(res == ERROR) return ERROR;

  while(s[idx] == '+' || s[idx] == '-'){
    bool plus = (s[idx] == '+');
    idx++;
    vector<int> tmp = term();

    if(res != tmp) return ERROR;
  }

  return res;
}

int main(){
  int m, p;

  while(cin >> n >> m >> p, n || m || p){
    d.clear();
    rd.clear();
    v.clear();

    for(int i = 0; i < m; i++){
      string derived;
      cin >> derived;

      vector<int> vec(n);
      for(int j = 0; j < n; j++){
        cin >> vec[j];
      }
      d[derived] = vec;
      rd[vec] = derived;
    }

    cin >> s;
    s = "(" + s + ")";

    for(int i = 0; i < p; i++){
      string V;
      string D;
      cin >> V >> D;
      v[V] = d[D];
    }

    idx = 0;

    vector<int> ans = formula();

    if(ans == ERROR){
      cout << "error\n";
    }
    else if(rd.find(ans) == rd.end()){
      cout << "undefined\n";
    }
    else{
      cout << rd[ans] << endl;
    }
  }
}