#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

int N, M;

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

int rec(string query)
{
  int res = 1000000;
  if (recipe.find(query) != recipe.end()) {
    int tmp = 0;
    for (int i = 0; i < recipe[query].size(); i++) {
      tmp += rec(recipe[query][i]);
    }
    res = min(res, tmp);
  }
  
  res = min(res, mp[query]);
  
  return res;
}

int main()
{
  while (cin >> N && N) {
    mp.clear();
    recipe.clear();
    for (int i = 0; i < N; i++) {
      string key; int val;
      cin >> key >> val;
      mp[key] = val; 
    }
    
    cin >> M;
    for (int i = 0; i < M; i++) {
      int num;
      string key, val;
      vector<string> vs;
      cin >> key >> num;
      for (int j = 0; j < num; j++) {
        cin >> val;
        vs.push_back(val);
      }
      
      recipe[key] = vs;
    }
    
    string q;
    cin >> q;
    cout << min(rec(q), mp[q]) << endl;
  }
}