#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define mp make_pair

int n, m;

void solve() {
  map<string,int> il;

  rep(i,n) {
    string name; int price; cin>>name>>price;
    il.insert(mp(name, price));
  }
  
  map<string,vector<string> > al;
  cin>>m;
  rep(i,m) {
    string name; int tn; cin>>name>>tn;
    vector<string> t_vec(tn);
    rep(j,tn) {
      cin>>t_vec[j];
    }
    al.insert(mp(name, t_vec));
  }
  
  map<string,int> al_p;
  
  
  string item; cin>>item;
  /*
  {
    cout<<"item list"<<endl;
    map<string,int>::iterator it = il.begin();
    while (it != il.end()) {
      cout<<it->first<<"|"<<it->second<<endl;
      it++;
    }
  }
  {
    cout<<"alchem item list"<<endl;
    map<string,vector<string> >::iterator it = al.begin();
    while (it != al.end()) {
      cout<<it->first<<"|";
      rep(i,it->second.size()) cout<<it->second.at(i)<<" ";
      cout<<endl;
      it++;
    }
  }*/
    
  // BàXg©çBàliXgÉliðo^·é
  while (true) {
    if (al.size() == al_p.size()) break;
    map<string,vector<string> >::iterator it = al.begin();
    while (it != al.end()) {
      if (al_p.count(it->first)) {it++;continue;}
      // efÞÌvÌliðvZ·é
      // tOð§ÄÄ¨«AêÂÅàvZÅ«È¢fÞª¶Ý·éêÍ[vðñ·
      int sum_price = 0;
      bool flag = true;
      rep(i,it->second.size()) {
        // BàfÞÌACeªACeXgAÜ½ÍBàXgÉ é©
        string material = it->second.at(i);
        // BàXgÉ èABàliXgÉ³¢êÍcontinue;
        if (il.count(material) && al.count(material)) {
          if (al_p.count(material)) {
            sum_price += min(il[material], al_p[material]);
          } else {
            flag = false;
          }
        }
        // BàXgÉµ©³¢ê
        else if (!il.count(material) && al.count(material)) {
          if (al_p.count(material)) {
            sum_price += al_p[material];
          } else {
            flag = false;
          }
        }
        // ACeXgÉµ©³¢ê
        else if (il.count(material) && !al.count(material)) {
          sum_price += il[material];
        }
      }
      // SÄÌfÞÌliðvZÅ«½
      if (flag) {
        //cout<<"insert "<<it->first<<endl;
        al_p.insert(mp(it->first, sum_price));
      }
      it++;
    }
  }
  
  /*
  {
    cout<<"alchem item price list"<<endl;
    map<string,int>::iterator it = al_p.begin();
    while(it != al_p.end()) {
      cout<<it->first<<"|"<<it->second<<endl;
      it++;
    }
  }*/
  int res;
  if (il.count(item) && al_p.count(item)) {
    res = min(il[item], al_p[item]);
  } else if (!il.count(item) && al_p.count(item)) {
    res = al_p[item];
  } else {
    res = il[item];
  }
  cout<<res<<endl;
}

int main (int argc, char const* argv[]) {
  while (cin>>n, n) {
    solve();
  }
  return 0;
}