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

typedef pair<string, int> psi;
vector<psi> items;
vector<pair<string, vector<psi> > > recipes;

int search_items(string name) {
  for (int i = 0; i < items.size(); i++) {
    if (name == items[i].first) {
      return items[i].second;
    }
  }
  return 0;
}

int dfs(string name) {
  int index;
  for (index = 0; index < recipes.size(); index++) {
    if (recipes[index].first == name) {
      break;
    }
  }

  int ret = 0;
  if (index == recipes.size()) {
    return search_items(name);
  } else {
    for (int i = 0; i < recipes[index].second.size(); i++) {
      ret += min(search_items(recipes[index].second[i].first), dfs(recipes[index].second[i].first));
    }
  }

  return ret;
}


int main(void) {
  int n, m;
  while (1) {
    cin >> n;
    if (n == 0) break;

    for (int i = 0; i < n; i++) {
      string name;
      int cost;
      cin >> name >> cost;
      items.push_back(psi(name, cost));
    }

    cin >> m;
    for (int i = 0; i < m; i++) {
      string name;
      int num;
      vector<psi> tmp;
      cin >> name >> num;
      for (int j = 0; j < num; j++) {
        string s;
        cin >> s;
        for (int k = 0; k < n; k++) {
          if (s == items[k].first) {
            tmp.push_back(items[k]);
            break;
          }
        }
      }
      recipes.push_back(pair<string, vector<psi> >(name, tmp));
    }

    string s;
    cin >> s;
    cout << dfs(s) << endl;
  }

  return 0;
}