#include <iostream>
#include <vector>
#include <map>

using namespace std;

int n, m;
struct Item {
    int id, price;
    string name;
    vector<int> edge;
    Item() {}
};
vector<Item> Graph;
int memo[101];

int Rec(int id)
{
    if (memo[id] != -1)
        return memo[id];

    int price = Graph[id].price, cmp = 0;

    for (int i = 0; i < Graph[id].edge.size(); ++i)
        cmp += Rec(Graph[id].edge[i]);

    return memo[id] = (cmp == 0) ? price : min(price, cmp);
}

int main()
{
    while (cin >> n, n) {
        string s, d;
        map<string, int> hash;

        Graph.resize(n);
        for (int i = 0; i < n; ++i) {
            memo[i] = -1;
            Graph[i].edge.clear();
        }

        for (int i = 0; i < n; ++i) {
            int p;

            cin >> s >> p;
            hash.insert(map<string, int>::value_type(s, i));
            Graph[i].id = i;
            Graph[i].price = p;
            Graph[i].name = s;
        }

        cin >> m;
        for (int i = 0; i < m; ++i) {
            int num;

            cin >> s >> num;
            for (int j = 0; j < num; ++j) {
                cin >> d;
                Graph[hash[s]].edge.push_back(hash[d]);
            }
        }

        cin >> s;
        cout << Rec(hash[s]) << endl;
    }

    return 0;
}