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

constexpr int INF = 1e9;

int main() {
    int n;
    while(cin >> n, n) {
        map<string, int> cost;
        for(int i=0; i<n; ++i) {
            string s;
            int p;
            cin >> s >> p;
            cost[s] = p;
        }
        map<string, vector<string>> recipe;
        int m;
        cin >> m;
        for(int i=0; i<m; ++i) {
            string o;
            int k;
            cin >> o >> k;
            for(int j=0; j<k; ++j) {
                string q;
                cin >> q;
                recipe[o].push_back(q);
            }
        }

        for(int i=0; i<n; ++i) {
            for(auto& p : cost) {
                if(recipe.count(p.first) == 0) {
                    continue;
                }
                int sum = 0;
                auto& r = recipe[p.first];
                for(int j=0; j<r.size(); ++j) {
                    sum += cost[r[j]];
                }
                p.second = min(p.second, sum);
            }
        }
        string t;
        cin >> t;
        cout << cost[t] << endl;
    }
}
