#include <iostream>
#include <set>
#include <vector>
#include <map>
#include <string>
#include <utility>
#include <algorithm>
using namespace std;
typedef pair<int,int> P;
int n,m,k;string s;
map<string,int> cost;
map<string,vector<string>> recipe;
int dfs(string t){
	int ans=0;
	vector<string> vs=recipe[t];
	if(!vs.empty()){
		for(auto it=vs.begin();it<vs.end();it++){ans+=dfs(vs.at(it-vs.begin()));}
		cost[t]=min(cost[t],ans);
		return cost[t];
	}else{
		return cost[t];
	}
	
}
int main()
{
	while(cin>>n&&n){
		cost.clear();recipe.clear();
		for(int i=0;i<n;i++){
			int tmp;
			cin>>s>>tmp;
			cost[s]=tmp;
		}
		cin>>m;
		for(int i=0;i<m;i++){
			vector<string> vs;vs.clear();
			string t;cin>>s>>k;
			while(k--){cin>>t;vs.push_back(t);}
			recipe[s]=vs;
		}
		cin>>s;
		cout<<dfs(s)<<endl;
	}
    return 0;
}