    #include<iostream>  
    #include<map>  
    #include<vector>  
    using namespace std;  
      
    int main(){  
      
      int n, p, m, q;  
      string s, o, u;  
      map < string ,int > mp;  
      vector < string > vec;  
      
      while(cin >> n, n){  
      
        map < string ,int > mp;  
        vector < string > vec;  
          
        for(int i=0;i<n;i++){  
          cin >> s >> p;  
          mp[s] = p;  
          vec.push_back(s);  
        }  
      
        cin >> m;  
        for(int i=0;i<m;i++){  
          cin >> s >> p;  
      
          int k = 0;  
          for(int i=0;i<p;i++){  
        cin >> o;  
        k += mp[o];  
          }  
      
          mp[s] = min(mp[s], k);  
        }  
        cin >> s;  
        cout << mp[s] << endl;  
      }  
      
    }  