#include <algorithm>
#include <iostream>
#include <string>
#include <map>
#include <vector>

using namespace std;

int main() {
  for (;;) {
    int n, m;
    cin >> n;
    if (!n) return 0;
    map<string, int> ps;
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      cin >> ps[s];
    }
    cin >> m;
    map<string, vector<string> > rs;
    for (int i = 0; i < m; i++) {
      string s, q;
      int k;
      cin >> s >> k;
      for (int j = 0; j < k; j++) {
        cin >> q;
        rs[s].push_back(q);
      }
    }
    bool f;
    do {
      f = false;
      for (map<string, int>::iterator it = ps.begin(); it != ps.end(); ++it)
        if (!rs[it->first].empty()) {
          int s = 0;
          for (int j = 0; j < rs[it->first].size(); j++)
            s += ps[rs[it->first][j]];
          if (s < ps[it->first]) {
            f = true;
            ps[it->first] = s;
          }
        }
    } while (f);
    string s;
    cin >> s;
    cout << ps[s] << endl;
  }
}