#include <stdlib.h>
#include <stdio.h>

typedef struct {
        int k, p; /* k personal code, p spouse's code if p > 0 */
        char s; /* sex */
        } T_pinfo;
static T_pinfo Pers[250000];
static int np, Er[6], Ok[2];

static int comp(const void *A, const void *B) {
T_pinfo *P = (T_pinfo *)A, *Q = (T_pinfo *)B;
return P->k - Q->k;
}

static void readList(void) {
T_pinfo *Pp;
int i;
scanf("%d",&np);
for (i=0,Pp=Pers; i<np; i++,Pp++)
    scanf("%d %c",&Pp->k,&Pp->s);
qsort(Pers,np,sizeof(T_pinfo),comp);
}

static int marriage(T_pinfo *P1, T_pinfo *P2) {
if (P1->s!='M') return 1;
if (P2->s!='F') return 2;
if (P1->p>0) return 3;
if (P2->p>0) return 4;
P1->p = P2->k;
P2->p = P1->k;
return 0;
}

static int divorce(T_pinfo *P1, T_pinfo *P2) {
if (P1->k!=P2->p) return 5;
P1->p = P2->p = 0;
return 0;
}

static void manage() {
T_pinfo *P1, *P2;
char r;
int k1, k2;
while (scanf(" %c %d %d",&r,&k1,&k2)>0) {
      P1 = bsearch(&k1,Pers,np,sizeof(T_pinfo),comp);
      P2 = bsearch(&k2,Pers,np,sizeof(T_pinfo),comp);
      if (r=='M') {
         if (r=marriage(P1,P2)) Er[r]++;
            else Ok[0]++;
         }
      else {
         if (r=divorce(P1,P2)) Er[r]++;
            else Ok[1]++;
         }
      }
}

int main() {
int e;
readList();
manage();
printf("Valid M: %d\n",Ok[0]);
printf("Valid D: %d\n",Ok[1]);
for (e=1; e<6; e++) printf("Error code %d: %d\n",e,Er[e]);
return 0;
}
