#include <iostream>
#include <string>
#include <vector>
#include <map>
#define INF (2 << 28)
using namespace std;

map<string, int> item;
map<string, vector<string> > wr;
map<string, int> dp;

void init(){
  
  item.clear(); dp.clear();
  map<string, vector<string> >::iterator it;
  for(it = wr.begin(); it != wr.end(); it++){
    it->second.resize(0);
  }
  wr.clear();

}

int find(string s){
  
  int ret = 0;
  bool judge = false;
  if(dp[s]) return dp[s];
  map<string, vector<string> >::iterator it;
  for(it = wr.begin(); it != wr.end(); it++){
    if(it->first == s){
      for(int i = 0; i < it->second.size(); i++){
	ret += find(it->second[i]);
      }
      judge = true;
      break;
    }
  }
  return dp[s] = min((judge ? ret : INF), (item.find(s) == item.end() ? INF : item[s]));

}

int main(){
  
  int n, m; string trg;
  while(cin >> n, n){
    init();
    for(int i = 0; i < n; i++){
      string s; int v; cin >> s >> v;
      item[s] = v;
    }
    cin >> m;
    for(int i = 0; i < m; i++){
      string s; int v; cin >> s >> v;
      for(int j = 0; j < v; j++){
	string name; cin >> name;
	wr[s].push_back(name);
      }
    }
    cin >> trg;
    cout << find(trg) << endl;
  }

}