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

using namespace std;

map<string,int> dp;
map<string,int> buy;
map<string, vector<string> > reci;

const int INF = 1000000;

#define rep(i,a) for(int i=0; i<(a); i++)

int solve(string to){
  int rtn;
  if(dp[to] == INF){
    if(reci[to].size() != 0){
      int ari = 0;
      rep(i,reci[to].size()){
	ari += solve(reci[to][i]);
      }
      rtn = min(buy[to] , ari);
    } else {
      rtn = buy[to];
    }
    dp[to] = rtn;
  } else {
    rtn = dp[to];
  }
  return rtn;
}


int main(){
  int n;
  while(cin >> n, n){
    dp.clear();
    buy.clear();
    reci.clear();
    rep(i,n){
      string str; int p;
      cin >> str >> p;
      buy[str] = p;
      dp[str] = INF;
    }
    int r; cin >> r;
    rep(i,r){
      string str; int m;
      cin >> str >> m;
      rep(j,m){
	string ing;
	cin >> ing;
	reci[str].push_back(ing);
      }
    }

    string to; cin >> to;
    cout << solve(to) << endl;
  }
  return 0;
}

    