#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstdio>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double ESP=1e-10;
using namespace std;
int main(){
  int n;
  while(cin>>n,n){
    map<string,int>in;
    rep(i,n){
      string s;
      int tmp;
      cin>>s>>tmp;
      in[s]=tmp;
    }
    int m;
    cin>>m;
    rep(i,m){
      string s;
      int k;
      cin>>s>>k;
      int co=0;
      rep(j,k){
	string tmp;
	cin>>tmp;
	co+=in[tmp];
      }
      if(in[s]>co)in[s]=co;
    }
    string out;
    cin>>out;
    cout<<in[out]<<endl;
  }

}