#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int n, m;
string buf;
map<string, int> cost, memo;
map<string, vector<string>> mat;

int dfs(string s) {
  if (memo.find(s) != memo.end()) return memo[s];
  if (mat.find(s) == mat.end()) return memo[s] = cost[s];
  int res = 0;
  for (string t : mat[s]) {
    res += dfs(t);
  }
  return memo[s] = min(cost[s], res);
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  while (cin >> n, n) {
    memo.clear();
    cost.clear();
    mat.clear();
    for (int i = 0; i < n; i++) {
      string s;
      int p;
      cin >> s >> p;
      cost[s] = p;
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
      string o;
      int k;
      cin >> o >> k;
      for (int i = 0; i < k; i++) {
        string q;
        cin >> q;
        mat[o].emplace_back(q);
      }
    }
    string t;
    cin >> t;
    cout << dfs(t) << endl;
  }

  return 0;
}
