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

int main(){
  int n;
  map<string,int> M,T;
  while( cin >> n,n){
    M.clear();
    string name;
    for(int i = 0 ; i < n ; i++ ){
      int price;
      cin >> name >> price;
      M[name] = price;
    }
    int sumprice = 0;
    string nam;
    cin >> n;
    for(int i = 0 ; i < n ; i++ ){
      int s;
      cin >> nam >> s;
      for(int j = 0 ; j < s ; j++ ){
	cin >> name;
	T[nam] += M[name];
      }
      M[nam] = min(M[nam],T[nam]);
    }
    cin >> name;
    cout << M[name] << endl;
  }
  return 0;
}