#include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;

int N, M;

int solve(string tar, map<string, int> &item, map<string, vector<string> > &recipe){
  if(recipe.find(tar) == recipe.end()) return item[tar];
  int res = item[tar], sum = 0;
  REP(i, recipe[tar].size()) sum += solve(recipe[tar][i], item, recipe);
  return min(res, sum);
}

int main() {
  while(cin >>N && N){
    map<string, vector<string> > recipe;
    map<string, int> item;
    REP(i, N){
      string s; int n; cin >>s >>n;
      item[s] = n;
    }
    cin >>M;
    REP(i, M){
      string s; cin >>s;
      int n; cin >>n;
      recipe[s] = vector<string>(n);
      REP(j, n){
        string t; cin >>t;
        recipe[s][j] = t;
      }
    }
    string tar; cin >>tar;
    cout <<solve(tar, item, recipe) <<endl;
  }
  return 0;
}