#include <iostream>
#include <map>
using namespace std;

map<string,int> price;
map<string,int> id;
map<int,string> di;
bool table[100][100];

int calcPrice(string t, int n) {

    bool flag = false;
    int tmp = 0;
    for(int i = 0; i < n; i++) {
       if(table[id[t]][i]) {
           flag = true;
           tmp += calcPrice(di[i], n);
       }
    }
    if(!flag) return price[t];

    if(tmp > price[t]) return price[t];

    return tmp;
}


int main(void){
    // Your code here!
    int n;
    while(true) {
        cin >> n;
        if(n==0) break;

        price.clear();
        id.clear();
        di.clear();
        
        string s;
        int p;
        for(int i = 0; i < n; i++) {
            cin >> s >> p;
            price[s] = p;
            id[s] = i;
            di[i] = s;
        }

        for(int i = 0; i < 100; i++) {
            for(int j = 0; j < 100; j++) {
                table[i][j] = false;
            }
        }
        
        int m;
        cin >> m;
        for(int i = 0; i < m; i++) {
            int k;
            string o;
            cin >> o >> k;
            string q;
            for(int j = 0; j < k; j++) {
                cin >> q;
                table[id[o]][id[q]] = true;
            }
        }
        string t;
        cin >> t;
        cout << calcPrice(t, n) << endl;
    }
}

