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

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