#define _USE_MATH_DEFINES
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<climits>
#include<map>
#include<string>
using namespace std;

int N, M, P[100], K;
int memo[100];

int DFS(int idx, const vector<int> e[100]){
	if(memo[idx] != INT_MAX) return memo[idx];
	memo[idx] = P[idx];
	if( !e[idx].empty() ){
		int cost = 0;
		for(int i = 0; i < e[idx].size(); i++){
			cost += DFS(e[idx][i], e);
		}
		memo[idx] = min(memo[idx], cost);
	}
	return memo[idx];
}

int main(){
	while(cin >> N, N){
		vector<int> e[100];
		map<string, int> idx;
		string I, O, Q;
		fill_n(memo, 100, INT_MAX);
	
		for(int i = 0; i < N; i++){
			cin >> I >> P[i];
			idx[I] = i;
		}
		cin >> M;
		for(int i = 0; i < M; i++){
			cin >> O >> K;
			int O_idx = idx[O];
			for(int j = 0; j < K; j++){
				cin >> Q;
				e[O_idx].push_back(idx[Q]);
			}
		}
		cin >> I;
		printf("%d\n", DFS(idx[I], e) );
	}
	return 0;
}