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

int main()
{
  int n, m;
  string s, s2;
  int k;
  map<string, int> cost;

  while(cin>>n, n){
    cost.clear();
    while(n-- > 0){
      cin>>s>>k;
      cost[s] = k;
    }
    cin>>n;
    while(n-- > 0){
      cin>>s>>m;
      k = 0;
      while(m-- > 0){
	cin>>s2;
	k += cost[s2];
      }
      if(cost[s] > k) cost[s] = k;
    }
    cin>>s;
    cout<<cost[s]<<endl;
  }
  return 0;
}