#include<iostream>
#include<vector>
#include<algorithm>
#include<map>

using namespace std;
int n,m;
map<string,int > buy;
map<string,pair<int,vector<string> > > make;

int cal(string s){
  int ret1,ret2;
  //cout<<"name:"<<s<<endl;
  ret1 = buy[s];
  if( make.find(s) != make.end() ){
    pair<int,vector<string> > p = make[s];
    int k = p.first;
    ret2 = 0;
    for(int i = 0;i<k;i++){
      ret2 += cal((p.second)[i]);
    }    
  }else{
    ret2 = 1111111111;
  }
  return min(ret1,ret2);
  

}

int main(){
  while(cin>>n){
    if(n==0)
      return 0;
    buy.clear();
    make.clear();
    for(int i = 0;i<n;i++){
      string s;
      int p;
      cin>>s>>p;
      buy[s] = p;
    }
    cin>>m;
    for(int i = 0;i<m;i++){
      string s,tmp;
      int k;
      vector<string> tmp_v;
      cin>>s;
      cin>>k;
      for(int j = 0;j<k;j++){
	cin>>tmp;
	tmp_v.push_back(tmp);
      }
      make[s] = make_pair(k,tmp_v);
    }
    string t;
    cin>>t;
    cout<<cal(t)<<endl;
  }

  return 0;
}