#include "bits/stdc++.h"

#define REP(i,num) for(int i=0;i<(num);++i)
#define LOOP(i) while(i--)
#define ALL(c) c.begin(),c.end()
#define PRINTALL(c) for(auto pitr=c.begin();pitr!=c.end();++pitr){cout<<*pitr;if(next(pitr,1)!=c.end())cout<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}

using namespace std;
using ll = long long;

constexpr ll atcoder_mod = 1e9+7;

template<typename T=int>
T in(){T x; cin >> x; return (x);}
template<typename T=int,typename C=vector<T>>
C vecin(int N){C x(N);REP(i,N){x[i]=in<T>();}return move(x);}

void vout(){cout << endl;}
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){cout << ' ' << h;vout(forward<Tail>(t)...);}
void out(){cout << endl;}
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){cout << h;vout(forward<Tail>(t)...);}

class Data{
public:
	string name;
	int value,new_value{0};
};

class ConnectNodeInfo{
	vector<vector<pair<ll,ll>>> graph;
public:
	ConnectNodeInfo(int node_num){
		graph.resize(node_num);
	}
	void AddNonDirectionalConnection(ll u,ll v,ll w){
		graph[u].emplace_back(v,w);
		graph[v].emplace_back(u,w);
	}
	void AddDirectionalConnection(ll u,ll v,ll w){
		graph[u].emplace_back(v,w);
	}
	vector<pair<ll,ll>>& operator[](ll index){
		return graph[index];
	}
	size_t size(){return graph.size();}
};

int DFS(ConnectNodeInfo& connect,int parent,int child,vector<Data>& D){
	int sum=0;
	if(connect[child].empty()) return D[child].value;

	for(auto& x:connect[child]){
		sum += DFS(connect,child,x.first,D);
	}
	D[child].value = min(D[child].value,sum);
	return D[child].value;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);

	while(true){
		int N=in();
		if(!N) break;
		vector<Data> D(N);
		map<string,int> E;
		REP(i,N){
			D[i].name=in<string>();
			D[i].value=in();
			E.emplace(D[i].name,i);
		}
		int M=in();
		ConnectNodeInfo connect(N),connect_inv(N);
		REP(i,M){
			string S=in<string>();
			int K=in();
			REP(j,K){
				string T=in<string>();
				connect.AddDirectionalConnection(E[S],E[T],1);
				connect_inv.AddDirectionalConnection(E[T],E[S],1);
			}
		}
		vector<int> L;
		REP(i,N){
			if(connect_inv[i].empty()) L.push_back(i);
		}
		for(auto& x:L){
			DFS(connect,-1,x,D);
		}
		out(D[E[in<string>()]].value);
	}

	return 0;
}

