#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<map>
#define INF 100000000
using namespace std;
int main(){
	int n;
	while(cin >> n,n){
		map<string,int> list;
		for(int i = 0; i < n; i++){
            string d;int a;
            cin >> d >> a;
            list[d] = a;
		}
		int m;
		cin >> m;
		for(int i = 0; i < m; i++){
            string d;int a;
            cin >> d >> a;
            int m = 0;
            for(int j = 0; j < a; j++){
                string dm;
                cin >> dm;
                m += list[dm];
            }
            list[d] = min(list[d],m);
		}
		string ans;
		cin >> ans;
		cout << list[ans] << endl;
	}
	return 0;
}