#include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define rrep(i,n) for(int i=(int)(n)-1; i>=0; i--)
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin(); it!=(c).end(); it++)
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define pr(a) cout << (a) << endl
#define PR(a,b) cout << (a) << " " << (b) << endl
#define F first
#define S second
typedef long long ll;
typedef pair<int,int> P;
const int MAX=1000000001;
const ll MAXL=1000000000000000001LL;
const ll mod=1000000007;
int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};

int main() {
  int n;
  while(cin >> n && n) {
    map<string,int> m;
    rep(i,n) {
      string s;
      int x;
      cin >> s >> x;
      m[s]=x;
    }
    cin >> n;
    map<string, vector<string> > m2;
    rep(i,n) {
      string s;
      cin >> s;
      int k;
      cin >> k;
      vector<string> v(k);
      rep(j,k) cin >> v[j];
      m2[s]=v;
    }
    while(1) {
      bool update=false;
      tr(it,m2) {
	vector<string> v=it->S;
	int sum=0;
	rep(i,v.size()) sum+=m[v[i]];
	if(m[it->F]>sum) {
	  update=true;
	  m[it->F]=sum;
	}
      }
      if(!update) break;
    }
    string s;
    cin >> s;
    pr(m[s]);
  }
  return 0;
}
