#include<iostream>
#include<vector>
#include<map>
using namespace std;

const int N = 100;

vector<int> recipe[N];//store recipe
int price[N];           //store price


int solve(int now){
  int ret = price[now];
  int tmp = 0;
  if (recipe[now].size() == 0)return ret;
  for(int i=0;i<recipe[now].size();i++){
    tmp+=solve(recipe[now][i]);
  }
  return min(ret,tmp);
}


main(){
  int n;
  while(cin>>n && n){
    int p;
    map<string,int> M;
    string tmp;
    
    //get buy information
    for(int i=0;i<n;i++){
      cin>>tmp>>p;
      M[tmp]=i;
      price[i]=p;
      recipe[i].clear();
    }
    
    //get recipe
    int m;
    cin>>m;
    for(int i=0;i<m;i++){
      int num,me;
      cin>>tmp>>num;
      me=M[tmp];
      for(int j=0;j<num;j++){
	cin>>tmp;
	recipe[me].push_back(M[tmp]);
      }
    }


    cin>>tmp;
    cout << solve(M[tmp]) <<endl;
  }
}