#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(a) (a).begin(),(a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const int INF=1e9;
int main(){
      int n;
      while(cin>>n&&n){
            string s,t[100];
            int p,m;
            bool update=true;
            map<string,int>M;
            string o[100];vector<string> pp[100];
            REP(i,n){
                  cin>>t[i]>>p;
                  M[t[i]]=p;
            }
            cin>>m;
            REP(i,m){
                  cin>>o[i]>>p;
                  REP(j,p){
                        cin>>s;pp[i].PB(s);
                  }
            }
            while(update){
                  update=false;
                  REP(i,m){
                        int cost=0;
                        REP(j,pp[i].size()){
                              cost+=M[pp[i][j]];
                        }
                        if(M[o[i]]>cost){
                              M[o[i]]=cost;
                              update=true;
                        }
                  }
            }
            cin>>s;
            cout<<M[s]<<endl;
      }
}