#include <iostream>
#include <fstream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)

int n,m;
map<string,int> t;
map<string,vector<string> > t2;

int f(string s){
	if(t2.find(s)==t2.end())return t[s];
	int c=0;
	rep(i,t2[s].size())c+=f(t2[s][i]);
	return min(t[s],c);
}

int main(){
	while(cin>>n&&n){
		t.clear();t2.clear();
		int p;
		string s,g;
		rep(i,n){
			cin>>s>>p;
			t[s]=p;
		}
		cin>>m;
		rep(i,m){
			cin>>s>>p;
			vector<string> v;
			rep(j,p){
				string st;
				cin>>st;
				v.push_back(st);
			}
			t2[s]=v;
		}
		cin>>g;
		cout<<f(g)<<endl;
	}
}