#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<pair<string,pair<int,int> > > resipi;
vector<pair<string,int> >psi;

int main(){
  int n;
  while(cin>>n,n!=0){
    resipi.clear();
    psi.clear();
    ll cost=0;
    for(int i=0;i<n;i++){
      string str;int ne;cin>>str>>ne;
      psi.push_back(make_pair(str,ne));
    }
    int m;
    cin>>m;
    int ban=0,k=0;
    vector<string> sozai;
    for(int i=0;i<m;i++){
      string renkin;cin>>renkin;
      int snum;cin>>snum;
      for(int j=0;j<snum;j++){
	string s;
	cin>>s;
	sozai.push_back(s);
      }
      resipi.push_back(make_pair(renkin,make_pair(ban,ban+snum)));
      ban+=snum;
    }
    string product;
    cin>>product;
    int flag=0;
    int kane=0;
    for(int i=0;i<resipi.size();i++){
      kane=0;
      for(int j=resipi[i].second.first;j<resipi[i].second.second;j++){
	for(int l=0;l<psi.size();l++){
	  if(sozai[j]==psi[l].first)
	    kane+=psi[l].second;
	}
      }
      for(int j=0;j<psi.size();j++){
	if(resipi[i].first==psi[j].first && kane<psi[j].second){
	  psi[j].second=kane;
	  break;
	}
      }
    }
    for(int i=0;i<psi.size();i++)
      if(psi[i].first==product)
	cout<<psi[i].second<<endl;
  }
return 0;
}