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

int min( long int a, long int b )
{
    if( a < b )
        return a;
    else
        return b;
}

int main()
{
    int n, m, k;
    string str, s[100];
    map<string,int> a;
    
    while( cin >> n )
    {
        if( n == 0 )
            break;

        for( int i = 0; i < n; i++ )
        {
            cin >> str >> m;
            a[str] = m;
        }

        cin >> m;

        for( int i = 0; i < m; i++ )
        {
            cin >> str >> k;
            long int sum = 0;
            for( int j = 0; j < k; j++ )
            {
                cin >> s[j];
                sum += a[s[j]];
            }
            a[str] = min( a[str], sum );
        }

        cin >> str;

        cout << a[str] << endl;
    }
    
    return 0;
}

