#include <iostream>
#include <utility>
#include <map>
#include <vector>
using namespace std;
typedef map<string, int> m;

m price;
map<string,vector<string> > renkin;
string p[100];

int main() {
  int n;
  int m;
  while(cin >> n) {
    if(n==0) break;
    for(int i=0;i<n;i++) {
      string name;
      int p;
      cin >> name >> p;
      price.insert( pair<string,int>(name,p) );
    }

    cin >> m;
    for(int i=0;i<m;i++) {
      int sozai;
      cin >> p[i] >> sozai;
      for(int k=0;k<sozai;k++) {
        string name;
        cin >> name;
        renkin[p[i]].push_back(name);
      }
    }

    bool update = true;
    while(update) {
      update = false;
      for(int i=0;i<m;i++) {
        int sum = 0;
        for(int j=0;j<renkin[p[i]].size();j++) {
          sum += price[renkin[p[i]][j]];
        }
        if(sum < price[p[i]]) {
          price[p[i]] = sum;
          update = true;
        }
      }
    }
    string str;
    cin >> str;
    cout << price[str] << endl;
    price.clear();
    renkin.clear();
  }
}