#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
long long func(string name,  map<string,int> &list,  map<string,vector<string> > &renkin){
  if(renkin.find(name)==renkin.end()) return list[name];
  long long ret = 0;
  vector<string> mat = renkin[name];
  REP(i,mat.size()){
    ret += min((long long)list[mat[i]], func(mat[i],list,renkin));
  }
  return ret;
}

int main(void){
  int n,m;
  while(cin>>n){
    if(n==0) break;
    map<string,int> list;
    map<string,vector<string> > renkin;
    REP(j,n){
      string i;
      int p;
      cin>>i>>p;
      list[i] = p;
    }
    cin>>m;
    REP(i,m){
      string o;
      int k;
      cin>>o>>k;
      vector<string> q(k);
      REP(j,k) cin>>q[j];
      renkin[o] = q;
    }
    string item;
    cin>>item;
    cout<<func(item,list,renkin)<<endl;
  }
  return 0;
}