#include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;
struct aa {
	int id;
	int time;
};
class Compare {
public:
	//aa?????????????????¶
	bool operator()(const aa&l, const aa&r) {
		return l.time> r.time;
	}
};
int main() {
	while (1) {

		int N; cin >> N;
		if (!N)break;
		map<string, int>mp;
		vector<int>costs;
		for (int i = 0; i < N; ++i) {
			string st; cin >> st;
			int cost; cin >> cost;
			mp[st] =i;
			costs.push_back(cost);
		}
		int M; cin >> M;
		vector<vector<pair<int,vector<int>>>>recipes(N);
		for (int i = 0; i < M; ++i) {
			string make; cin >> make;
			int makeid = mp[make];
			int a; cin >> a;
			vector<int>ids;
			for (int j = 0; j < a; ++j) {
				string name; cin >> name;
				int nameid = mp[name];
				ids.emplace_back(nameid);
			}
			for (auto id : ids) {
				recipes[id].emplace_back(makeid,ids);
			}
		}
		priority_queue<aa, vector<aa>, Compare>que;
		for (int i = 0; i < N; ++i) {
			que.push(aa{ i,costs[i] });
		}
		while (!que.empty()) {
			aa atop(que.top());
			que.pop();
			if (atop.time != costs[atop.id])continue;
			for (auto re : recipes[atop.id]) {
				int sum = 0;
				for (auto i : re.second) {
					sum += costs[i];
				}
				if (sum < costs[re.first]) {
					costs[re.first]=sum;
					que.push(aa{ re.first,sum });
				}
			}
		}
		string ans; cin >> ans;
		cout << costs[mp[ans]] << endl;
		
	}
	
	return 0;
}