#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count etc.
#include <cstdlib>	// require abs
#include <cstdio>	// require printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>	// require tolower, toupper
#include <fstream>	// require freopen

using namespace std;

map <string, int> minC;

void dfs (string qns, map <string, vector <string> > rela )
{

	int prev = minC[qns];

	int sum = 0;
	map <string, vector <string> >::iterator it = rela.begin();
	for (; it != rela.end(); ++it ){
		if ((*it).first == qns ){
			vector <string> r = (*it).second;
			vector <string>::iterator jt = r.begin();
			for (; jt != r.end(); ++jt ){
				sum += minC[(*jt)];
			} // end for
			if (sum < minC[qns] ){
				minC[qns] = sum;
			} // end if
			break;
		} // end if
	} // end for

	if (prev != minC[qns] ){
		vector <string>::iterator it = rela[qns].begin();
		for (; it != rela[qns].end(); ++it ){
			dfs (*it, rela );
		} // end for
		dfs (qns, rela );
	}else{
		return;
	}
}

int main()
{
//	cut here before submit 
//	freopen ("testcase.wgm", "r", stdin );
	int n, m;

	while (cin >> n && n ){
		minC.clear();
		for (int i = 0; i < n; ++i ){
			string ite = "";
			int cost;
			cin >> ite >> cost;
			minC[ite] = cost;
		} // end for
		cin >> m;
		map <string, vector <string> >	rela;
		for (int i = 0; i < m; ++i ){
			string s = "";
			cin >> s;
			int num;
			cin >> num;
			vector <string> t (num );
			for (int j = 0; j < num; ++j ){
				cin >> t[j];
			} // end for
			rela[s] = t;
		} // end for
		string qns;
		cin >> qns;
		
		dfs (qns, rela );

		cout << minC[qns] << endl;
	} // end loop
		
	return 0;
}