#include <iostream>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

map<string, vector<string> > recipe;
map<string, int> cost;

int search(string item){
    if(recipe[item].size() == 0) return cost[item];
    else {
        int sum = 0;
        for(int i=0; i<recipe[item].size(); i++){
            sum += search(recipe[item][i]);
        }
        return min(cost[item], sum);
    }
}

int main(){
    int n;
    while(cin >> n, n){
        recipe.clear(); cost.clear();
        for(int i=0; i< n; i++){
            string name; int m;
            cin >> name >> m;
            cost[name] = m;
        }
        cin >> n;
        for(int i=0; i< n; i++){
            string item;
            cin >> item;
            int m;
            cin >> m;
            for(int j=0; j < m; j++){
                string material;
                cin >> material;
                recipe[item].push_back(material);
            }
        }
        string item;
        cin >> item;
        cout << search(item) << endl;
    }
}