#include<iostream>
#include<map>
#include<cstdio>
#include<string>
using namespace std;
int main(){
	int n,m,en,cnt,a,b,c;
	string str,str2,str3;
	map<string,int> data;
	
	cin >> n;
	for(int i=0;i<n;i++){
		cin >> str >> en;
		data[str]=en;
	}
	cin >> m;
	for(int i=0;i<m;i++){
		cnt = 0;
		cin >> str >> a;
		for(int j=0;j<a;j++){
			cin >> str2;
			cnt += data[str2];
		}
		if(data[str] > cnt){
			data[str] = cnt;
		}
	}
	cin >> str;
	cout << data[str] << endl;	
}