#include<iostream>
#include<vector>
#include<map>
#include<climits>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
map<string,int> item;
map<string,vector<string> > recipe;
typedef long long ll;

ll recurse(string name){
	long long ans = (1ll<<63);
	if(item.find(name) != item.end()){
		ans = item[name];
	}
	if(recipe.find(name) != recipe.end() ){
		ll t = 0;
		rep(i,recipe[name].size()){
			t += recurse(recipe[name][i]);
		}
		ans = min(t,ans);
	}
	return ans;
}

int main(){
	int n;
	while(cin >> n , n){
		item.clear(),recipe.clear();
		rep(i,n){
			string s;
			int p;
			cin >> s >> p;
			item[s] = p;
		}
		int m;
		cin >> m;
		rep(i,m){
			string get;
			int k;
			cin >> get >> k;
			rep(j,k){
				string need;
				cin >> need;
				recipe[get].push_back(need);
			}
		}
		string wantItem;
		cin >> wantItem;
		cout << recurse(wantItem) << endl;
	}
}