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

int main(){
    int n;
    while (cin>>n,n){
        map<string, int>items;
        for (int i = 0; i < n; i++){
            string str;
            int a;
            cin >> str >> a;
            items[str]= a;
        }
        int renkinn;
        cin >> renkinn;
        for (int i = 0; i < renkinn; i++){
            string item;
            int sum = 0, a;
            cin >> item >> a;
            for (int j = 0; j < a; j++){
                string b;
                cin >> b;
                sum += items[b];
            }
            if (sum < items[item])items[item] = sum;
        }
        string ans;
        cin >> ans;
        cout << items[ans] << endl;
    }
    return 0;
}