#include <bits/stdc++.h>  
 
using namespace std;
 
#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define repr(i, j) for(int i = j; i >= 0; --i)
#define INF (1 << 30)
#define MOD 1e9 + 7
 
typedef long long ll;
typedef unsigned long long ull;
typedef pair<string, int> P;

map<string, int> goods;
map<string, int> mp;

int main() {
	int N;
	while(scanf("%d", &N), N) {
		rep(i, N) {
			string s;
			int price;
			cin >> s >> price;
			goods[s] = price; 
		}
		int M;
		scanf("%d", &M);
		rep(i, M) {
			string s;
			int num;
			cin >> s >> num;
			int sum = 0;
			rep(j, num) {
				string g;
				cin >> g;
				sum += goods[g];
			}
			if(sum < goods[s]) goods[s] = sum;
		}
		string ans;
		cin >> ans;
		cout << goods[ans] << endl;
	}
	return 0;
}