#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <algorithm>
using namespace::std;
int n;

struct item{
    string name;
    int cost;
    vector<string> used;
    int size;
} ;

item list[100];
int memo[100];

int getCost(int v)
{
    if(memo[v]!=-1)return memo[v];
    int i, j;
    int ans;
    
    if (list[v].size == 0){
        return (list[v].cost);
    }
    else {
        ans = 0;
        for (i = 0; i < list[v].size; i++){
            for (j = 0; j < n; j++){
                if (list[v].used[i]== list[j].name){
                    ans += getCost(j);
                }
            }
        }
        list[v].cost = min(list[v].cost,ans);
    }
    return (list[v].cost);
}

int main(void)
{
    int i, j, k;
    int m, kk;
    string key, temp;
    int look, sum;
    
    while (1){
        scanf("%d", &n);
        
        if (n == 0){
            break;
        }
        for(i=0;i<n;i++)memo[i]=-1;
        for (i = 0; i < n; i++){
            cin >> list[i].name >> list[i].cost;
            list[i].size = 0;
        }
        
        cin >> m;
        for (i = 0; i < m; i++){
            cin >> key;
            for (j = 0; j < n; j++){
                if (key==list[j].name){
                    look = j;
                }
            }
            cin >> kk;
            list[look].size = kk;
            for (j = 0; j < kk; j++){
                cin >> temp;
                for (k = 0; k < n; k++){
                    if (temp==list[k].name){
                        list[look].used[j]=temp;
                    }
                }
            }
        }
        cin >> key;
        for (j = 0; j < n; j++){
            if (strcmp(key, list[j].name) == 0){
                look = j;
            }
        }
        
        cout << getCost(look);
        
    }
    
    return (0);
}