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

class Item{
public:
    string name;
    int cost;
    vector<string> subItems;
    int calc();
};

const int MAX_N = 100;
Item items[MAX_N + 1];
map<string, int> item_idx;

int main() {
    while (true) {
        int n;
        cin >> n;
        if (n == 0) {
            break;
        }
        item_idx.clear();
        for (int i = 0; i < n; i++) {
            cin >> items[i].name >> items[i].cost;
            item_idx.insert(make_pair(items[i].name, i));
        }
        int m;
        cin >> m;
        for (int i = 0; i < m; i++) {
            string name;
            int k;
            cin >> name >> k;
            for (int j = 0; j < k; j++) {
                string sub;
                cin >> sub;
                items[item_idx[name]].subItems.push_back(sub);
            }
        }
        string name;
        cin >> name;
        cout << items[item_idx[name]].calc() << endl;
    }
}

int Item::calc() {
    int result = cost;
    if (subItems.size() > 0) {
        result = 0;
        for (vector<string>::iterator it = subItems.begin(); it != subItems.end(); it++) {
            result += items[item_idx[*it]].calc();
        }
    }
    return result;
}