#include <bits/stdc++.h>

const int MOD = 1e9 + 7;
const int iINF = 1000000000;
const long long int llINF = 1000000000000000000;
const double PI = acos(-1.0);
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
using ll = long long int;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using edge = struct
{
    int to;
    int cost;
};

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main()
{
    int N;
    while(cin >> N && N)
    {
        map<string, ll> mp;
        rep(i, N)
        {
            string s;
            int p;
            cin >> s >> p;

            mp[s] = p;

        }

        int M;
        cin >> M;

        rep(i, M)
        {
            int num;
            ll sum = 0;
            string maked;
            cin >> maked >> num;

            rep(i, num)
            {
                string mat;
                cin >> mat;
                sum += mp[mat];
            }

            mp[maked] = min(sum, mp[maked]);
        }

        string t;
        cin >> t;

        cout << mp[t] << endl;

    }
    
    return 0;
}
