#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;

int main() {
    int n;
    while (cin >> n, n) {
        string i; int p;
        map<string, int> price;
        for (int j=0; j<n; ++j) {
            cin >> i >> p;
            price[i] = p;
        }
        int m;
        cin >> m;
        vector<vector<string>> recipe(m);
        string o, q; int k;
        for (int j=0; j<m; ++j) {
            cin >> o;
            recipe[j].push_back(o);
            cin >> k;
            for (int l=0; l<k; ++l) {
                cin >> q;
                recipe[j].push_back(q);
            }
        }
        for (int j=0; j<m; ++j) {
            for (int l=0; l<m; ++l) {
                int new_price = 0;
                for (int kk=1; kk<(int)recipe[l].size(); ++kk) {
                    new_price += price[recipe[l][kk]];
                }
                price[recipe[l][0]] = min(price[recipe[l][0]], new_price);
            }
        }
        string name;
        cin >> name;
        cout << price[name] << endl;
    }
    return 0;
}