#include<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int f(int a[],int b[],int m,int n,string s[],vector<string> t[],string u){
  int i,j;
  int c[100],g=-1;
  int sum;
  for(i=0;i<m;i++){
    if(b[i]&&t[i][0]==u)
      return -1;
  }
  for(i=0;i<n;i++){
    if(s[i]==u)
      break;
  }
  if(i!=n)
    g=a[i];
  for(i=0;i<m;i++){
    if(t[i][0]==u){
      memcpy(c,b,sizeof(c));
      c[i]=1;
      sum=0;
      for(j=1;j<(int)t[i].size();j++){
	int tmp;
	if((tmp=f(a,c,m,n,s,t,t[i][j]))==-1)
	  return g;
	else
	  sum+=tmp;
      }
      if(g==-1||g>sum)
	g=sum;
    }
  }
  return g;
}
int main(){
  int i,j,k;
  int a[100],b[100],m,n;
  string s[100],u;
  vector<string> t[100];
  while(cin>>n&&n){
    for(i=0;i<n;i++)
      cin>>s[i]>>a[i];
    cin>>m;
    for(i=0;i<m;i++){
      cin>>u>>k;
      t[i].clear();
      t[i].push_back(u);
      for(j=0;j<k;j++){
	cin>>u;
	t[i].push_back(u);
      }
    }
    cin>>u;
    memset(b,0,sizeof(b));
    cout<<f(a,b,m,n,s,t,u)<<endl;
  }
  return 0;
}