// 基本テンプレート
 
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
using namespace std;
 
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
typedef pair<int, int> pii;
typedef long long ll;
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

int dp[110], price[110];
int solve(int id, vector< vector< vector<int> > >& recipe) {
    if(dp[id] != INF) return dp[id];

    dp[id] = price[id];
    for(auto vec : recipe[id]) {
        int sum = 0;
        for(auto e : vec) {
            sum += solve(e, recipe);
        }
        chmin(dp[id], sum);
    }
    return dp[id];
}
 
signed main() {
    int N, M;
    while(cin >> N, N) {
        int id = 0;
        map<string, int> stoid;

        for(int i=0; i<N; i++) {
            string s;
            cin >> s >> price[i];
            stoid[s] = id++;
        }

        cin >> M;
        fill(dp, dp + N, INF);
        vector< vector< vector<int> > > recipe(N);
        for(int i=0; i<M; i++) {
            string s; cin >> s;
            int idx = stoid[s];

            int K; cin >> K;
            vector<int> tmp_list;
            for(int j=0; j<K; j++) {
                string t; cin >> t;
                tmp_list.push_back(stoid[t]);
            }
            recipe[idx].push_back(tmp_list);
        }

        string x; cin >> x;
        cout << solve(stoid[x], recipe) << endl;
    }
    return 0;
}
