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

struct Trn {
  int k;
  string *mat;
};

map<string, int> p;
map<string, Trn> q;

int min(string item) {
  int buy = p[item];
  if (q.find(item) != q.end()) {
    int trn = 0;
    for (int j=0; j<q[item].k; j++) {
      trn += min(q[item].mat[j]);
    }
    if (trn < buy) {
      return trn;
    }
  }
  return buy;
}

int main(void) {
  while (true) {
    int n;
    cin >> n;
    if (n == 0) {
      break;
    }
    for (int j=0; j<n; j++) {
      string i;
      cin >> i;
      cin >> p[i];
    }
    int m;
    cin >> m;
    for (int j=0; j<m; j++) {
      string o;
      cin >> o;
      cin >> q[o].k;
      q[o].mat = new string[q[o].k];
      for (int l=0; l<q[o].k; l++) {
	cin >> q[o].mat[l];
      }
    }
    string item;
    cin >> item;
    cout << min(item) << endl;
    p.clear();
    if (!q.empty()) {
      for (map<string, Trn>::iterator j=q.begin(); j!=q.end(); j++) {
	delete[] j->second.mat;
      }
      q.clear();
    }
  }
  return 0;
}