#include <iostream>
#include <vector>
#include <map>
//#include <fstream>	// require freopen

using namespace std;

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

	while (cin >> n && n ){
		map <string, int > item;
		for (int i = 0; i < n; ++i ){
			string name; int money;
			cin >> name >> money;
			item[name] = money;
		} // end for
		cin >> m;
		map <string, vector <string> > recipe;
		for (int i = 0; i < m; ++i ){
			string p; int k; vector <string> t;
			cin >> p >> k;
			for (int j = 0; j < k; ++j){
				string q;
				cin >> q; t.push_back(q);
			} /// end for
			recipe[p] = t;
		} // end for
		map <string, int > pre_item;

		do{
			pre_item = item;

			map<string, int>::iterator it = item.begin();
			for (; it != item.end(); ++it ){
				string r = (*it).first;
				if (!recipe[r].empty() ){
					vector <string>::iterator jt = recipe[r].begin();
					int res = 0;
					for (; jt != recipe[r].end(); ++jt ){
						map <string, int >::iterator kt = item.begin();
						bool found = false;
						for (; kt != item.end(); ++kt ){
							if ((*jt) == (*kt).first ){
								res += (*kt).second;
								found = true;
							} // end if
						} // end for
						if (!found ){
							break;
						} // end if
					} // end for
					if (jt == recipe[r].end() ){
						item[r] = res;
					} // end if
				} // end if
			} // end for
		}while (pre_item != item );

		string search;
		cin >> search;
		cout << item[search] << endl;

	} // end loop
		
	return 0;
}