//42
#include<iostream>
#include<map>
#include<vector>
#include<string>
#include<algorithm>

using namespace std;

map<string,int> pm;
map<string,vector<string> >rm;

int cost(string s){
  if(rm.find(s)!=rm.end()){
    int c=0;
    for(int i=0;i<rm[s].size();i++){
      c+=cost(rm[s][i]);
    }
    return min(c,pm[s]);
  }else{
    return pm[s];
  }
}

int main(){
  for(int n;cin>>n,n;){
    pm.clear();
    rm.clear();
    for(int i=0;i<n;i++){
      string i;
      int p;
      cin>>i>>p;
      pm[i]=p;
    }
    int m;
    cin>>m;
    for(int i=0;i<m;i++){
      string p;
      int k;
      cin>>p>>k;
      while(k--){
	string q;
	cin>>q;
	rm[p].push_back(q);
      }
    }
    string a;
    cin>>a;
    cout<<cost(a)<<endl;
  }
  return 0;
}