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

int main(){
  int itemn;
  int goldn;
  int goldnn;
  int f;
  int outp;
  string goldname;
  string goldpname;
  vector<int> outw;
  int lk;
  for(lk = 0; ;lk++){
    cin >> itemn;
    if(itemn == 0){
      break;
    }
    typedef struct item_s{
      vector<string> num;
      vector<int> price;
    }item_sd;
    item_sd item;
    item.num.resize(itemn);
    item.price.resize(itemn);

    for(int n = 0;n<itemn;n++){
      cin >> item.num[n];
      cin >> item.price[n];
    }
    cin >> goldn;
    for(int a = 0;a<goldn;a++){
      cin >> goldname;
      for(int z = 0;z<itemn;z++){
        if(item.num[z] == goldname){
          item.price[z] = 0;
          f = z;
        }
      }
      cin >> goldnn;
      for(int l = 0;l<goldnn;l++){
        cin >> goldpname;
        for(int z = 0;z<itemn;z++){
          if(item.num[z] == goldpname){
            item.price[f] += item.price[z];
          }
        }
      }
    }
    cin >> goldpname;
    for(int z = 0;z<itemn;z++){
      if(item.num[z] == goldpname){
        outw.resize(lk+1,item.price[z]);
      }
    }
  }
  for(int y = 0;y<lk;y++){
    cout << outw[y] << endl;
  }
  return 0;
}