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

map<string, int> item;
map<string, vector<string>> recipe;
int rec(string s) {
    if (!recipe.count(s)) return item[s];
    int cost = 0;
    for (string t: recipe[s]) cost += rec(t);
    return min(item[s], cost);
}

int main() {
    int n;
    while (cin >> n, n) {
        item.clear();
        while (n--) {
            string s; int p; cin >> s >> p; item[s] = p;
        }
        int m; cin >> m;
        recipe.clear();
        while (m--) {
            string s; int k; cin >> s >> k;
            while (k--) {
                string t; cin >> t; recipe[s].emplace_back(t);
            }
        }
        string r; cin >> r;
        cout << rec(r) << endl;
    }
    return 0;
}
