#include <iostream>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;

typedef pair<int, int> P;
typedef map<string, P> IM;
const int NMAX = 100;
int N, M;
int depends[NMAX][NMAX];
int origCosts[NMAX];
int target;
int counts[NMAX];
int costs[NMAX];
string names[NMAX];

bool input() {
  IM items;

  cin >> N;
  if (!N) {
    return false;
  }
  for (int i = 0; i < N; ++i) {
    string name;
    int cost;
    cin >> name;
    cin >> cost;
    items.insert(IM::value_type(name, P(i, cost)));
    names[i] = name;
    origCosts[i] = cost;
  }
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      depends[i][j] = 0;
    }
  }
  cin >> M;
  for (int i = 0; i < M; ++i) {
    string s1;
    int num;
    cin >> s1 >> num;
    int a = items[s1].first;
    for (int j = 0; j < num; ++j) {
      string s2;
      cin >> s2;
      int b = items[s2].first;
      ++depends[a][b];
    }
  }

  string st;
  cin >> st;
  target = items[st].first;
  return true;
}

int solve() {
  for (int i = 0; i < N; ++i) {
    counts[i] = 0;
  }
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      if (depends[i][j]) {
        ++counts[i];
      }
    }
  }

  queue<int> q;
  while (1) {
    for (int i = 0; i < N; ++i) {
      if (!counts[i]) {
        q.push(i);
      }
    }
    if (q.empty()) {
      break;
    }
    while (!q.empty()) {
      int x = q.front();
      q.pop();
      int y = 0;
      bool assembled = false;
      for (int i = 0; i < N; ++i) {
        if (depends[x][i]) {
          y += depends[x][i] * costs[i];
          assembled = true;
        }
      }
      if (!assembled) {
        costs[x] = origCosts[x];
      } else {
        costs[x] = min(origCosts[x], y);
      }
      --counts[x];
      for (int i = 0; i < N; ++i) {
        if (depends[i][x]) {
          --counts[i];
        }
      }
    }
  }

  return costs[target];
}

int main() {
  while (input()) {
    cout << solve() << endl;
  }
}