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

struct material
{
    int price;
    vector<string> need;
};

int min_price(map<string, material>& mmap, string s)
{
    if (!mmap[s].need.empty()){
        int sum = 0;
        for (string name : mmap[s].need){
            sum += min_price(mmap, name);
        }
        mmap[s].price = min(mmap[s].price, sum);
        mmap[s].need.clear();
    }

    return mmap[s].price;
}

void solve(int n)
{
    int m;
    map<string, material> mmap;

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

        cin >> s >> p;
        mmap[s] = { p, vector<string>() };
    }

    cin >> m;
    for (int i = 0; i < m; i++){
        string o;
        int k;
        int sum = 0;

        cin >> o >> k;
        while (k-- > 0){
            string q;

            cin >> q;
            mmap[o].need.push_back(q);
        }
    }
    for (auto i = mmap.begin(); i != mmap.end(); i++){
        i->second.price = min_price(mmap, i->first);
    }

    string s;
    cin >> s;
    printf("%d\n", mmap[s].price);
}

int main()
{
    int n;

    while (cin >> n, n){
        solve(n);
    }

    return 0;
}