#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
int main(){
  int n;
  map<string, int> items;
  string name;
  int value;
  
  while(cin >> n, n){
    for(int i = 0 ; i < n ; i++){
      cin >> name >> value;
      items.insert(make_pair(name, value));
    }
    int m;
    int total;
    string names[101];
    cin >> n;
    for(int i = 0 ; i < n ; i++){
      total = 0;
      cin >> name;
      cin >> m;
      for(int j = 0 ; j < m ; j++){
	cin >> names[j];
	total += items[names[j]];
      }
      items[name] = min(items[name], total);
    }
    cin >> name;
    cout << items[name] << endl;
  }
  return 0;
}