#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
typedef long long ll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
int n;
int main()
{
	while(1)
	{
		cin >> n;
		if(n == 0) return 0;
		map<string,int>hoge;
		int dp[105];
		for(int i=0;i<n;i++)
		{
			string s; int v;
			cin >> s >> v;
			hoge[s] = i;
			dp[i] = v;
		}
		int m;
		cin >> m;
		vector<vector<int> >vec[105];
		for(int i=0;i<m;i++)
		{
			string st; cin >> st;
			int n;
			cin >> n;
			int t = hoge[st];
			vector<int>c;
			for(int j=0;j<n;j++)
			{
				string a;
				cin >> a;
				c.pb(hoge[a]);
			}
			vec[t].pb(c);
		}
		
		while(1)
		{
			bool upd = false;
			for(int i=0;i<n;i++)
			{
				for(int j=0;j<vec[i].size();j++)
				{
					int cnt = 0;
					for(int k=0;k<vec[i][j].size();k++) cnt += dp[vec[i][j][k]];
					if(cnt < dp[i])
					{
						dp[i] = cnt;
						upd = true;
					}
				}
			}
			if(!upd) break;
		}
		string t; cin >> t;
		cout << dp[hoge[t]] << endl;
	}
}