#include<iostream>
#include<map>
#include<vector>

using namespace std;

typedef long long ll;

const int MAX = 100;
int n;
string name;

int main(){

  while(cin >> n && n){
    map<string,ll> M;
    for(int i = 0; i < n; i++){
      int price;
      cin >> name >> price;
      M[name] = price;
    }
    int rev;
    cin >> rev;
    for(int i = 0; i < rev; i++){
      int num;
      cin >> name >> num;
      ll sum = 0;
      for(int j = 0; j < num; j++){
	string str;
	cin >> str;
	sum += M[str];
      }
      M[name] = min(M[name],sum);
    }
    cin >> name;
    cout << M[name] << endl;
  }
  return 0;
}