#include <iostream>
#include <vector>
#include <map>
#include <string>

#define REP(i,n) for(int i = (int)(n) - 1; i >= 0; --i)

using namespace std;

int costs[101];
vector<int> recipe[101];
bool flags[101];

int getCost(int goal){
	if(recipe[goal].empty()) return costs[goal];
	flags[goal] = true;
	int sum = 0;
	REP(i, recipe[goal].size()){
		if(flags[recipe[goal][i]]){
			flags[goal] = false;
			return costs[goal];
		}
		sum += getCost(recipe[goal][i]);
	}
	flags[goal] = false;
	if(sum < costs[goal]){
		return sum;
	}else{
		return costs[goal];
	}
}

int main(){
	int n, m;
	while(true){
		cin >> n;
		if(n==0)break;
		
		memset(flags, 0, sizeof(flags));

		map<string,int> hash;
		
		REP(i,n){
			string name;
			cin >> name >> costs[i];
			hash.insert(map<string,int>::value_type(name,i));
			recipe[i].clear();
		}

		cin >> m;
		REP(i, m){
			string name;
			int item;
			int num;
			cin >> name >> num;
			item = hash[name];
			recipe[item].reserve(num);
			REP(j, num){
				string mate;
				int mat;
				cin >> mate;
				mat = hash[mate];
				recipe[item].push_back(mat);
			}
		}

		string goalName;
		int goal;
		cin >> goalName;
		goal = hash[goalName];

		cout << getCost(goal) << endl;
		
	}
	
	return 0;
}