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

#define INF (1 << 8)

using namespace std;

map<string, int> table;
map<string, vector<string> > recipe;

int rec(string item)
{
    if(recipe[item].size() == 0) {
        return table[item];
    } else {
        int ret = 0;
        for(int i = 0; i < recipe[item].size(); i++) ret += rec(recipe[item][i]);
        return min(ret, table[item]);
    }
}

int main()
{
    int N, R; 
    while(cin >> N && N) {
        table.clear();
        for(int i = 0; i < N; i++) {
            int v; string name;
            cin >> name >> v;
            table.insert( map<string, int>::value_type(name, v));
        }
        
        cin >> R;
        recipe.clear();
        for(int i = 0; i < R; i++) {
            int n; string name, tmp; vector<string> child;
            cin >> name >> n;
            for(int j = 0; j < n; j++) {
                cin >> tmp;
                child.push_back(tmp);
            }
            recipe.insert( map<string, vector<string> >::value_type(name, child));
        }

        string query;
        cin >> query;
        cout << rec(query) << endl;
    }
}