#include <iostream>
#include <string.h>

using namespace std;

/* SANITY CHECK: assertion macro for verifying input data and internal state */
#define ASSERT(e) { if(!(e)) { cerr << #e << endl; throw; } }

/* List of ASCII characters representing chess pieces for side 0 and side 1 */
const string PIECE[2] = { "kqbrn", "KQBRN" };

/* Enumerated type indexing either side's PIECE[0] or PIECE[1] string */
enum { KING, QUEEN, BISHOP, ROOK, KNIGHT };

/* Structure encoding possible movement types for each piece */
typedef struct {
    bool free;     /* True for rook/bishop/queen which move multiple spaces */
    int count;     /* Number of valid entries in the following dir[] array */
    int dir[8][2]; /* Array of (row, col) allowed movement directions */
} movement_t;

/*
 * Movement definitions for each piece. These are in the same order as the
 * PIECE strings, so they can be indexed by the enumerated type. By using a
 * data structure like this, the movement definitions can be shared by both
 * the ischecked() and ismate() functions.
 */
const movement_t MOVEMENT[] = {
  /* KING */
  { false, 8, { {-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1} } },
  
  /* QUEEN */
  { true, 8,  { {-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1} } },

  /* BISHOP */
  { true, 4,  { {-1,-1},{-1,1},{1,-1},{1,1} } },

  /* ROOK */
  { true, 4,  { {-1,0},{0,-1},{0,1},{1,0} } },

  /* KNIGHT */
  { false, 8, { {-2,-1},{-2,1},{-1,-2},{-1,2},{2,-1},{2,1},{1,-2},{1,2} } }
};

/* Array holding the chess board layout for the current data set */
char board[8][8];

/* Either 0 or 1 depending on which side gets analyzed for current data set */
int side;

/* Row and column containing the king piece to be analyzed in this data set */
int kingrow = -1, kingcol = -1;

/* Return true if coordinate pair (row, col) is within chess board area */
inline bool isinside(int row, int col)
{
    return row >= 0 && row < 8 && col >= 0 && col < 8;
}

#ifdef STEP
/* DEBUG ONLY: Print chess board to stderr along with row and column headings */
void dump(void)
{
  cerr << " 01234567" << endl;
  
  for(int row = 0; row < 8; row++) {  
      cerr << row;
      
      for(int col = 0; col < 8; col++) {
          cerr << board[row][col];
      }
      
      cerr << endl;
  }
}
#endif

/*
 * Detect if this side's current king is in check by searching all the possible
 * locations around the king for pieces that could possibly threaten the king.
 * Starting with the king's current position (kingrow, kingcol) seach all the
 * positions around him from which an enemy piece could threaten based on the
 * movement directions defined in the MOVEMENT array. If an enemy piece is
 * found which can threaten the king in the specified movement direction, then
 * return true since the king is checked. If any other piece (from either side)
 * is found along the specified movement direction found, then return false
 * since this other piece will block any enemy pieces on the other side from
 * possibly threatening the king. Likewise return false if no pieces are found
 * along any of the directions that the king could be threatened from.
 */
bool ischecked(void)
{
    bool checked = false;

#ifdef STEP
    dump();
#endif

    /* Check for each enemy piece that could threaten the king */
    for(int piece = 0; piece < 5; piece++) {
        const char ascii = PIECE[!side][piece];
        const movement_t &move = MOVEMENT[piece];

        /* Search in each direction from which this piece could threaten */
        for(int dir = 0; dir < move.count; dir++) {
            int row = kingrow, col = kingcol;

            do {
                /* Advance search location by current direction */
                row += move.dir[dir][0];
                col += move.dir[dir][1];

                /* Abort this direction if location is past board edge */
                if(!isinside(row, col)) {
                    break;
                }

                /* Detect if the threatening piece was found */
                if(board[row][col] == ascii) {
#ifdef STEP
                    cerr << ascii << " @ " << row << "," << col << endl;
#endif
                    checked = true;
                    break;
                }

                /* Other pieces block further threats from this direction */
                if(board[row][col] != '.') {
                    break;
                }

            /*
             * A rook, bishop, or queen can move multiple spaces in the same
             * direction so keep searching until either the edge of the board
             * is reached, the threatening piece was found, or some other piece
             * was found that blocks any further threats from that direction.
             */
            } while(move.free);
        }
    }

    return checked;
}

bool ismate(void)
{
    bool mate = true;

    /* Search the board for a piece to try moving */
    for(int row = 0; row < 8; row++) {
        for(int col = 0; col < 8; col++) {
            int piece = PIECE[side].find(board[row][col]);
            
            /* If our side's piece has been found try moving it around */
            if(piece != -1) {
                const char ascii = PIECE[side][piece];
                const movement_t &move = MOVEMENT[piece];

                /* Try moving in all directions allowed by this piece */
                for(int dir = 0; dir < move.count; dir++) {
                    int newrow = row, newcol = col;
                    char oldpiece;

                    do {
                        /* Advance search location by current direction */
                        newrow += move.dir[dir][0];
                        newcol += move.dir[dir][1];

                        /* Abort this direction if location past board edge */
                        if(!isinside(newrow, newcol)) {
                            break;
                        }

                        /* Other friendly pieces block further movement */
                        if(PIECE[side].find(board[newrow][newcol]) != -1) {
                            break;
                        }
                        
                        /* Enemy kings block movement; they can't be captured */
                        if(board[newrow][newcol] == PIECE[!side][KING]) {
                            break;
                        }

                        /*
                         * Move piece to new location; if an enemy piece already
                         * occupies the new location, it will be captured (i.e.
                         * removed from the board) so a temporary copy of the
                         * captured enemy piece is saved in "oldpiece".
                         */
                        oldpiece = board[newrow][newcol];
                        board[newrow][newcol] = ascii;
                        board[row][col] = '.';
                        
                        /* If the king was moved, update his coordinates */
                        if(piece == KING) {
                            kingrow = newrow;
                            kingcol = newcol;
                        }
#ifdef STEP
                        cerr << endl << ascii << " @ " << row << "," << col;
                        cerr << " --> " << newrow << "," << newcol << endl;
#endif
                        /* Determine if the king is in check after this move */
                        mate &= ischecked();

                        /*
                         * Restore the piece to its previous location and
                         * restore any enemy piece that was captured by this
                         * piece.
                         */
                        board[newrow][newcol] = oldpiece;
                        board[row][col] = ascii;

                        /* If the king was moved, restore his coordinates */
                        if(piece == KING) {
                            kingrow = row;
                            kingcol = col;
                        }

                    /*
                     * A rook, bishop, or queen can move multiple spaces in the
                     * same direction until either the edge of the board is
                     * reached or an enemy piece was captured (i.e. oldpiece is
                     * not an empty '.' square).
                     */
                    } while(move.free && oldpiece == '.');
                }
            }
        }
    }
    
    return mate;
}

/* Main body of program */
void process(void)
{
    int data_num, data_idx;
    
    /* Read how many data sets to process */
    cin >> data_num;
    
    /* Process each data set separately */
    for(data_idx = 0; data_idx < data_num; data_idx++) {
        bool check = false, mate = false;
            
        /* Read in which side (white or black) to analyze */
        char c;
        cin >> c;
        ASSERT(c == 'w' || c == 'B');
        side = (c == 'w') ? 0 : 1;

        /* Reset the king's cooridante position for each data set */
        kingrow = -1;
        kingcol = -1;
        
        /* Read in the chess board layout */
        for(int row = 0; row < 8; row++) {
            for(int col = 0; col < 8; col++) {
                char &c = board[row][col];            
                cin >> c;
                
                /* Check for location of the king for the side we're analyzing */
                if(c == PIECE[side][KING]) {
                
                    /* SANITY CHECK: Make sure only one king was found */
                    ASSERT(kingrow == -1 && kingcol == -1);
                    
                    kingrow = row;
                    kingcol = col;
                }
            }
        }
        
        /* SANITY CHECK: Make sure that a king was found */
        ASSERT(kingrow != -1 && kingcol != -1);

        /* Detect a check and checkmate condition */
        check = ischecked();
        mate = ismate();

        /* Print the result */
        cout << (side ? "BLACK" : "WHITE") << " IS ";
        cout << (mate ? "CHECKMATED" : (check ? "CHECKED" : "SAFE")) << endl;
#ifdef STEP
        cerr << (side ? "BLACK" : "WHITE") << " IS ";
        cerr << (mate ? "CHECKMATED" : (check ? "CHECKED" : "SAFE")) << endl;
#endif
    }
}

/* Run program and print out any exceptions that occur */
int main(void)
{
    /* Throw exceptions on failed data extraction in >> operator */
    cin.exceptions(ios::failbit);
    
    /* Run main body of code */
    try {
        process();
    }
    
    /* Catch unexpected EOF or bad input data */
    catch(ios::failure const &e) {
        cerr << "Unexpected EOF or data type mismatch on input" << endl;
    }

    return 0;
}