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

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 ari = 0;
  rep(i,reci[to].size()){
    ari += solve(reci[to][i]);
  }
  if(ari == 0){
    ari = buy[to];
  }
  
  int rtn = min(ari, dp[to]);
  dp[to] = rtn;
  return rtn;
}


int main(){
  int n;
  while(cin >> n, n){
    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);
      }
    }

    // solve

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

    