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

const int INF = 1<<21;
map<string, int> value;
map<string, vector<string> > R;

int rec(string s) {
  int res = INF;
  if(value.find(s) != value.end()) res = min(res, value[s]);
  if(R.find(s) != R.end()) {
    int sum = 0;
    for(vector<string>::iterator it = R[s].begin(); it != R[s].end(); it++) {
      sum += rec(*it);
    }
    res = min(res, sum);
  }
  return res;
}

main() {
  int n;
  while((cin >> n) && n) {
    value.clear();
    R.clear();
    while(n--) {
      string s;
      int a;
      cin >> s >> a;
      value[s] = a;
    }
    cin >> n;
    while(n--) {
      string s;
      int m;
      cin >> s >> m;
      while(m--) {
	string t;
	cin >> t;
	R[s].push_back(t);
      }
    }
    string s;
    cin >> s;
    cout << rec(s) << endl;
  }
}