#include <bits/stdc++.h>

using namespace std;

struct Node {
    int cost;
    vector<string> to;
};

map<string, Node> price;

int solve(string item) {

    if(price[item].to.size() == 0) return price[item].cost;

    int sum = 0;
    for(int i = 0; i < price[item].to.size(); ++i) {
        sum += solve(price[item].to[i]);
    }

    return min(price[item].cost, sum);

}

int main() {

    int n, m, p, k;
    string s, o, q, t;

    while(cin >> n, n) {

        for(map<string, Node>::iterator it = price.begin(); it != price.end(); ++it) {
            vector<string> &to = it->second.to;
            to.erase(to.begin(), to.end());
        }

        for(int i = 0; i < n; ++i) {
            cin >> s >> p;
            price[s].cost = p;
        }

        cin >> m;

        for(int i = 0; i < m; ++i) {
            cin >> o >> k;
            for(int j = 0; j < k; ++j) {
                cin >> q;
                price[o].to.push_back(q);
            }
        }

        cin >> t;

        cout << solve(t) << endl;

    }

}