#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

typedef vector<int> vec;
typedef pair<vec, const char*> parsed;

map<vector<int>, string> nameMap;
map<string, vector<int> > mp;
string name[10];

parsed expr(const char *p);
parsed term(const char *p);
parsed fact(const char *p);

bool error;
parsed expr(const char *p)
{
  parsed r=term(p);
  while(*r.second=='+'||*r.second=='-'){
    char op=*r.second;
    vec tmp=r.first;
    r=term(r.second+1);
    // cout << "+" << endl;
    // FOR(it, tmp) cout << *it << " ";cout << endl;
    // FOR(it, r.first) cout << *it << " "; cout << endl;
    if (tmp != r.first) {
      error = 1;
    }
  }
  return r;
}

parsed term(const char *p)
{
  parsed r=fact(p);
  while(*r.second=='*'||*r.second=='/'){
    char op=*r.second;
    vec tmp=r.first;
    r=fact(r.second+1);
    if (op=='*') {
      REP(i, tmp.size()) {
        r.first[i] += tmp[i];
      }
    } else {
      REP(i, tmp.size()) {
        r.first[i] = tmp[i]-r.first[i];
      }
    }
    //cout << "*/" << endl;
    //FOR(it, r.first) cout << *it << " "; cout << endl;
    
  }
  return r;
}

parsed fact(const char *p)
{
  if (isalpha(*p)){
    string t=string(1,*(p++));
    while(isalpha(*p)) t += string(1, *(p++));
    return parsed(mp[t],p);
  }
  else if (*p=='('){
    parsed r=expr(p+1);
    if (*r.second!=')') exit(0); // invalid input
    return parsed(r.first,r.second+1);
  }
  else
    exit(0); // invalid input
}

int main() {
  int n, m, p;
  while(cin>>n>>m>>p,n||m||p) {
    nameMap.clear();
    mp.clear();
    REP(i, m) {
      cin >> name[i];
      vector<int> v(n);
      REP(j, n) cin >> v[j];
      nameMap[v] = name[i];
      mp[name[i]] = v;
    }
    string formula;
    cin >> formula;
    REP(i, p) {
      string v, D;
      cin >> v >> D;
      mp[v] = mp[D];
    }
    error = 0;
    vector<int> v = expr(formula.c_str()).first;
    if (error) {
      cout << "error" << endl;
    } else {
      if (nameMap.count(v) == 0) {
        cout << "undefined" << endl;
      } else {
        cout << nameMap[v] << endl;
      }
    }
  }
}