/*
TASK:FOLDERS
LANG:C++
*/

#include<iostream.h>
#include<stdlib.h>
#include<string.h>
 struct fold
 {
  char name[10];
  short num;
  int t;
  struct fold *next;
 }*A=NULL,*B=NULL,*C=NULL,*D=NULL;
 void read(short m, struct fold **L);
 struct fold *insa(struct fold **L, char *nm, short num1, int tm);
 struct fold *insb(struct fold **L, char *nm, short num1, int tm);
// void del();
 void solve();
 int count(struct fold **L);
 int main()
 {
  int t;
  short n;
  char nam[10];
  cin>>nam>>n>>t;
  C=insb(&A,nam,n,t);
  if(n>0)read(n,&C);
  cin>>nam>>n>>t;
  D=insb(&B,nam,n,t);
  if(n>0)read(n,&D);
  solve();
  return 0;
 }

 void read(short m,struct fold **L)
 {
  char n[10];
  short nm,i;
  int t;
  for(i=0;i<m;i++)
  {
   cin>>n>>nm>>t;
   (*L)=insa(L,n,nm,t);
   if(nm>0)read(nm,L);
   }

 }

 struct fold *insb(struct fold **L, char *nm, short num1, int tm)
 {
  struct fold *temp;
  temp=new struct fold;
  if(!temp){cout<<"Error1"; exit(1);}
  temp->next=(*L);
  (*L)=temp;
  strcpy(temp->name,nm);
  temp->num=num1;
  temp->t=tm;
  return temp;
 }

 struct fold *insa(struct fold **L, char *nm, short num1, int tm)
 {
  struct fold *temp;
  if((*L)==NULL){return insb(L,nm,num1,tm);}
  temp=new struct fold;
  if(!temp){cout<<"Error2"; exit(1);}
  temp->next=(*L)->next;
  (*L)->next=temp;
  strcpy(temp->name,nm);
  temp->num=num1;
  temp->t=tm;
  return temp;
 }

 void solve()
 {
  int br=0;
  char n[12];
  while(B!=NULL)
  {
   short a,r;
   a=strcmp(B->name,A->name);
   if(!a && A->t>B->t){
   r=strlen(A->name);
   strcpy(n,A->name);
   n[r]=NULL;
   br+=count(&A);
   while(B!=NULL && strstr(B->name,n))
    B=B->next;
    }
   else {
    if(a<0)B=B->next;
     else {
      if(a>0){br+=count(&A);}
       else {A=A->next; B=B->next;}
       }
      }
  }
  cout<<br<<endl;
  return;
 }

 int count(struct fold **L)
 {
  int br=0;
  char n[12];
  if(!(*L)->num){(*L)=(*L)->next; return 1;}
  short r=strlen((*L)->name);
  strcpy(n,(*L)->name);
  n[r]=NULL;
  (*L)=(*L)->next;
  while((*L)!=NULL && strstr((*L)->name,n))
   {if((*L)->num==0)br++;
    (*L)=(*L)->next; }
  return br;
 }