#include <cstdio>
#include <string>
#include <map>
#include <vector>
using namespace std;

map <string, int> item;
map <string, vector<string> > recipe;
int n, m, k, money;
char name[101], o[101], material[101];

int dp(string str);

int main(void)
{
    
    while (1){
        scanf("%d", &n);
        if (n == 0) break;
        for (int i = 0; i < n; i++){
            scanf("%s %d", name, &money);
            item.insert( map<string, int>::value_type( name, money ) );
        }
        
        scanf("%d", &m);
        for (int i = 0; i < m; i++){
            scanf("%s %d", o, &k);
            for (int j = 0; j < k; j++){
                scanf("%s", material);
                recipe[o].push_back(material);
            }
        }
        
        scanf("%s", name);
        printf("%d\n", dp(name));
        
        item.clear();
        recipe.clear();
    }
    
    return (0);
}

int dp(string str)
{
    int ans = 0;
    
    if (recipe[str].size() != 0){
        for (int i = 0; i < recipe[str].size(); i++){
            ans += dp(recipe[str][i]);
        }
    }
    else {
        ans = item[str];
    }
    
    if (item[str] < ans) ans = item[str];
    
    return ans;
}