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

struct Recipe;

struct Item {
  int buy_cost;
  Recipe* recipe;
  int calced_min_cost;

  void init() {
    recipe = NULL;
    calced_min_cost = -1;
  }

  bool is_calced() {
    return calced_min_cost != -1;
  }
};

struct Recipe {
  int n;
  Item* parts[100];
};

map<string, Item> imap;

int calc_min_cost_of(Item& item) {
  if (item.is_calced()) return item.calced_min_cost;
  if (item.recipe == NULL) return item.calced_min_cost = item.buy_cost;
  auto& recipe = *item.recipe;
  int gen_cost = 0;
  for (int i = 0; i < recipe.n; i++) {
    gen_cost += calc_min_cost_of(*recipe.parts[i]);
    if (gen_cost >= item.buy_cost) return item.calced_min_cost = item.buy_cost;
  }
  return item.calced_min_cost = gen_cost;
}

// buff
Recipe recipes[100];

int main() {
  int n;
  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      string name;
      int buy_cost;
      cin >> name >> buy_cost;
      imap[name] = {buy_cost, NULL, -1};
    }

    int m;
    cin >> m;
    for (int i = 0; i < m; i++) {
      auto& recipe = recipes[i];

      string target_name;
      cin >> target_name >> recipe.n;
      imap[target_name].recipe = &recipe;
      
      for (int j = 0; j < recipe.n; j++) {
        string part_name;
        cin >> part_name;
        recipe.parts[j] = &imap[part_name];
      }
    }
    
    string name;
    cin >> name;
    cout << calc_min_cost_of(imap[name]) << endl;
  }
  return 0;
}