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

struct Item{
  string name;

  int cost;
};

struct recipe{
  vector<string> needs;
  string product;
};

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

    string name;
    int cost;
    vector<Item> itemlist;
    Item item;

    while(n--){
      cin>>name>>cost;
      item.name=name;
      item.cost=cost;
      itemlist.push_back(item);
    }

    cin>>n;

    int num;
    recipe r;
    vector<recipe> repices;

    while(n--){
      cin>>name>>num;
      r.product=name;
      r.needs.clear();

      while(num--){
	cin>>name;
	r.needs.push_back(name);
      }

      repices.push_back(r);
    }

    for(int p=0; p<repices.size(); p++){
      for(int i=0; i<repices.size(); i++){
	int t=0;

	for(int j=0; j< repices[i].needs.size(); j++){
	  for(int k=0; k<itemlist.size(); k++){
	    if(repices[i].needs[j] == itemlist[k].name) {
	      t+=itemlist[k].cost;
	    }
	  }
	}

	for(int j=0; j < itemlist.size(); j++)
	  if(repices[i].product == itemlist[j].name && t < itemlist[j].cost)
	    itemlist[j].cost = t,t=0;
      }
    }

    cin>>name;

    for(int j=0; j < itemlist.size(); j++){
      if(itemlist[j].name == name ) cout<<itemlist[j].cost<<endl;
    }

  }
}