#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <fstream>
#include <sstream>
using namespace std;
typedef long long ll;

map<string, int> item;
map<string, vector<string> > recipe;
map<string, int> dp;

int solve(string s){
    if(dp.count(s))return dp[s];
    
    int res = 1 << 29;
    if(item.count(s)){
        res = min(res, item[s]);
    }
    
    if(recipe.count(s)){
        int total = 0;
        
        vector<string> &grp = recipe[s];
        for(int i=0;i<grp.size();i++)total += solve(grp[i]);
        
        res = min(res, total);
    }
    
    return dp[s] = res;
}

int main(){
    int N, M;
    while(cin >> N && N){
        item.clear();
        recipe.clear();
        dp.clear();
        
        for(int i=0;i<N;i++){
            string name;
            int price;
            
            cin >> name >> price;
            item[name] = price;
        }
        
        cin >> M;
        for(int i=0;i<M;i++){
            string name;
            int k;
            
            cin >> name >> k;
            vector<string> grp(k);
            
            for(int j=0;j<k;j++)cin >> grp[j];
            
            recipe[name] = grp;
        }
        
        string target;
        cin >> target;
        
        cout << solve(target) << endl;
    }
}