#include<stdio.h>
#include<list>
#include<string.h>

class item{
 public:
  int pr;
  char name[101];
  std::list<int> ren;
};
item its[100];
int solve(int n){
  int sum;
  std::list<int>::iterator it;
  if(its[n].ren.empty()){
    return its[n].pr;
  }
  for(sum=0,it=its[n].ren.begin();it!=its[n].ren.end();it++){
    sum+=solve(*it);
  }
  return sum<its[n].pr?sum:its[n].pr;
}
int main(){
  int i,j,n,m,k,l,o;
  char buf[400];
  using namespace std;
  list<int>::iterator it;
  do{
    scanf("%d",&n);
    if(!n) break;
    for(i=0;i<n;i++) scanf("%s %d",its[i].name,&(its[i].pr));
    scanf("%d",&m);
    for(i=0;i<m;i++){
      scanf("%s ",buf);
      for(j=0;j<n;j++) if(!strcmp(buf,its[j].name)) break;
      scanf("%d ",&k);
      for(l=0;l<k-1;l++){
	scanf("%s ",buf);
	for(o=0;o<n;o++) if(!strcmp(its[o].name,buf)) break;
	its[j].ren.push_back(o);
      }
      scanf("%s",buf);
      for(o=0;o<n;o++) if(!strcmp(its[o].name,buf)) break;
      its[j].ren.push_back(o);
    }
    scanf("%s",buf);
    for(i=0;i<n;i++) if(!strcmp(buf,its[i].name)) break;
    printf("%d\n",solve(i));
  }while(1);
  return 0;
}