#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;
map<string, int> items;
map<string, vector<string> > combs;

int rec(string name){
  if (combs.count(name) == 0)
    return items[name];
  int sum = 0;
  for (int i=0;i<combs[name].size();i++){
    sum += rec(combs[name][i]);
  }
  return min(sum, items[name]);
}

int main(){
  int m, n, l, cost;
  string name, tmp;
  while (cin >> n && n){
    items.clear();
    combs.clear();
    for (int i=0;i<n;i++){
      cin >> name;
      cin >> cost;
      items[name] = cost;
    }
    cin >> m;
    for (int i=0;i<m;i++){
      cin >> name >> l;
      for (int j=0;j<l;j++){
        cin >> tmp;
        combs[name].push_back(tmp);
      }
    }
    cin >> name;
    cout << rec(name) << endl;
  }

  return 0;
}