#include <bits/stdc++.h>
using namespace std;
#define int long long

int dfs(string t, map<string, int> cost, map<string, vector<string>> recipe) {
    int res = 0;
    
    if (recipe.find(t)!=recipe.end()) {
        for (auto& qi : recipe[t]) {
            res += dfs(qi, cost, recipe);
        }
        
        return min(res, cost[t]);
    }
    else {
        return cost[t];
    }
}

signed main() {
    while (true) {
        int n;
        cin >> n;
        
        if (n==0) {
            break;
        }
        
        map<string, int> cost;
        
        for (int i=0; i<n; i++) {
            string si;
            int pi;
            cin >> si >> pi;
            cost[si] = pi;
        }
        
        int m;
        cin >> m;
        
        map<string, vector<string>> recipe;
        
        for (int i=0; i<m; i++) {
            string oi;
            int ki;
            cin >> oi >> ki;
            
            for (int j=0; j<ki; j++) {
                string qi;
                cin >> qi;
                recipe[oi].push_back(qi);
            }
        }
        
        string t;
        cin >> t;
        cout << dfs(t, cost, recipe) << endl;
    }
}
