#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
#include <map>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<30;

map<string, int> mp;
map<string, vector<string> > mp2;

int solve(string s) {
  if (mp2.count(s)) {
    int a = 0;
    FOR(it, mp2[s])
      a += solve(*it);
    return min(a, mp[s]);
  }
  return mp[s];
}

int main() {
  int n;
  while(cin>>n, n) {
    mp.clear();
    REP(i,n) {
      string s;
      int p;
      cin >> s >> p;
      mp[s] = p;
    }
    int m;
    cin >> m;
    mp2.clear();
    REP(i,m) {
      string s;
      int k;
      cin >> s >> k;
      vector<string> v;
      REP(j,k) {
        string t;
        cin >> t;
        v.push_back(t);
      }
      mp2[s] = v;
    }
    string s;
    cin >> s;
    cout << solve(s) << endl;
  }
}