#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <bitset>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int N, M;
map<string, int> id;
int A[100], memo[100];
vector<int> G[100];

int dfs(int x) {
  if (memo[x] != -1) return memo[x];
  int m = A[x];
  if (G[x].size()) {
    int s = 0;
    for (int t : G[x]) s += dfs(t);
    m = min(m, s);
  }
  return memo[x] = m;
}

int read() {
  string s;
  cin >> s;
  assert(id.find(s) != id.end());
  return id[s];
}

signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  while (cin >> N) {
    if (N == 0) break;
    id.clear();
    rep(i, N) G[i].clear();
    rep(i, N) {
      string s;
      cin >> s >> A[i];
      id[s] = i;
    }
    cin >> M;
    rep(_, M) {
      int s = read();
      int k;
      cin >> k;
      rep(i, k) G[s].pb(read());
    }
    rep(i, N) memo[i] = -1;
    int o = read();
    cout << dfs(o) << "\n";
  }
  return 0;
}