#include <bits/stdc++.h>
using namespace std;
int main() {
	int n,m;
	while(cin>>n&&n){
		map<string,int> data;
		string s;
		int a;
		for(int i=0;i<n;i++){
			cin>>s>>a;
			data[s]=a;
		}
		cin>>m;
		string wrought[100][100];
		int k,sum;
		for(int i=0;i<m;i++){
			cin>>wrought[i][0];
			sum=0;
			cin>>k;
			for(int j=0;j<k;j++){
				cin>>wrought[i][j+1];
				sum+=data[wrought[i][j+1]];
			}
			if(data[wrought[i][0]]>sum)data[wrought[i][0]]=sum;
		}
		for(int i=0;i<m;i++){
			int sum=0;
			for(int j=0;j<k;j++)sum+=data[wrought[i][j+1]];
			if(data[wrought[i][0]]>sum)data[wrought[i][0]]=sum;
		}
		string ans;
		cin>>ans;
		cout<<data[ans]<<endl;
	}
}