#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main()
{
  for(;;){
    int n;
    cin >> n;
    if(n == 0) break;
    map<string,int> price;
    for(int i=0; i<n; i++){
      string s;
      int p;
      cin >> s >> p;
      price.insert(make_pair(s,p));
    }
    int m;
    cin >> m;
    map<string,vector<string> > recipe;
    for(int i=0; i<m; i++){
      string goal;
      cin >> goal;
      int j;
      cin >> j;
      int sum = 0;
      for(int k=0; k<j; k++){
        string src;
        cin >> src;
        sum += price[src];
        recipe[src].push_back(goal);
      }
      if(sum < price[goal]){
        int diff = price[goal] - sum;
        price[goal] = sum;
        vector<string>::iterator it;
        for(it = recipe[goal].begin(); it != recipe[goal].end(); it++){
          price[*it] -= diff;
        }
      }
    }
    string target;
    cin >> target;
    cout << price[target] << endl;    
  }
  return 0;
}