#include <iostream>
#include <vector>
#include <string>
#include <map>

using namespace std;

typedef map<string, int> item_price_map;
//typedef map<string, vector<string>> renkin_map;

item_price_map price_table;

bool get_input() {
    int n, m;

    string name;
    int price;
    cin >> n;
    if (n == 0) return false;
    for (int i = 0; i < n; i++) {
        cin >> name >> price;
        price_table[name] = price;
    }

    vector<string> items;
    int count;
    cin >> m;
    for (int i = 0; i < m; i++) {
        cin >> name >> count;
        items.resize(count);
        for (int j = 0; j < count; j++) {
            cin >> items[j];
        }
        int min_price = 0;
        for (int j = 0; j < count; j++) {
            min_price += price_table[items[j]];
        }
        price_table[name] = min_price < price_table[name] ? min_price : price_table[name];
    }

    return true;
}

void solve() {
    string name;
    cin >> name;
    cout << price_table[name] << endl;
}

int main() {
    while (get_input()) {
        solve();
    }
    return 0;
}