#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

#define pb push_back
#define rep(i, a, n) for(int i = (a); i < (n); i++)
#define dep(i, a, n) for(int i = (a); i >= (n); i--)
#define mod 1e9+7

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

int main() {
  int n;
  while(cin >> n) {
    if(!n) return 0;
    map<string, int> m;
    rep(i, 0, n) {
      string s;
      int p;
      cin >> s >> p;
      m[s] = p;
    }

    cin >> n;
    string o[100];
    vector<string> q[100];
    rep(i, 0, n) {
      cin >> o[i];
      int k;
      cin >> k;
      rep(j, 0, k) {
        string sum;
        cin >> sum;
        q[i].pb(sum);
      }
    }

    rep(i, 0, 100) {
      rep(j, 0, n) {
        int f = 0;
        int gou = 0;
        rep(k, 0, q[i].size()) {
          if(m.count(q[i][k])) {
            gou += m[q[i][k]];
          }else f = 1;
        }
        if(!f) {
          if(m.count(o[i])) {
            m[o[i]] = min(m[o[i]], gou);
          }else m[o[i]] = gou;
        }
      }
    }

    string s;
    cin >> s;
    cout << m[s] << endl;
  }
}