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

int main()
{
  int n, x, y, x1, y1, dx, dy, res;
  char s[9];

  scanf("%d", &n);
  while (n--)
  {
    res = 0;
    scanf("%s", s);
    x = s[0] - 'a';
    y = s[1] - '1';
    for (dx = -2; dx <= 2; dx++)
      for (dy = -2; dy <= 2; dy++)
      {
        x1 = x + dx;
        y1 = y + dy;
        if (abs(dx) + abs(dy) == 3 && 0 <= x1 && x1 < 8 && 0 <= y1 && y1 < 8)
          res++;
      }
    printf("%d\n", res);
  }
  return 0;
}