#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;

map<string,int> normal;
map<string,vector<string> > rec;

int f(string x){
	int ans = normal[x] , sum = 0;
	if( rec[x].size() == 0 ) return ans;
	for(int i = 0 ; i < rec[x].size() ; i++){
		sum += f(rec[x][i]);
	}
	return min(ans,sum);
}

int main(){
	int n;
	while(cin >> n && n){
		normal.clear();
		rec.clear();
		for(int i = 0 ; i < n ; i++){
			string a;
			int b;
			cin >> a >> b;
			normal[a] = b;
		}
		int m;
		cin >> m;
		for(int i = 0 ; i < m ; i++){
			string x,y;
			cin >> x;
			int t;
			cin >> t;
			for(int j = 0 ; j < t ; j++){
				cin >> y;
				rec[x].push_back(y);
			}
		}
		string want;
		cin >> want;
		cout << f(want) << endl;
	}
}