/*
 * 0201.cc: Wrought Gold Master
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100;
const int INF = 1 << 30;

/* typedef */

typedef vector<int> vi;
typedef map<string,int> msi;

/* global variables */

string is[MAX_N];
msi imap;
int ps[MAX_N], cache[MAX_N];
vi chlds[MAX_N];
bool used[MAX_N];

/* subroutines */

int rec(int k) {
  if (cache[k] >= 0) return cache[k];
  if (used[k]) return ps[k];
  if (chlds[k].size() == 0) return (cache[k] = ps[k]);

  used[k] = true;
  int sum = 0;
  for (int i = 0; i < chlds[k].size(); i++)
    sum += rec(chlds[k][i]);
  used[k] = false;

  return (cache[k] = min(sum, ps[k]));
}

/* main */

int main() {
  for (;;) {
    int n;
    cin >> n;
    if (n == 0) break;

    imap.clear();
    memset(cache, -1, sizeof(cache));
    memset(used, false, sizeof(used));
    
    for (int i = 0; i < n; i++) {
      cin >> is[i] >> ps[i];
      imap[is[i]] = i;
      chlds[i].clear();
    }

    int m;
    cin >> m;

    for (int i = 0; i < m; i++) {
      string si;
      int ki;
      cin >> si >> ki;
      int x = imap[si];

      for (int j = 0; j < ki; j++) {
	string sj;
	cin >> sj;
	chlds[x].push_back(imap[sj]);
      }
    }

    string gl;
    cin >> gl;

    int ans = rec(imap[gl]);

    //for (int i = 0; i < n; i++) {
    //  cout << is[i];
    //  printf(" %d (%d)\n", cache[i], ps[i]);
    //}
    printf("%d\n", ans);
  }

  return 0;
}