#include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>

struct item
{
  std::vector<std::string> materials;
  int price = 0;
  int true_price = -1;
  item(int price) : price(price){};
};

int calc_cost (std::unordered_map<std::string, item>& items, const std::string& name) {
  auto& target = items.at(name);
  if ( target.true_price != -1 )
    return target.true_price;

  if ( target.materials.empty() ){
    target.true_price = target.price;
    return target.true_price;
  }
  
  int sum_of_price = 0;
  for(const std::string& material : target.materials){
    sum_of_price += calc_cost(items, material);
  }

  return target.true_price = std::min(target.price, sum_of_price);
}

int main(){
  while(true) {
    std::unordered_map<std::string, item> items;
    int N;

    std::cin >> N;
    if (N == 0)
      break;

    for(int i = 0; i < N; ++i) {
      std::string name;
      int price;
      std::cin >> name >> price;
      items.insert({name, price});
    }

    std::cin >> N;
    for(int i = 0; i < N; ++i){
      std::string name;
      std::cin >> name;

      int M;
      std::cin >> M;
      std::vector<std::string> materials(M);
      for(int j = 0; j < M; ++j){
        std::string mat_name;
        std::cin >> mat_name;
        materials.at(j) = mat_name;
      }

      items.at(name).materials = materials;
    }

    std::string name;
    std::cin >> name;
    std::cout << calc_cost(items, name) << std::endl;
  }
}