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

#define min(a,b) ((a)<(b)?(a):(b))

map<string, int> item;
map<string, vector<string> > recipe;

int get_lowest_price(string name) {
    if (recipe.find(name) == recipe.end()) {
        return item[name];
    }
    vector<string> m = recipe[name];
    int mat_price = 0;
    for (vector<string>::iterator e = m.begin(); e != m.end(); e++) {
        mat_price += get_lowest_price(*e);
    }
    return min(item[name], mat_price);
}

int main() {
    string name;
    int price, i, n, m;

    while ((cin >> n) && n) {
        item.clear();
        for (i = 0; i < n; i++) {
            cin >> name >> price;
            item[name] = price;
        }

        cin >> m;
        recipe.clear();
        for (i = 0; i < m; i++) {
            vector<string> material;
            int k;
            cin >> name >> k;
            while (k--) {
                string itm;
                cin >> itm;
                material.push_back(itm);
            }
            recipe[name] = material;
        }
        cin >> name;
        cout << get_lowest_price(name) << endl;

    }
    return 0;
}