#include<iostream>
#include<vector>
#include<string>
#include <map>
#include<algorithm>
using namespace std;
int search(string, pair<string, int>*, pair<string, vector<string> >*);
int n;
int m,si;
int main()
{
     
    while (true){
        pair<string, int>p[101];
        pair<string, vector<string> >chan[101];
        cin >> n;
        if (n == 0){
            break;
        }
        for (int i = 0; i < n; i++){
            cin >> p[i].first >> p[i].second;
        }
 
        cin >> m;
        for (int i = 0; i < m; i++){
            cin >> chan[i].first >> si;
            for (int i2 = 0; i2 < si; i2++){
                string s;
                cin >> s;
                chan[i].second.push_back(s);
            }
        }
        string s;
        cin >> s;
        int ans = search(s,p,chan);
        cout << ans << endl;
    }
}
int search(string s, pair<string, int>*p, pair<string, vector<string> >*chan){
    int num = 0;
    bool flag = true;
    for (int i = 0; i < m; i++){
        if (chan[i].first == s){
            flag = false;
            for (int i2 = 0; i2 < chan[i].second.size(); i2++){
                num += search(chan[i].second[i2],p,chan);
            }break;
        }
    }
    if (!flag){
        int k;
        for (int i = 0; i < n; i++){
            if (p[i].first == s){
                num=min(p[i].second,num);
                break;
            }
        }
    }
    if (flag){
        for (int i = 0; i < n; i++){
            if (p[i].first == s){
                num += p[i].second;
            }
        }
    }
    return num;
}