#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <map>


using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)



class Rec{
public:
	int prodI;
	vector<int> ingIS;
};


int main(){
	while(true){
		int itemL;
		cin >> itemL;					if(!itemL)break;
		map<string,int> itemX2I;
		map<int,string> itemI2X;
		vector<int> priceS;

		REP(itemI,itemL){
			string itemX;
			int itemPrice;
			cin >> itemX >> itemPrice;
			itemX2I[itemX] = itemI;
			itemI2X[itemI] = itemX;
			priceS.push_back(itemPrice);
		}

		int recL;
		cin >> recL;
		vector<Rec> recS;

		REP(recI,recL){
			Rec rec;
			string prodX;
			cin >> prodX;
			rec.prodI = itemX2I[prodX];
			int ingL;
			cin >> ingL;
			REP(gomi,ingL){
				string ingX;
				cin >> ingX;
				rec.ingIS.push_back(itemX2I[ingX]);
			}
			recS.push_back(rec);
		}

		REP(gomi,recL+1){
			REP(recI,recL){
				Rec rec = recS[recI];
				int prodI = rec.prodI;
				int nPrice = 0;
				REP(ingII,rec.ingIS.size()){
					int ingI = rec.ingIS[ingII];
					nPrice += priceS[ingI];
				}
				priceS[prodI] = min(priceS[prodI],nPrice);
			}
		}

		string prodX;
		cin >> prodX;
		int prodI = itemX2I[prodX];
		int price = priceS[prodI];
		cout << price << endl;


	}
}