#include <iostream>
#include <string>
#include <queue>
#include <map>

std::map< std::string, unsigned long int > mp;
std::map< std::string, std::queue< std::string > > recipe;

int n, m;
std::string t;

unsigned long int wrought(std::string s) {
    if ( recipe.find(s) == recipe.end() ) {
        return mp[s];
    } else {
        std::queue< std::string > q = recipe[s];
        unsigned long int res = 0;
        while (!q.empty()) {
            std::string t = q.front();
            q.pop();
            res += wrought( t );
        }
        return std::min(res, mp[s]);
    }
}

void solve(std::string t) {
    unsigned long int res = std::min(mp[t], wrought( t ));
    std::cout << res << std::endl;
}

int main() {
    while (std::cin >> n, n) {
        // initialize map
        mp.clear();
        recipe.clear();

        // get input
        for (int i=0; i<n; i++) {
            std::string s;
            unsigned long int price;
            std::cin >> s >> price;
            mp[s] = price;
        }
        std::cin >> m;
        for (int i=0; i<m; i++) {
            std::string obj;
            int k;
            std::cin >> obj >> k;
            for (int j=0; j<k; j++) {
                std::string q;
                std::cin >> q;
                recipe[obj].push( q );
            }
        }
        std::string t;
        std::cin >> t;
        solve( t );
    }
    return 0;
}

