#include<iostream>
#include<string>
#include<map>
#include<algorithm>

using namespace std;

map<string, int> InputMaterial(int n);
map< string, map<string, int> > InputWrought(int n);
int Calculate(const string, map<string, int>, map< string, map<string, int> >);

int main(){
  int n, m;
  string str;
  map<string, int> material;
  map< string, map<string, int> > wrought;

  while(1){
    cin >> n;
    if(n == 0) break;
    material = InputMaterial(n);

    cin >> n;
    wrought = InputWrought(n);

    cin >> str;
    cout << Calculate(str, material, wrought) << endl;
  }
  return 0;
}

map<string, int> InputMaterial(int n){
  int i, m;
  string str;
  map<string, int> data;

  for(i=0; i<n; ++i){
    cin >> str >> m;
    data.insert(make_pair(str, m));
  }
  return data;
}

map< string, map<string, int> > InputWrought(int n){
  int i, j, m;
  string str, _str;
  map<string, int> _data;
  map< string, map<string, int> > data;
  map<string, int>::iterator iter;

  for(i=0; i<n; ++i){
    cin >> str >> m;
    for(j=0; j<m; ++j){
      cin >> _str;
      iter = _data.find(_str);
      if(iter == _data.end()) _data.insert(make_pair(_str, 1));
      else ++iter->second;
    }
    data.insert(make_pair(str, _data));
    _data.clear();
  }
  return data;
}

int Calculate(const string one_material, map<string, int> material,
	      map< string, map<string, int> > wrought){
  int n, sum;
  map<string, int>::iterator i, j;
  map< string, map<string, int> >::iterator k;

  i = material.find(one_material);
  if(i == material.end()) return -1;

  k = wrought.find(one_material);
  if(k == wrought.end()) return i->second;

  for(j=k->second.begin(), sum=0; j!=k->second.end(); ++j){
    n = Calculate(j->first, material, wrought);
    if(n == -1) return i->second;
    sum += (n * j->second);
  }
  if(sum > i->second) return i->second;
  else return sum;
}