#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned int uint;
typedef long long ll;

struct recipe{
  int dist;
  vector<int> src;
};

int main(){
  int n;
  while(cin>>n, n){
    map<string,int> mp;
    vector<int> g(n);

    REP(i,n){
      string str;
      int p;
      cin >> str >> p;
      mp[str] = i;
      g[i] = p;
    }

    int m;
    cin >> m;
    vector<recipe> r(m);
    REP(i,m){
      string str;
      int k;
      cin >> str >> k;
      r[i].dist = mp[str];
      r[i].src = vector<int>(k);
      REP(j,k){
        cin >> str;
        r[i].src[j] = mp[str];
      }
    }

    bool flag = true;
    while(flag){
      flag = false;
      REP(i,m){
        int dist = r[i].dist;
        int sum = 0;
        REP(j,r[i].src.size()){
          sum += g[r[i].src[j]];
        }
        if(sum < g[dist]){
          flag = true;
          g[dist] = sum;
        }
      }
    }

    string str;
    cin >> str;
    cout << g[mp[str]] << endl;
  }
  return 0;
}