#include<bits/stdc++.h>
using namespace std;
const int INF = 1 << 28;

int main(){
  int n, m;
  map< string, int > peace;
  
  
  while(cin >> n, n){
    for(int i = 0; i < n; i++){
      string s;
      int p;
      cin >> s >> p;
      peace.insert( make_pair( s, p));
    }
    cin >> m;
    for(int i = 0; i < m; i++){
      string a;
      int c, cost = 0;
      cin >> a >> c;
      while(c--){
        string b;
        cin >> b;
        cost += peace[b];
      }
      peace[a] = min( peace[a], cost);
    }
    string query;
    cin >> query;
    cout << peace[query] << endl;
    peace.clear();
  }
}