#include <iostream>
#include <string>
#include <map>
using namespace std;

int main()
{
	map<string,int> memo;
	int n;
	while(cin >> n){
		if(n==0)
			break;
		string str;
		int prise;
		for(int i=0;i<n;i++){
			cin >> str >> prise;
			memo[str] = prise;
		}
		int resipi;
		cin >> resipi;
		map<string,int> resi;
		int count;
		string o;
		for(int i=0;i<resipi;i++){
			cin >> str >> count;
			resi[str] = 0;
			for(int j=0;j<count;j++){
				cin >> o;
				resi[str] += memo[o];
			}
			if(resi[str] < memo[str])
				memo[str] = resi[str];
		}
		string final;
		cin >> final;
		cout << memo[final] << endl;
	}
	return 0;
}