#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <queue>
#include <complex>

#define INF 100000000
#define EPS 1e-9

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

#define MAX_N 100
int N;
map<string, int> M;
map<string, int> id;

string contents[MAX_N];

vector<int> vec[MAX_N];

vector<int> ans_list;

int solve(int number){
	
	int ans = 0;
	for (int i = 0; i < vec[number].size(); i++){
		ans += solve(vec[number][i]);
	}

	if (ans == 0)
		return M[contents[number]];
	else
		return min(ans, M[contents[number]]);

}

int main(){

	while (true){
	
		cin >> N;
		if (N == 0)
			break;

		map<string, int> m;
		for (int i = 0; i < N; i++){
			vec[i].clear();
			string str;
			int cost;
			cin >> str >> cost;
			m[str] = cost;
			id[str] = i;
			contents[i] = str;
		}

		M = m;

		int num;
		cin >> num;
		for (int i = 0; i < num; i++){
			string s;
			int k;
			cin >> s >> k;
			int id_number = id[s];
			for (int j = 0; j < k; j++){
				string str1;
				cin >> str1;
				int number = id[str1];
				vec[id_number].push_back(number);
			}
		}

		string ans;
		cin >> ans;

		ans_list.push_back(solve(id[ans]));
	
	}

	for (int i = 0; i < ans_list.size(); i++){
		cout << ans_list[i] << endl;
	}

	return 0;

}