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

#define rep(i,x,y) for(int i=(x);i<(y);++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int,int> pii;
//template<typename T> using vec=std::vector<T>;

const int inf=1<<30;
const long long int infll=1LL<<58;
const double EPS=1e-9;
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
	os << "[";
	for (const auto &v : vec) {
		os << v << ",";
	}
	os << "]";
	return os;
}

map<string,int> table;
map<string,vector<string>> recipe;
map<string,int> opt;

int dfs(string& name,map<string,int>& state){
    state[name]=1;
    int& res=opt[name];
    if(recipe[name].empty()) return res;

    int tmp=0;
    for(auto &next:recipe[name]){
        if(state[next]==1) tmp+=table[next];
        else if(state[next]==2) tmp+=opt[next];
        else tmp+=dfs(next,state);
    }
    res=min(res,tmp);

    state[name]=2;
    return res;
}

void solve(){
    int n;
    while(true){
        cin >> n;
        if(!n) break;

        table.clear();
        recipe.clear();
        opt.clear();

        rep(i,0,n){
            string name;
            int cost;
            cin >> name >> cost;
            table[name]=cost;
            opt[name]=inf;
        }

        int m;
        cin >> m;
        rep(i,0,m){
            string name1;
            int k;
            cin >> name1 >> k;
            rep(j,0,k){
                string name2;
                cin >> name2;
                recipe[name1].push_back(name2);
            }
        }

        string str;
        cin >> str;
        map<string,int> state;
        cout << dfs(str,state) << endl;
    }
}

int main(){
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	solve();
	return 0;
}