#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <sstream>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define min(a, b) (a > b ? b : a)

struct Item {
    int price;
    std::vector<std::string> recipe;
};

int main() {
    std::map<std::string, Item> dict;
    int n;
    while (std::cin >> n && n) {
        std::string s, t;
        int p;
        rep(i, n) {
            std::cin >> s >> p;
            dict[s].price = p;
        }

        int k, l;
        std::cin >> k;
        rep(i, k) {
            std::cin >> s >> l;

            std::getline(std::cin, t);
            std::stringstream ss(t);
            std::copy(std::istream_iterator<std::string>(ss), {},
                      std::back_inserter(dict[s].recipe));
        }

        std::function<int(std::string)> min_cost = [&](std::string name) {
            int res = dict[name].price;
            if (dict[name].recipe.size() == 0) return res;

            int rec = 0;
            for (auto i : dict[name].recipe) rec += min_cost(i);
            return min(res, rec);
        };

        std::cin >> s;
        std::cout << min_cost(s) << std::endl;
        dict.clear();
    }
}