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

int main(int argc, char const *argv[])
{
  int zO, zA, zB, zAB, n;
  scanf("%d", &n);
  int  pO[n], pA[n], pB[n], pAB[n];
  int  iO[n], iA[n], iB[n], iAB[n];
  char pac[n][2];
  scanf("%d %d %d %d", &zO, &zA, &zB, &zAB);
  int z = zO + zA + zB + zAB;
  int z1 = 0;
  if (!(n >= 0 && n <= 1000) || (z > 1000000))
  {
    goto kraj;
  }
  for (int i = 0; i < n; i++)
  {
    scanf("%s", pac[i]);
    if (strcmp(pac[i], "O") == 0)
    {
      scanf("%d\n", &pO[i]);
    }
    if (strcmp(pac[i], "A") == 0)
    {
      scanf("%d", &pA[i]);
    }
    if (strcmp(pac[i], "B") == 0)
    {
      scanf("%d", &pB[i]);
    }
    if (strcmp(pac[i], "AB") == 0)
    {
      scanf("%d", &pAB[i]);
    }
    z1= pO[i] + pA[i] + pB[i] + pAB[i];
  }
  if (z1 > 1000000)
  {
    goto kraj;
  }
  dalje:
  for (int i = 0; i < n; i++)
  {
    if (i == 0) {
      iO[i] = 0;
      iA[i] = 0;
      iB[i] = 0;
      iAB[i] = 0;
    }
    if (strcmp(pac[i], "O") == 0)
    {
      for (int j = 0; j < pO[i]; j++)
      {
        if (zO == 0)
        {
          goto dalje;
        }
        zO--;
        iO[i]++;


      }
    }
    if (strcmp(pac[i], "A") == 0)
    {
      for (int j = 0; j < pA[i]; j++)
      {
        if (zA == 0)
        {
          goto nulaA;
        }
        zA--;
        iA[i]++;

      }
      nulaA:
      for (int j = 0; j < pA[i]; j++)
      {
        if (zO == 0)
        {
          goto dalje;
        }
        zO--;
        iO[i]++;

      }
    }

  if (strcmp(pac[i], "B") == 0)
  {

    for (int j = 0; j < pB[i]; j++)
    {
      if (zB == 0)
      {
        goto nulaB;
      }
      zB--;
      iB[i]++;

    }
    nulaB:
    for (int j = 0; j < pB[i]; j++)
    {
      if (zO == 0)
      {
        goto dalje;
      }
      zO--;
      iO[i]++;

    }
  }

  if (strcmp(pac[i], "AB") == 0)
  {
    for (int j = 0; j < pAB[i]; j++)
    {
      if (zAB == 0)
      {
        goto nulaAB1;
      }
      zAB--;
      iAB[i]++;

    }
    nulaAB1:
    for (int j = 0; j < pAB[i]; j++)
    {
      if (zA == 0)
      {
        goto nulaAB2;
      }
      zA--;
      iA[i]++;

    }
    nulaAB2:
    for (int j = 0; j < pAB[i]; j++)
    {
      if (zB == 0)
      {
        goto nulaAB3;
      }
      zB--;
      iB[i]++;

    }
    nulaAB3:
    for (int j = 0; j < pAB[i]; j++)
    {
      if (zO == 0)
      {
        goto dalje;
      }
      zO--;
      iO[i]++;
      
    }
  }
}
  if (zO < 0 || zA < 0 || zB < 0 || zAB < 0) 
  {
    printf("nemoguce\n");
  }
  for (int i = 0; i < n; i++)
  {
    printf("%d %d %d %d\n", iO[i], iA[i], iB[i], iAB[i]);
  }
  kraj:
  return 0;
}