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

int main(){
  int n, price, m, sum;
  string item, ingre, wantMake;
  while(cin >> n, n){
    sum = 0;
    map <string,int> itemPrice;
    while(n--){
      cin >> item >> price;
      itemPrice.insert(map<string,int>::value_type(item,price));
    }
    cin >> m;
    while(m--){
      sum = 0;
      cin >> item >> price;
      while(price--){
	cin >> ingre;
	if(itemPrice.find(ingre)!=itemPrice.end()){
          sum += itemPrice[ingre];
	}
      }
      if(itemPrice[item]>sum){
	itemPrice[item] = sum;
      }
    }
    cin >> wantMake;
    cout << itemPrice[wantMake] <<endl;
  }
}