#include <cstdio>
#include <string>
#include <iostream>
#include <map>
#include <vector>

#define reep(i,n,m) for(int i=n;i<m;i++)
#define rep(i,n) reep(i,0,n)

using namespace std;

map<string,int> name_id;
vector<int> recipe[100];
int price[100];

int solve(int x){
    if(recipe[x].size() == 0){
        return price[x];
    }

    int ans=0;
    rep(i,recipe[x].size()){
        ans += min(solve(recipe[x][i]), price[recipe[x][i]]);
    }
    return ans;
}

int main(){
    int n;
    while(cin >> n,n){
        rep(i,n){
            string name;
            int p;
            cin >> name >> p;
            name_id[name] = i;
            price[i] = p;
        }

        int m;
        cin >> m;
        rep(i,m){
            string name;
            int num;
            cin >> name >> num;
            int id = name_id[name];
            rep(k,num){
                string r;
                cin >> r;
                recipe[id].push_back(name_id[r]);
            }
        }

        string a;
        cin >> a;
        cout << solve(name_id[a]) << endl;
    }
    return 0;
}