#include "bits/stdc++.h"

using namespace std;

typedef pair<int,int> pii;
typedef vector<int> vi;

#define dump(x)  cerr << #x << " = " << (x) << endl
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF (999999999/2)
map< string,vector<string> > pas;
map<string,int> mp;

int dfs(string s){
    int ret=0;
    if(pas.find(s)==pas.end()){
        return mp[s];
    }
    else{
        rep(i,pas[s].size()){
            ret+=dfs(pas[s][i]);
        }
    }
    return min(ret,mp[s]);
}

int main(){
    int n,m;
    while(cin>>n&&n){
        pas.clear();
        mp.clear();
        rep(i,n){
            string s;
            int a;
            cin>>s>>a;
            mp[s]=a;
        }
        cin>>m;
        rep(i,m){
            string s;
            cin>>s;
            int k;
            cin>>k;
            rep(j,k){
                string x;
                cin>>x;
                pas[s].pb(x);
            }
        }
        string a;
        cin>>a;
        int sum=dfs(a);
        cout<<sum<<endl;
    }
}