#include<iostream>
#include<vector>
#include<map>
#include<algorithm>

class UnionFind{
public:
	int size;
	std::vector<int> par, rank;
	UnionFind();
	UnionFind(int _size);
	int find(int x);
	bool same(int x, int y);
	void unite(int x, int y);
};

UnionFind::UnionFind()
	:size(0), par(0), rank(0){}

UnionFind::UnionFind(int _size)
	:size(_size), par(_size), rank(_size){
	for(int i=0;i<size;i++){
		par[i] = i;
		rank[i] = 0;
	}
}

int UnionFind::find(int x){
	if(x == par[x]){
		return x;
	}
	return par[x] = find(par[x]);
}

bool UnionFind::same(int x, int y){
	return find(x) == find(y);
}

void UnionFind::unite(int x, int y){
	x = find(x);
	y = find(y);

	if(x == y)return;

	if(rank[x] < rank[y]){
		par[x] = par[y];
	}else{
		par[y] = par[x];
		if(rank[x] == rank[y]){
			rank[x]++;
		}
	}
}

struct Recipe{
	int index, stuff[100], size;
};

UnionFind uf;

bool comp(const Recipe& l_recipe, const Recipe& r_recipe){
	if(uf.same(l_recipe.index, r_recipe.index)){
		return uf.rank[l_recipe.index] > uf.rank[r_recipe.index];
	}
	return l_recipe.index < r_recipe.index;
}

int main(){
	int n;
	while(std::cin >> n, n){
		std::map<std::string, int> indexes;
		int items[100];//items[index]: index番目の値段
		for(int i=0;i<n;i++){
			std::string name;
			int price;
			std::cin >> name >> price;
			indexes[name] = i;
			items[i] = price;
		}
		Recipe r[100];//recipe[n][0]には材料の数
		uf = UnionFind(100);

		int m;
		std::cin >> m;
		for(int i=0;i<m;i++){
			std::string name;
			int stuff_n;
			std::cin >> name >> stuff_n;

			r[i].size = stuff_n;
			r[i].index = indexes[name];

			for(int j=0;j<stuff_n;j++){
				std::string stuff;
				std::cin >> stuff;
				r[i].stuff[j] = indexes[stuff];
				uf.unite(indexes[name], indexes[stuff]);
			}
		}
	
		std::sort(r, r+m, comp);

		for(int i=0;i<m;i++){
			int price = 0;
			for(int j=0;j<r[i].size;j++){
				price += items[r[i].stuff[j]];
			}
			items[r[i].index] = std::min(items[r[i].index], price);
		}
	
		std::string want;
		std::cin >> want;
		std::cout << items[indexes[want]] << std::endl;
	}
}