#!/usr/bin/env python

# Chess
# Phil Bordelon

import os
import sys

DEBUG = os.getenv("DEBUG", False)

# Let's have some constants for the piece types.
KING = KINGS = ("K", "k")
QUEEN = QUEENS = ("Q", "q")
ROOK = ROOKS = ("R", "r")
BISHOP = BISHOPS = ("B", "b")
KNIGHT = KNIGHTS = ("N", "n")

# And constants for the sides.
BLACK = "B"
WHITE = "w"

# And constants for the results.
SAFE = "SAFE"
CHECKED = "CHECKED"
CHECKMATED = "CHECKMATED"

# Chessboards are 8x8, but juuust in caaaaase ...
N = 8

# Ah, classes as structs, how I love you.
class Struct(object):
   pass

####
# printBoard prints a chessboard; debugariffic.
def printBoard(board):

   for i in range(N):

      to_print = ""
      for j in range(N):

         if board[i][j].contents:
            to_print += board[i][j].contents.representation
         else:
            to_print += "."

      print(to_print)

####
# readBoard reads in a chessboard.
def readBoard(game):

   # Literal representations of boards are passe, but hey.  They look
   # neat.
   game.board = []

   # Initialize the lists of pieces.  Kings stored separately as well.
   game.pieces = {BLACK: [], WHITE: []}
   game.kings = {BLACK: None, WHITE: None}

   for i in range(N):
      board_line = sys.stdin.readline().strip()
      board_row = []
      for j in range(len(board_line)):

         # New location.
         this_loc = Struct()
         this_loc.x = i
         this_loc.y = j

         this_char = board_line[j]
         if this_char == '.':

            # Empty space.
            this_loc.contents = None

         else:

            # An actual chess piece!  Make a new object to track.
            piece = Struct()
            piece.representation = this_char

            # Note its location.
            piece.x = i
            piece.y = j

            # What side is it on?
            if this_char.upper() == this_char:

               # Capital letters are black.
               piece.side = BLACK
            else:
               piece.side = WHITE

            # Now, what type is it?
            if this_char in KINGS:

               # We track kings separately from the other pieces.
               piece.type = KING
               game.kings[piece.side] = piece

            else:

               # Others.
               if this_char in QUEENS:
                  piece.type = QUEEN
               elif this_char in BISHOPS:
                  piece.type = BISHOP
               elif this_char in ROOKS:
                  piece.type = ROOK
               elif this_char in KNIGHTS:
                  piece.type = KNIGHT
               else:
                  print("ERROR: Unknown piece type %s!" % this_char)

            # No matter what type of piece it is, let's put it in the right
            # list.
            game.pieces[piece.side].append(piece)

            # Put this piece in this location.
            this_loc.contents = piece

         # Append this location to the list, empty or full.
         board_row.append(this_loc)

      # Append this row to the board.
      game.board.append(board_row)

   if DEBUG:
      printBoard(game.board)

####
# isValid returns whether a particular coordinate is valid.
def isValid(coord):
   
   if coord >= 0 and coord < N:
      return True
   return False

####
# getMovelist returns a list of valid moves for a particular piece.
def getMovelist(game, piece):

   movelist = []

   # For knights and kings, we don't even bother preculling invalid locations,
   # because we do that check for safety's sake later.  We don't bother culling
   # moves on top of our own pieces for any moves until later as well.
   if piece.type == KNIGHT:
      for delta in ((1, 2), (1, -2), (-1, 2), (-1, -2), (2, 1), (2, -1),
       (-2, 1), (-2, -1)):
         movelist.append((piece.x + delta[0], piece.y + delta[1]))

   elif piece.type == KING:
      for xd in (-1, 0, 1):
         for yd in (-1, 0, 1):
            movelist.append((piece.x + xd, piece.y + yd))

   # For the sliders, we stop once we hit an edge or another piece.  We
   # don't care what type of piece it is; we'll use the later cull to remove
   # it if it's our own.
   else:

      # We use deltas for directions.
      deltas = []
      if piece.type == ROOK or piece.type == QUEEN:
         deltas.extend([(-1, 0), (1, 0), (0, -1), (0, 1)])
      if piece.type == BISHOP or piece.type == QUEEN:
         deltas.extend([(-1, -1), (-1, 1), (1, -1), (1, 1)])

      for delta in deltas:

         # Assume that we're not done.
         done = False
         i = piece.x + delta[0]
         j = piece.y + delta[1]

         while isValid(i) and isValid(j) and not done:

            # No matter what, this is a potential spot to move to.
            movelist.append((i, j))

            if game.board[i][j].contents:

               # There's a piece here.  We're done.
               done = True

            else:

               # Not done yet; follow the delta and continue.
               i += delta[0]
               j += delta[1]

   # Cull time.  Remove all moves where we end on top of our own pieces.
   # Opponents are fine, though; those are captures!
   real_movelist = []
   for move in movelist:
      x = move[0]
      y = move[1]

      # The move needs to be valid, and the cell needs to be empty or
      # have an opponent's piece.
      if isValid(x) and isValid(y) and ((not game.board[x][y].contents) or
       (game.board[x][y].contents.side != piece.side)):
         real_movelist.append(move)

   # Return that mess!
   return real_movelist

####
# inCheck determines whether a king is in check.
def inCheck(game):

   # The way we determine whether a king is in check is simple.  We get the
   # move list of every piece on the other side; if that move list contains
   # the king's location, the king is in check.
   pieces_to_analyze = game.pieces[game.other_side]
   king_loc = (game.kings[game.side_to_analyze].x,
    game.kings[game.side_to_analyze].y)

   for piece in pieces_to_analyze:
      movelist = getMovelist(game, piece)
      if king_loc in movelist:
         
         # Bail early for speed reasons; definitely in check.
         return True

   # We never bailed, so the king wasn't in check.
   return False

####
# isCheckmated analyzes a board and determines whether the side to analyze
# has no moves that would get them out of check.
def isCheckmated(game):

   # We assume that the player cannot get out of check.
   still_in_check = True

   # For every piece the side to analyze has, let's see if it can get us out
   # of check.
   i = 0
   pieces_to_try = game.pieces[game.side_to_analyze]
   while still_in_check and i < len(pieces_to_try):

      this_piece = pieces_to_try[i]
      movelist = getMovelist(game, this_piece)
      original_x = this_piece.x
      original_y = this_piece.y
      j = 0
      while still_in_check and j < len(movelist):

         move = movelist[j]

         # For every move, we're going to move the piece there, do analysis,
         # and move back for the next move.  If the space is empty, this is
         # easy.  If not, it's a bit more complex.
         move_x = move[0]
         move_y = move[1]

         # Either way, the original location is temporarily vacated and the
         # piece moves.
         game.board[original_x][original_y].contents = None
         this_piece.x = move_x
         this_piece.y = move_y

         if game.board[move_x][move_y].contents:

            # A capture.  We need to remove the enemy piece, but not
            # permanently, and analyze.
            captured_piece = game.board[move_x][move_y].contents
            game.pieces[game.other_side].remove(captured_piece)
            game.board[move_x][move_y].contents = this_piece
            if not inCheck(game):

               # Woo!  Not in check any more.
               if DEBUG:
                  print("This move got us out of checkmate:")
                  printBoard(game.board)

               still_in_check = False

            # Return the board to its prior state.
            game.pieces[game.other_side].append(captured_piece)
            game.board[move_x][move_y].contents = captured_piece

         else:

            # Just move and analyze.
            game.board[move_x][move_y].contents = this_piece
            if not inCheck(game):
               if DEBUG:
                  print("This move got us out of checkmate:")
                  printBoard(game.board)

               still_in_check = False

            # Empty the square we moved to after said move.
            game.board[move_x][move_y].contents = None

         # Return the moved piece to its original location for the next go.
         game.board[original_x][original_y].contents = this_piece
         this_piece.x = original_x
         this_piece.y = original_y

         # Increment j to go to the next move.
         j += 1

      # Increment i to go to the next piece.
      i += 1

   # We return whether, after all that analysis, we were still in check every
   # time or not.
   return still_in_check

####
# analyze analyzes a chessboard and determines whether the side to analyze
# is safe, checked, or checkmated.
def analyze(game):

   # If we determine that the side to analyze is checked, we'll set it.
   # If we determine that it's checkmated, we'll set that too.  For now,
   # assume not.
   is_checked = False
   is_checkmated = False

   # First, we determine if the king to analyze is in check.
   if inCheck(game):
      is_checked = True
   else:
      is_checked = False

   # Now, determine whether the king to analyze is checkmated.
   is_checkmated = isCheckmated(game)

   # Now that we've done our analysis, we need to return the state of the
   # analyzed side.  The order of precedence is:
   #
   # Checkmated, otherwise
   # Checked, otherwise
   # safe
   if is_checkmated:
      return CHECKMATED
   elif is_checked:
      return CHECKED
   else:
      return SAFE

def main():

   dataset_count = int(sys.stdin.readline())
   for dataset_loop in range(dataset_count):

      # New game.
      game = Struct()

      # Get the side to analyze.  Store its opposite too.
      game.side_to_analyze = sys.stdin.readline().strip()
      if game.side_to_analyze == BLACK:
         game.other_side = WHITE
      else:
         game.other_side = BLACK

      # Read in the board.
      readBoard(game)

      # Get the result of analysis.
      result = analyze(game)

      # Print the results.
      if game.side_to_analyze == WHITE:
         print("WHITE IS %s" % result)
      else:
         print("BLACK IS %s" % result)

if "__main__" == __name__:
   main()