#include<iostream>
#include<queue>
#include<map>
#include<vector>
#include<cstdio>
#include<cstring>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)(n);++i)

map<string,int> dict;
int getId(string s) {
  if (dict.find(s) == dict.end()) {
      int n = dict.size();
      dict.insert(make_pair(s, n));
      return n;
  }
  return dict[s];
}

int price[128];
bool edge[128][128];
int ref[128];

int main() {
  for (;;) {
      dict.clear();
      memset(edge,0,sizeof(edge));
      int N; cin >> N; if (!N) break;
      REP(i, N) {
          string s;
          int p; cin >> s >> p;
          price[getId(s)] = p;
      }
      int M; cin >> M;
      REP(i, M) {
          string goal;
          int K;
          cin >> goal >> K;
          REP(k, K) {
              string elem;
              cin >> elem;
              edge[getId(elem)][getId(goal)] = true;
          }
      }
      memset(ref, 0, sizeof(ref));
      REP(i, N) REP(j, N) {
          if (edge[i][j]) ++ref[j];
      }
      for (;;) {
          bool update = false;
          REP(i, N) if (ref[i] == 0) {
              update = true;
              ref[i] = -1;
          }

          REP(i, N) if (ref[i] != -1) {
              int cnt = 0;
              REP(j, N) if (edge[j][i] && ref[j] == -1) ++cnt;
              if (cnt == ref[i]) {
                  int buy = 0;
                  REP(j, N) if (edge[j][i]) buy += price[j];
                  if (cnt > 0 && buy < price[i])
                      price[i] = buy;
                  ref[i] = 0;
              }
          }
          if (!update) break;
      }

      string goal; cin >> goal;
      cout << price[getId(goal)] << endl;
  }
  return 0;
}