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

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

void solve(int);

int main()
{
    int n;

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

    return 0;
}

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);
            sum += mmap[q].price;
        }
        mmap[o].price = min(mmap[o].price, sum);
    }

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