#include <bits/stdc++.h>
using namespace std;

int main(){
    int n;
    while(cin >> n , n){
        map<string,int> id;
        vector<int> p(n);
        for(int i=0;i<n;i++){
            string s;
            cin >> s >> p[i];
            id[s] = i;
        }
        int m;
        cin >> m;
        vector<pair<int,vector<int> > > recipe(m);
        for(int i=0;i<m;i++){
            string o;
            int k;
            vector<int> ls;
            cin >> o >> k;
            for(int j=0;j<k;j++){
                string q;
                cin >> q;
                ls.push_back(id[q]);
            }
            recipe[i] = make_pair(id[o],ls);
        }
        bool update = true;
        while(update){
            update = false;
            for(int i=0;i<m;i++){
                int sum = 0;
                for(auto j : recipe[i].second){
                    sum += p[j];
                }
                if(p[recipe[i].first] > sum){
                    p[recipe[i].first] = sum;
                    update = true;
                }
            }
        }
        string t;
        cin >> t;
        cout << p[id[t]] << endl;
    }
    return 0;
}
