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

const int INF = (1 << 30) - 1;
int main()
{
    int N, M;

    while ( cin >> N, N ) {

        bool path[105][105] = {};
        int value[105];
        int memo[105];

        string purpose;
        unordered_map < string , int > hash;

        for ( int i = 0; i < N; i++ ) {
            string name;
            cin >> name >> value[i];
            hash[name] = i;
        }

        cin >> M;
        for ( int p = 0; p < M; p++ ) {
            string node;
            int k;
            cin >> node >> k;
            for ( int i = 0; i < k; i++ ) {
                string to;
                cin >> to;
                path[ hash[node] ][ hash[to] ] = true;
            }
        }

        cin >> purpose;
        memset(memo, -1, sizeof(memo));
        function < int(int) > calc = [&]( int v )
        {
            if ( memo[v] == -1 ) {
                memo[v] = INF;
                for ( int i = 0; i < N; i++ ) {
                    if ( path[v][i] ) {
                        if ( memo[v] == INF ) {
                            memo[v] = 0;
                        }
                        memo[v] += calc(i);
                    }
                }
                memo[v] = min(memo[v], value[v]);
            }
            return ( memo[v] );
        };

        cout << calc(hash[purpose]) << endl;
    }

    return (0);
}

