#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
 
using namespace std;
 
#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,s,e) for(int i=(s); i<(e); i++)
#define pb(n) push_back((n))
#define mp(n,m) make_pair((n),(m))
#define all(r) r.begin(),r.end()
#define fi first
#define se second
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef pair<int, pii> pip;
 
 
 
const int INF = 1000000;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
 



int main(){
	int n, m, a, sum;
	string s, t;
	while(cin>> n && n > 0){
		//cin>>m;
		m = n;
		map<string,int> mp;
		for(int i= 0; i < m; i++){
			cin>>s>>a;
			mp[s] = a;
		}
		//cout<<n<<endl;
		cin>>m;
		for(int i = 0; i < m; i++){
			cin>>s>>a;
			sum = 0;
			for(int j = 0; j < a; j++){
				cin>>t;
				sum += mp[t];
			}
			mp[s] = min(mp[s], sum);
		}
		cin>>s;
		cout<<mp[s]<<endl;
	}
}