#include<iostream>
#include<vector>
#include<map>

const int infty = 1<<29;
using namespace std;

int minimumCost(string need,
		map<string,int> &matval,
		map<string,vector<string> > &wrought)
{
  int ret = matval[need];
  
  int S = 1<<wrought[need].size();
  for(int i = 1; i < S; ++i){
    int cost = 0;
    for(int j = 0; j < wrought[need].size(); ++j){
      if( i & (1<<j) ){
	cost += minimumCost( wrought[need][j], matval, wrought );
      }else{
	cost += matval[ wrought[need][j] ];
      }
    }
    ret = min( ret, cost );
  }

  return ret;
}

int main()
{
  while(true){
    int n;
    cin >> n;
    map<string,int> material;
    map<string,vector<string> > wrought;

    if(n==0)break;
    for(int i = 0; i < n; ++i){
      string s;
      int val;
      cin >> s >> val;
      material[s]=val;
    }
    int m;
    cin >> m;
    for(int i = 0; i < m; ++i){
      string s;
      cin >> s;
      int num;
      cin >> num;
      for(int j = 0; j < num; ++j){
	string mat;
	cin >> mat;
	wrought[s].push_back(mat);
      }
    }
    string needed;
    cin >> needed;

    cout << minimumCost( needed, material, wrought ) << endl;
    
  }
  return 0;

}