#include <bits/stdc++.h>

#define all(a)  (a).begin(),(a).end()
#define clr(a,b) memset((a), (b) ,sizeof(a))
#define exist(s,e) ((s).find(e)!=(s).end())
#define pb push_back
#define range(i,a,b) for(int (i)=(a);(i)<(b);++(i))
#define rep(i,n)  range(i,0,n)
#define dump(x)  cerr << #x << " = " << (x) << endl;

using namespace std;

typedef vector<int> vi;
typedef long long ll;

const double eps = 1e-10;
const double pi  = acos(-1.0);
const ll INF =1LL << 62;
const int inf =1 << 29;

const int vmax=110;
int value[vmax];
vector<int> graph[vmax];
string item[vmax];
map<string,int> convert;


int dfs(int v){
	if(graph[v].size()==0) return value[v];
	int sum=0;	
	for(auto &v2:graph[v]) sum+=dfs(v2);
	return min(value[v],sum);
}

int main(void){
	int n;
	while(cin >> n,n){
		// reset
		rep(i,vmax) graph[i].clear();
		convert.clear();

		rep(i,n) cin >> item[i] >> value[i];
		rep(i,n) convert[item[i]]=i;
		int m;
		cin >> m;
		rep(i,m){
			string parent;
			cin >> parent;
			int l;
			cin >> l;
			rep(j,l){
				string child;
				cin >> child;
				graph[convert[parent]].push_back(convert[child]);
			}
		}
		string target;
		cin >> target;
		cout << dfs(convert[target]) << endl;
	}
	return 0;
}