#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <limits>
#include <cmath>
#include <cassert>
#include <map>

using namespace std;
using ll = long long;

const ll INF = 1<<30;
const ll MOD = (ll)1e9 + 7;
const ll MAX_N = (ll)1e5 + 5;
#define debug(x) cout << #x << ": " << x << endl

ll N, M;
map<string, ll> price;
map<string, vector<string>> G;

ll dfs(const string &target)
{
    if(G.count(target) == 0) return price[target];
    ll res = 0;
    for(auto &&x : G[target]) res += dfs(x);
    return min(res, price[target]);
}

signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(cin >> N, N)
    {
        price.clear();
        G.clear();
        for(ll i = 0; i < N; i++)
        {
            string s; ll p; cin >> s >> p;
            price[s] = p;
        }
        cin >> M;
        for(ll i = 0; i < M; i++)
        {
            string s; ll k; cin >> s >> k;
            for(ll j = 0; j < k; j++)
            {
                string q; cin >> q;
                G[s].push_back(q);
            }
        }
        string target; cin >> target;
        cout << dfs(target) << endl;
    }
    return 0;
}


