#include<iostream>
#include<algorithm>
#include<map>
#include<cassert>
#include<vector>
#define F first
#define S second
#define MAX 10000000
using namespace std;
typedef pair<int,bool> P;
map<string,P> material;
vector<string> vec[110];
map<string,P> index;


int rec(string target){
  
  int value = MAX;
  map<string,P>::iterator it = material.begin();
  while(it != material.end() ){
    if((*it).F == target)value = min(value,(*it).S.F);
    it++;
  }
  
  int X = 10000000;
  int id = -1,pre=X;
  if(index[target].S)id = index[target].F;

  if(id != -1){
    for(int i=0;i<vec[id].size();i++){
      pre += rec(vec[id][i]);
    }
  }

  value = min(id == -1?X:pre-X,value);
  assert(value != MAX);
  return value;
}


int main(){
  int n,m;

  while(true){
    cin >> n;
    if(n == 0)break;
    
    material.clear();
    for(int i=0;i<n;i++){
      vec[i].clear();
      string s; int val;
      cin >> s >> val;
    
      material[s] = P(val,true);
    }
   
    index.clear();
    cin >> m;
    for(int i=0;i<m;i++){
      string s,ss; int cn;
      cin >> s >> cn;
      index[s] = P(i,true);
      for(int j=0;j<cn;j++){
	cin >> ss;
	vec[i].push_back(ss);
      }
    }
    
    string st;
    cin >> st;
    cout << rec(st) << endl;


  }
  return 0;
}