#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

int main()
{
    for(;;){
        int n;
        cin >> n;
        if(n == 0)
            return 0;

        map<string, int> item;
        for(int i=0; i<n; ++i){
            string name;
            int cost;
            cin >> name >> cost;
            item[name] = cost;
        }

        int m;
        cin >> m;
        vector<vector<string> > recipe(m);
        for(int i=0; i<m; ++i){
            string s;
            int k;
            cin >> s >> k;
            recipe[i].resize(k+1);
            recipe[i][0] = s;
            for(int j=1; j<=k; ++j)
                cin >> recipe[i][j];
        }

        for(int i=0; i<m; ++i){
            for(int j=0; j<m; ++j){
                int cost = 0;
                for(unsigned k=1; k<recipe[j].size(); ++k)
                    cost += item[recipe[j][k]];
                item[recipe[j][0]] = min(item[recipe[j][0]], cost);
            }
        }

        string s;
        cin >> s;
        cout << item[s] << endl;
    }
}