#include<iostream>
#include<algorithm>
#include<cassert>
#include<map>
#include<vector>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)
#define N 100

const int INF = 1000100; 
int table[N];
int buycost[N];

int solve(int now,vector<int> *make){
  if ( table[now] != -1)return table[now];
  if(  make[now].size()==0)return table[now]=buycost[now];
  

  int ret = buycost[now],tmp=0;  
  rep(i,make[now].size()){
    tmp+=solve(make[now][i],make);
  }
  
  ret= min(ret,tmp);
  return table[now]=ret; 
}

main(){
  int n;
  while(cin>>n && n){
    map<string,int> M;
    rep(i,n){
      string t;int a;
      cin>>t>>a;
      M[t]=i;
      buycost[i]=a;
      table[i]=-1;
    }
    
    vector<int> make[n];
    cin>>n;
    rep(i,n){
      string a;
      cin>>a;
      int now =M[a],m;
      cin>>m;
      rep(j,m){
	cin>>a;
	make[now].push_back(M[a]);
      }     
    }
    string tmp;
    cin>>tmp;
    int tar=M[tmp];
    cout << solve(tar,make) << endl;
    
  }
  return false;
}