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

int main(){
	int n,m,a,b;
	string s,ts;
	while(cin >> n,n){
		map<string,int> ma;
		for(int i = 0;i < n;++i){
			cin >> s >> a;
			ma[s] = a;
		}
		cin >> m;
		for(int i = 0;i < m;++i){
			b = 0;
			cin >> s >> a;
			for(int j = 0;j < a;++j){
				cin >> ts;
				b += ma[ts];
			}
			if(b < ma[s]) ma[s] = b;
		}
		cin >> s;
		cout << ma[s] << endl;
	}
	return 0;
}