Chess is a very popular game played by hundreds of millions of people. Nowadays, we have chess engines such as Stockfish and Komodo to help us analyze games. These engines are very powerful pieces of well-developed software that use intelligent ideas and algorithms to analyze positions and sequences of moves, as well as to find tactical ideas. Consider the following simplified version of chess:

Board: 
It's played on a $4\times4$ board between two players named Black and White.
Rows are numbered from $1$ to $4$, where the top row is $4$ and the bottom row is $1$.  
Columns are lettered from $\mbox{A}$ to $\mbox{D}$, where the leftmost column is $\mbox{A}$ and the rightmost column is $\mbox{D}$.
Pieces and Movement: 
White initially has $\boldsymbol{w}$ pieces and Black initially has $\boldsymbol{b}$ pieces.
There are no Kings on the board. Each player initially has exactly $1$ Queen, at most $2$ Pawns, at most $2$ Rooks, and at most $2$ minor pieces (i.e., a Bishop and/or Knight).
White's Pawns move up the board, while Black's Pawns move down the board.
Each move made by any player counts as a single move.
Each piece's possible moves are the same as in classical chess, with the following exceptions:
Pawns cannot move two squares forward.
The en passant move is not possible.
Promotion:
Pawns promote to either a Bishop, Knight, or Rook when they reach the back row (promotion to a Queen is not allowed). 
The players must perform promotions whenever possible. This means White must promote their Pawns when they reach any cell in the top row, and Black must promote their Pawns when they reach any cell in the bottom row. 
Objective: 
The goal of the game is to capture the opponent’s Queen without losing your own.
There will never be a draw or tie scenario like you might see in classical chess.

Given $m$ and the layout of pieces for $\mathrm{~g~}$ games, implement a very basic engine for our simplified version of chess that determines whether or not White can win in $\leq m$ moves (regardless of how Black plays) if White always moves first. For each game, print YES on a new line if White can win in $\leq m$ moves; otherwise, print NO.

Input Format

The first line contains an integer, $\mathrm{~g~}$, denoting the number of games. The subsequent lines describe each game in the following format:

The first line contains three space-separated integers describing the respective values of $\boldsymbol{w}$ (the number of white pieces), $\boldsymbol{b}$ (the number of black pieces), and $m$ (the maximum number of moves we want to know if White can win in).
The $w+b$ subsequent lines describe each chess piece in the form t c r, where $\boldsymbol{\boldsymbol{t}}$ is a character $\in\{Q,N,B,R,P\}$ denoting the type of piece (where $Q$ is Queen, $N$ is Knight, $\mbox{B}$ is Bishop, $\mbox{R}$ is Rook, and $\mbox{P}$ is a Pawn), and $c$ and $r$ denote the respective column and row on the board where the figure is located (where $c\in\{A,B,C,D\}$ and $r\in\{1,2,3,4\}$). These inputs are  given as follows: 
Each of the first $\boldsymbol{w}$ lines describes the type and location of a White piece.
Each of the subsequent $\boldsymbol{b}$ lines describes the type and location of a Black piece.

Constraints

$1\leq g\leq1000$    
$1\leq w,b\leq7$  
$1\leq m\leq6$  
Each player has exactly $1$ Queen, at most $2$ Pawns, at most $2$ Rooks, and at most $2$ minor pieces (i.e., a Bishop and/or Knight).
It is guaranteed that the initial location of each chess piece is distinct.
No pawn is initially placed in a row where it would promote.

Output Format

For each of the $\mathrm{~g~}$ games of simplified chess, print whether or not White can win in $\leq m$ moves on a new line. If it's possible, print YES; otherwise, print NO instead.

Sample Input 0
1
2 1 1
Q B 1
P B 3
Q A 4

Sample Output 0
YES

Explanation 0

We play the following $g=1$ game of simplified chess:

White wins by moving their Pawn to $A4$ and capturing Black's Queen, so we print YES on a new line.

Difficulty: MEDIUM_HARD
Tags: Complete search
Skill types: Complete search

Starter code:

Solutions:
from collections import namedtuple, defaultdict
trans = {'A': 0, 'B': 1, 'C': 2, 'D': 3}
trans_r = {0: 'A', 1: 'B', 2: 'C', 3: 'D'}

def translate(c, r):
	return (int(trans[c]), int(r) - 1)
Piece = namedtuple('Piece', ['side', 'type', 'pos'])
Pos = namedtuple('Position', ['c', 'r'])
pieces = {p: {t: {s: Piece(s, t, p) for s in 'WB'} for t in 'RBQNP'} for p in (Pos(c, r) for c in range(4) for r in range(4))}

def move_paths(c, r):
	orthogonals = (tuple(((c, r_) for r_ in range(r + 1, 4))), tuple(((c, r_) for r_ in reversed(range(r)))), tuple(((c_, r) for c_ in range(c + 1, 4))), tuple(((c_, r) for c_ in reversed(range(c)))))
	diagonals = (tuple(zip(range(c + 1, 4), range(r + 1, 4))), tuple(zip(range(c + 1, 4), reversed(range(r)))), tuple(zip(reversed(range(c)), range(r + 1, 4))), tuple(zip(reversed(range(c)), reversed(range(r)))))
	knights = [(c + 2, r + 1), (c + 2, r - 1), (c - 2, r + 1), (c - 2, r - 1), (c + 1, r + 2), (c - 1, r + 2), (c + 1, r - 2), (c - 1, r - 2)]
	knights = [((c, r),) for (c, r) in knights if 0 <= c < 4 and 0 <= r < 4]
	return {'R': orthogonals, 'B': diagonals, 'Q': orthogonals + diagonals, 'N': knights}
all_move_paths = {(c, r): move_paths(c, r) for c in range(4) for r in range(4)}
promotion_types = 'BNR'
pawn_capture_moves = defaultdict(tuple)
for (c_from, c_to) in ((0, (1,)), (1, (0, 2)), (2, (1, 3)), (3, (2,))):
	for r in range(2):
		pawn_capture_moves[pieces[c_from, r]['P']['W']] = tuple((pieces[to, r + 1]['P']['W'] for to in c_to))
		pawn_capture_moves[pieces[c_from, 3 - r]['P']['B']] = tuple((pieces[to, 2 - r]['P']['B'] for to in c_to))
	pawn_capture_moves[pieces[c_from, 2]['P']['W']] = tuple((pieces[to, 3][typ]['W'] for to in c_to for typ in promotion_types))
	pawn_capture_moves[pieces[c_from, 1]['P']['B']] = tuple((pieces[to, 0][typ]['B'] for to in c_to for typ in promotion_types))
pawn_advance_moves = defaultdict(tuple)
for c in range(4):
	for r in range(2):
		pawn_advance_moves[pieces[c, r]['P']['W']] = (pieces[c, r + 1]['P']['W'],)
		pawn_advance_moves[pieces[c, 3 - r]['P']['B']] = (pieces[c, 2 - r]['P']['B'],)
	pawn_advance_moves[pieces[c, 2]['P']['W']] = tuple((pieces[c, 3][typ]['W'] for typ in promotion_types))
	pawn_advance_moves[pieces[c, 1]['P']['B']] = tuple((pieces[c, 0][typ]['B'] for typ in promotion_types))

def rank(piece):
	if piece.side == 'W':
		return piece.pos[1] + 1
	else:
		return 4 - piece.pos[1]

def win_with_move(grid, pos_from, piece, turn, m):
	grid_ = grid.copy()
	grid_[piece.pos] = piece
	del grid_[pos_from]
	return not forced_win(grid_, 'B' if turn == 'W' else 'W', m - 1)

def forced_win(grid, turn, m):
	for piece in (p for p in grid.values() if p.side == turn):
		if piece.type == 'P':
			if m > 1:
				for new_piece in pawn_advance_moves[piece]:
					if new_piece.pos not in grid and win_with_move(grid, piece.pos, new_piece, turn, m):
						return True
			for new_piece in pawn_capture_moves[piece]:
				other_p = grid.get(new_piece.pos, None)
				if other_p and other_p.side != turn:
					if other_p.type == 'Q':
						return True
					elif m > 1 and win_with_move(grid, piece.pos, new_piece, turn, m):
						return True
			continue
		for path in all_move_paths[piece.pos][piece.type]:
			for pos in path:
				other_p = grid.get(pos, None)
				if other_p and other_p.side == turn:
					break
				elif other_p and other_p.side != turn and (other_p.type == 'Q'):
					return True
				elif m > 1 and win_with_move(grid, piece.pos, Piece(piece.side, piece.type, pos), turn, m):
					return True
				if other_p:
					break
	return False
for _ in range(int(input().strip())):
	(w, b, m) = [int(x) for x in input().strip().split(' ')]
	white = [Piece('W', t, translate(c, r)) for (t, c, r) in (input().strip().split(' ') for _ in range(w))]
	black = [Piece('B', t, translate(c, r)) for (t, c, r) in (input().strip().split(' ') for _ in range(b))]
	grid = {p.pos: p for p in white + black}
	if m % 2 == 0:
		m -= 1
	print('YES' if forced_win(grid, 'W', m) else 'NO')


class Piece(object):

	def __init__(self, color, ptype, row, col):
		self.color = color
		self.ptype = ptype
		self.row = row
		self.col = col
		self.captured = False

	def valid_moves(self, board):
		pass

	def move_will_promote(self):
		return False

	def can_move_to(self, r, c):
		if r == self.row and c == self.col:
			return False
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if board.mat[r][c] is None:
			return True
		elif board.mat[r][c].color != self.color:
			return True
		return False

	def _straight_moves(self, board):
		possible = []
		for row in range(1, 4):
			(r, c) = (self.row - row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for row in range(1, 4):
			(r, c) = (self.row + row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col - col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col + col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

	def _diagonal_moves(self, board):
		possible = []
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

class Rook(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board)
		return possible

class Queen(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board) + self._diagonal_moves(board)
		return possible

class Knight(Piece):

	def valid_moves(self, board):
		(r, c) = (self.row, self.col)
		possible = [(r - 2, c - 1), (r - 2, c + 1), (r - 1, c + 2), (r - 1, c - 2), (r + 1, c + 2), (r + 1, c - 2), (r + 2, c - 1), (r + 2, c + 1)]
		return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]

class Bishop(Piece):

	def valid_moves(self, board):
		possible = self._diagonal_moves(board)
		return possible

class Pawn(Piece):

	def can_move_to(self, r, c):
		if self.ptype == 'P':
			if r == self.row and c == self.col:
				return False
			if r < 0 or r > 3:
				return False
			if c < 0 or c > 3:
				return False
			if self.color == 'W' and r < self.row:
				return False
			elif self.color == 'B' and self.row < r:
				return False
			if c == self.col:
				if board.mat[r][c] is None:
					return True
			elif board.mat[r][c] is not None and board.mat[r][c].color != self.color:
				return True
			return False
		else:
			return Piece.can_move_to(self, r, c)

	def move_will_promote(self):
		if self.ptype == 'P':
			if self.color == 'W' and self.row == 2:
				return True
			elif self.color == 'B' and self.row == 1:
				return True
		return False

	def valid_moves(self, board):
		if self.ptype == 'P':
			(r, c) = (self.row, self.col)
			possible = [(r - 1, c), (r - 1, c - 1), (r - 1, c + 1), (r + 1, c), (r + 1, c - 1), (r + 1, c + 1)]
			return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]
		elif self.ptype == 'N':
			return Knight.valid_moves(self, board)
		elif self.ptype == 'R':
			return Rook.valid_moves(self, board)
		elif self.ptype == 'B':
			return Bishop.valid_moves(self, board)

	def promote_to(self, pType):
		self.ptype = pType

	def undo_promote(self):
		self.ptype = 'P'

class Board(object):

	def __init__(self):
		self.mat = [[None for i in range(4)] for u in range(4)]
		self.white = []
		self.black = []

	def add_piece(self, color, ptype, row, col):
		if ptype == 'Q':
			piece = Queen(color, ptype, row, col)
		elif ptype == 'N':
			piece = Knight(color, ptype, row, col)
		elif ptype == 'B':
			piece = Bishop(color, ptype, row, col)
		elif ptype == 'R':
			piece = Rook(color, ptype, row, col)
		elif ptype == 'P':
			piece = Pawn(color, ptype, row, col)
		else:
			raise Exception('Invalid type')
		self.mat[row][col] = piece
		if color == 'W':
			self.white.append(piece)
		elif color == 'B':
			self.black.append(piece)

	def move_to(self, piece, row, col):
		self.mat[piece.row][piece.col] = None
		if self.mat[row][col] is not None:
			captured = self.mat[row][col]
			captured.captured = True
		self.mat[row][col] = piece
		piece.row = row
		piece.col = col

	def undo_move(self, captured, piece, prev_r, prev_c):
		self.move_to(piece, prev_r, prev_c)
		if captured:
			(cr, cc) = (captured.row, captured.col)
			self.mat[cr][cc] = captured
			captured.captured = False

	def is_open(self, r, c):
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if self.mat[r][c] is None:
			return True
		return False

	def white_can_win(self, turns_left):
		if turns_left <= 0:
			return False
		white_pieces = self.white
		q = []
		for piece in white_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return True
				q.insert(0, (piece, r, c))
		for (piece, r, c) in q:
			(prev_r, prev_c) = (piece.row, piece.col)
			captured = self.mat[r][c]
			willPromote = piece.move_will_promote()
			self.move_to(piece, r, c)
			if willPromote:
				for pType in ['N', 'B', 'R']:
					piece.promote_to(pType)
					if self.black_cannot_avoid_loss(turns_left - 1):
						piece.undo_promote()
						self.undo_move(captured, piece, prev_r, prev_c)
						return True
					piece.undo_promote()
			elif self.black_cannot_avoid_loss(turns_left - 1):
				self.undo_move(captured, piece, prev_r, prev_c)
				return True
			self.undo_move(captured, piece, prev_r, prev_c)
		return False

	def black_cannot_avoid_loss(self, turns_left):
		if turns_left <= 0:
			return False
		black_pieces = self.black
		for piece in black_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				(prev_r, prev_c) = (piece.row, piece.col)
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return False
				willPromote = piece.move_will_promote()
				self.move_to(piece, r, c)
				if willPromote:
					for pType in ['N', 'B', 'R']:
						piece.promote_to(pType)
						if not self.white_can_win(turns_left - 1):
							piece.undo_promote()
							self.undo_move(captured, piece, prev_r, prev_c)
							return False
						piece.undo_promote()
				elif not self.white_can_win(turns_left - 1):
					self.undo_move(captured, piece, prev_r, prev_c)
					return False
				self.undo_move(captured, piece, prev_r, prev_c)
		return True
games = int(input())
for g in range(games):
	board = Board()
	cols = 'ABCD'
	(w, b, m) = map(int, input().split(' '))
	for i in range(w):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('W', ptype, row, col)
	for i in range(b):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('B', ptype, row, col)
	print('YES' if board.white_can_win(m) else 'NO')


class Piece:

	def __init__(self, color, typ, row, col):
		self.color = color
		self.type = typ
		self.row = row
		self.col = col
		self.captured = False

	def valid_moves(self, board):
		if self.type == 'R':
			return self.rook_moves(board)
		if self.type == 'Q':
			return self.queen_moves(board)
		if self.type == 'B':
			return self.bishop_moves(board)
		if self.type == 'N':
			return self.knight_moves(board)
		if self.type == 'P':
			return self.pawn_moves(board)

	def rook_moves(self, board):
		return self.straight(board)

	def queen_moves(self, board):
		return self.straight(board) + self.diagonal(board)

	def bishop_moves(self, board):
		return self.diagonal(board)

	def knight_moves(self, board):
		(r, c) = (self.row, self.col)
		result = []
		offsets = [(-2, -1), (-2, 1), (2, -1), (2, 1), (-1, 2), (-1, -2), (1, 2), (1, -2)]
		for (i, j) in offsets:
			if self.can_move_to(board, r + i, c + j):
				result.append((r + i, c + j, None))
		return result

	def pawn_moves(self, board):
		(r, c) = (self.row, self.col)
		result = []
		if self.color == 'B':
			L = []
			if self.can_move_to(board, r - 1, c - 1):
				if board.l[r - 1][c - 1] is not None:
					L.append((r - 1, c - 1))
			if self.can_move_to(board, r - 1, c + 1):
				if board.l[r - 1][c + 1] is not None:
					L.append((r - 1, c + 1))
			if self.can_move_to(board, r - 1, c):
				if board.l[r - 1][c] is None:
					L.append((r - 1, c))
			for i in L:
				if i[0] == 0:
					result.append((i[0], i[1], 'B'))
					result.append((i[0], i[1], 'N'))
					result.append((i[0], i[1], 'R'))
				else:
					result.append((i[0], i[1], None))
		else:
			L = []
			if self.can_move_to(board, r + 1, c - 1):
				if board.l[r + 1][c - 1] is not None:
					L.append((r + 1, c - 1))
			if self.can_move_to(board, r + 1, c + 1):
				if board.l[r + 1][c + 1] is not None:
					L.append((r + 1, c + 1))
			if self.can_move_to(board, r + 1, c):
				if board.l[r + 1][c] is None:
					L.append((r + 1, c))
			for i in L:
				if i[0] == 3:
					result.append((i[0], i[1], 'B'))
					result.append((i[0], i[1], 'N'))
					result.append((i[0], i[1], 'R'))
				else:
					result.append((i[0], i[1], None))
		return result

	def can_move_to(self, board, r, c):
		if r == self.row and c == self.col:
			return False
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if board.l[r][c] is None:
			return True
		elif board.l[r][c].color != self.color:
			return True
		return False

	def straight(self, board):
		result = []
		for i in range(1, 4):
			(r, c) = (self.row - i, self.col)
			if self.can_move_to(board, r, c):
				result.append((r, c, None))
			if not board.is_open(r, c):
				break
		for i in range(1, 4):
			(r, c) = (self.row + i, self.col)
			if self.can_move_to(board, r, c):
				result.append((r, c, None))
			if not board.is_open(r, c):
				break
		for i in range(1, 4):
			(r, c) = (self.row, self.col + i)
			if self.can_move_to(board, r, c):
				result.append((r, c, None))
			if not board.is_open(r, c):
				break
		for i in range(1, 4):
			(r, c) = (self.row, self.col - i)
			if self.can_move_to(board, r, c):
				result.append((r, c, None))
			if not board.is_open(r, c):
				break
		return result

	def diagonal(self, board):
		result = []
		for i in range(1, 4):
			(r, c) = (self.row - i, self.col - i)
			if self.can_move_to(board, r, c):
				result.append((r, c, None))
			if not board.is_open(r, c):
				break
		for i in range(1, 4):
			(r, c) = (self.row + i, self.col - i)
			if self.can_move_to(board, r, c):
				result.append((r, c, None))
			if not board.is_open(r, c):
				break
		for i in range(1, 4):
			(r, c) = (self.row - i, self.col + i)
			if self.can_move_to(board, r, c):
				result.append((r, c, None))
			if not board.is_open(r, c):
				break
		for i in range(1, 4):
			(r, c) = (self.row + i, self.col + i)
			if self.can_move_to(board, r, c):
				result.append((r, c, None))
			if not board.is_open(r, c):
				break
		return result

	def __str__(self):
		if self.color == 'B':
			return self.type.lower()
		return self.type
	__repr__ = __str__

class Board:

	def __init__(self):
		self.l = [[None for i in range(4)] for j in range(4)]
		self.white = []
		self.black = []

	def add_piece(self, color, typ, row, col):
		piece = Piece(color, typ, row, col)
		self.l[row][col] = piece
		if color == 'W':
			self.white.append(piece)
		else:
			self.black.append(piece)

	def move_to(self, piece, row, col, promotion=None):
		self.l[piece.row][piece.col] = None
		if self.l[row][col] is not None:
			captured = self.l[row][col]
			captured.captured = True
		if promotion is not None:
			piece.type = promotion
		self.l[row][col] = piece
		piece.row = row
		piece.col = col

	def undo(self, captured, piece, prev_r, prev_c, prev_typ=None):
		self.move_to(piece, prev_r, prev_c, prev_typ)
		if captured:
			(cr, cc) = (captured.row, captured.col)
			self.l[cr][cc] = captured
			captured.captured = False

	def is_open(self, r, c):
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if self.l[r][c] is None:
			return True
		return False

	def white_can_win(self, turns_left):
		if turns_left <= 0:
			return False
		white_pieces = self.white
		q = []
		for piece in white_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c, p) in valid_moves:
				captured = self.l[r][c]
				if captured is not None and captured.type == 'Q':
					return True
				q.insert(0, (piece, r, c, p))
		for (piece, r, c, p) in q:
			(prev_r, prev_c, prev_typ) = (piece.row, piece.col, piece.type)
			captured = self.l[r][c]
			self.move_to(piece, r, c, p)
			if self.black_cannot_avoid_loss(turns_left - 1):
				self.undo(captured, piece, prev_r, prev_c, prev_typ)
				return True
			self.undo(captured, piece, prev_r, prev_c, prev_typ)
		return False

	def black_cannot_avoid_loss(self, turns_left):
		if turns_left <= 0:
			return False
		black_pieces = self.black
		for piece in black_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c, p) in valid_moves:
				(prev_r, prev_c, prev_typ) = (piece.row, piece.col, piece.type)
				captured = self.l[r][c]
				if captured is not None and captured.type == 'Q':
					return False
				self.move_to(piece, r, c, p)
				if not self.white_can_win(turns_left - 1):
					self.undo(captured, piece, prev_r, prev_c, prev_typ)
					return False
				self.undo(captured, piece, prev_r, prev_c, prev_typ)
		return True

	def __str__(self):
		r = []
		for i in range(4):
			l = []
			for j in range(4):
				if self.l[i][j] is None:
					l.append('-')
				else:
					l.append(str(self.l[i][j]))
			r.append(''.join(l))
		return '\n'.join(r[::-1])
	__repr__ = __str__

def f(white, black, m):
	board = Board()
	cols = 'ABCD'
	for (typ, col, row) in white:
		col = cols.index(col)
		row -= 1
		board.add_piece('W', typ, row, col)
	for (typ, col, row) in black:
		col = cols.index(col)
		row -= 1
		board.add_piece('B', typ, row, col)
	return ['NO', 'YES'][board.white_can_win(m)]
g = int(input().strip())
for i in range(g):
	(w, b, m) = map(int, input().split(' '))
	white = []
	black = []
	for i in range(w):
		(t, c, r) = input().strip().split(' ')
		white.append([t, c, int(r)])
	for i in range(b):
		(t, c, r) = input().strip().split(' ')
		black.append([t, c, int(r)])
	print(f(white, black, m))


from string import ascii_uppercase
from collections import namedtuple, defaultdict
from itertools import chain, repeat
from enum import Enum
Piece = namedtuple('Piece', ['side', 'type', 'pos'])
Pos = namedtuple('Position', ['c', 'r'])
Path = namedtuple('Path', ['capture', 'path'])
(W, B) = 'WB'

def gen_move_table(n):
	promos = 'BNR'
	pieces = {(c, r): {s + t: Piece(s, t, (c, r)) for s in 'WB' for t in 'RBQNP'} for c in range(n) for r in range(n)}
	P = lambda s, t, p: pieces[p][s + t]

	def move_paths(c, r):
		orthogonals = (tuple(((c, r_) for r_ in range(r + 1, n))), tuple(((c, r_) for r_ in reversed(range(r)))), tuple(((c_, r) for c_ in range(c + 1, n))), tuple(((c_, r) for c_ in reversed(range(c)))))
		diagonals = (tuple(zip(range(c + 1, n), range(r + 1, n))), tuple(zip(range(c + 1, n), reversed(range(r)))), tuple(zip(reversed(range(c)), range(r + 1, n))), tuple(zip(reversed(range(c)), reversed(range(r)))))
		knights = [(c + 2, r + 1), (c + 2, r - 1), (c - 2, r + 1), (c - 2, r - 1), (c + 1, r + 2), (c - 1, r + 2), (c + 1, r - 2), (c - 1, r - 2)]
		knights = tuple((((c_, r_),) for (c_, r_) in knights if 0 <= c_ < n and 0 <= r_ < n))
		return {'R': orthogonals, 'B': diagonals, 'Q': orthogonals + diagonals, 'N': knights}
	all_paths = defaultdict(tuple)
	for p_from in ((c, r) for c in range(n) for r in range(n)):
		for (t, paths) in move_paths(*p_from).items():
			for s in 'WB':
				all_paths[Piece(s, t, p_from)] = tuple((Path(1, tuple((P(s, t, p_to) for p_to in path))) for path in paths))
	ct = {0: ((0, 0), (2, 1)), n - 1: ((2, n - 2), (0, n - 1))}
	ct.update({x: ((2, x - 1), (0, x), (2, x + 1)) for x in range(1, n - 1)})
	for c in range(n):
		for r in range(n - 2):
			all_paths[P(W, 'P', (c, r))] = tuple((Path(cap, (P(W, 'P', (to, r + 1)),)) for (cap, to) in ct[c]))
			all_paths[P(B, 'P', (c, n - 1 - r))] = tuple((Path(cap, (P(B, 'P', (to, n - 2 - r)),)) for (cap, to) in ct[c]))
		all_paths[P(W, 'P', (c, n - 2))] = tuple((Path(cap, (P(W, typ, (to, n - 1)),)) for (cap, to) in ct[c] for typ in promos))
		all_paths[P(B, 'P', (c, 1))] = tuple((Path(cap, (P(B, typ, (to, 0)),)) for (cap, to) in ct[c] for typ in promos))
	return all_paths
all_paths = gen_move_table(4)

def win_with_move(grid, pos_from, piece, turn, m):
	if m == 1:
		return False
	grid_ = grid.copy()
	grid_[piece.pos] = piece
	del grid_[pos_from]
	return not forced_win(grid_, 'B' if turn == 'W' else 'W', m - 1)

def forced_win(grid, turn, m):
	next_turn = 'B' if turn == 'W' else 'W'
	for piece in (p for p in grid.values() if p.side == turn):
		for (capture, path) in all_paths[piece]:
			for new_piece in path:
				other_p = grid.get(new_piece.pos, None)
				if other_p:
					if other_p.side != turn and capture:
						if other_p.type == 'Q':
							return True
						if m > 1:
							grid_ = grid.copy()
							grid_[new_piece.pos] = new_piece
							del grid_[piece.pos]
							if not forced_win(grid_, next_turn, m - 1):
								return True
					break
				elif m > 1 and capture != 2:
					grid_ = grid.copy()
					grid_[new_piece.pos] = new_piece
					del grid_[piece.pos]
					if not forced_win(grid_, next_turn, m - 1):
						return True
	return False
trans = dict(zip(ascii_uppercase, range(8)))
trans_r = dict(zip(range(8), ascii_uppercase))

def translate(c, r):
	return (int(trans[c]), int(r) - 1)
for _ in range(int(input().strip())):
	(w, b, m) = [int(x) for x in input().strip().split(' ')]
	white = [Piece('W', t, translate(c, r)) for (t, c, r) in (input().strip().split(' ') for _ in range(w))]
	black = [Piece('B', t, translate(c, r)) for (t, c, r) in (input().strip().split(' ') for _ in range(b))]
	grid = {p.pos: p for p in white + black}
	if m % 2 == 0:
		m -= 1
	print('YES' if forced_win(grid, 'W', m) else 'NO')


from itertools import product
WHITE = -1
BLACK = 1
FREE = 0
SIZE = 4
EMPTY = (FREE, -1)

def within_bounds(y, x):
	return 0 <= y < 4 and 0 <= x < 4
COORDINATES = tuple(product(range(4), repeat=2))
DIRECTIONS = {'Q': [(y, x) for (y, x) in product([-1, 0, 1], repeat=2) if y or x], 'N': [(y, x) for (y, x) in product([-2, -1, 1, 2], repeat=2) if (y + x) % 2], 'B': list(product([-1, 1], repeat=2)), 'R': [(y, x) for (y, x) in product([-1, 0, 1], repeat=2) if (y + x) % 2]}

def generate_moves():
	board = [[None] * 4 for _ in range(4)]
	for (y, x) in COORDINATES:
		d = {}
		for (piece, directions) in DIRECTIONS.items():
			piece_moves = []
			for (dy, dx) in directions:
				(ny, nx) = (y, x)
				piece_directions = []
				for _ in range(3):
					ny += dy
					nx += dx
					if not within_bounds(ny, nx):
						break
					piece_directions.append((ny, nx))
				piece_moves.append(piece_directions)
			d[piece] = piece_moves
		board[y][x] = d
	return board
MOVES = generate_moves()

def sequential_moves(player, piece, y, x, board):
	for direction in MOVES[y][x][piece]:
		for (y, x) in direction:
			owner = board[y][x][0]
			if owner == player:
				break
			yield (piece, y, x)
			if owner != FREE:
				break

def pawn_moves(player, other, y, x, board):
	y += player
	if y % 3:
		if x > 0 and board[y][x - 1][0] == other:
			yield ('P', y, x - 1)
		if x < 3 and board[y][x + 1][0] == other:
			yield ('P', y, x + 1)
		if board[y][x] == EMPTY:
			yield ('P', y, x)
	else:
		if x > 0 and board[y][x - 1][0] == other:
			yield ('R', y, x - 1)
			yield ('B', y, x - 1)
			yield ('N', y, x - 1)
		if x < 3 and board[y][x + 1][0] == other:
			yield ('R', y, x + 1)
			yield ('B', y, x + 1)
			yield ('N', y, x + 1)
		if board[y][x] == EMPTY:
			yield ('R', y, x)
			yield ('B', y, x)
			yield ('N', y, x)

def play(player, player_locations, other, other_locations, board, turns_left):
	for (y, x) in player_locations:
		(_, src_piece) = board[y][x]
		if src_piece == 'P':
			gen = pawn_moves(player, other, y, x, board)
		else:
			gen = sequential_moves(player, src_piece, y, x, board)
		for (to_piece, to_y, to_x) in gen:
			existing = board[to_y][to_x]
			if existing[1] == 'Q':
				return player == WHITE
			if turns_left == 1:
				continue
			board[to_y][to_x] = (player, to_piece)
			board[y][x] = EMPTY
			to_loc = {(to_y, to_x)}
			res = play(other, other_locations - to_loc, player, player_locations - {(y, x)} | to_loc, board, turns_left - 1)
			board[to_y][to_x] = existing
			board[y][x] = (player, src_piece)
			if res and player == WHITE:
				return True
			elif not res and player == BLACK:
				return False
	return player == BLACK

def read_pieces(owner, board, n):
	locations = set()
	for _ in range(n):
		(t, x, y) = input().split()
		x = ord(x) - ord('A')
		y = abs(int(y) - 4)
		board[y][x] = (owner, t)
		locations.add((y, x))
	return locations
for i in range(int(input())):
	(w, b, m) = (int(x) for x in input().split())
	board = [[EMPTY] * 4 for _ in range(4)]
	white_locations = read_pieces(WHITE, board, w)
	black_locations = read_pieces(BLACK, board, b)
	m = m - (m % 2 == 0)
	print('YES' if play(WHITE, white_locations, BLACK, black_locations, board, m) else 'NO')


import sys

def piece_slays_other(pos, color_id, layout):
	for color in [0, 1]:
		for other_piece in layout[color]:
			if other_piece[1] == pos[0] and other_piece[2] == pos[1]:
				return ['self', other_piece] if color == color_id else ['other', other_piece]
	return ['nothing', None]

def get_next_positions(layout, piece, color_id):
	directions = []
	if piece[0] == 'Q':
		directions += [(0, -1), (1, 0), (0, 1), (-1, 0), (1, -1), (1, 1), (-1, 1), (-1, -1)]
	elif piece[0] == 'N':
		directions += [(2, -1), (2, 1), (-2, -1), (-2, 1), (1, 2), (-1, 2), (1, -2), (-1, -2)]
	elif piece[0] == 'B':
		directions += [(1, -1), (1, 1), (-1, 1), (-1, -1)]
	elif piece[0] == 'R':
		directions += [(0, -1), (1, 0), (0, 1), (-1, 0)]
	elif piece[0] == 'P':
		directions += [(1 - 2 * color_id, -1), (1 - 2 * color_id, 0), (1 - 2 * color_id, 1)]
	next_pos_list = []
	for direction in directions:
		next_pos = [piece[1] + direction[0], piece[2] + direction[1]]
		if piece[0] == 'N':
			if 1 <= next_pos[0] <= 4 and 1 <= next_pos[1] <= 4:
				slay_id = piece_slays_other(next_pos, color_id, layout)
				if slay_id[0] != 'self':
					next_pos_list.append(list(next_pos) + slay_id)
		elif piece[0] == 'P':
			if 1 <= next_pos[0] <= 4 and 1 <= next_pos[1] <= 4:
				slay_id = piece_slays_other(next_pos, color_id, layout)
				if piece[0] == 'P':
					if direction[1] != 0:
						if slay_id[0] == 'other':
							next_pos_list.append(list(next_pos) + slay_id)
					elif slay_id[0] == 'nothing':
						next_pos_list.append(list(next_pos) + slay_id)
		else:
			slay_id = piece_slays_other(next_pos, color_id, layout)
			while 1 <= next_pos[0] <= 4 and 1 <= next_pos[1] <= 4 and (slay_id[0] == 'nothing'):
				next_pos_list.append(list(next_pos) + slay_id)
				next_pos = [next_pos[0] + direction[0], next_pos[1] + direction[1]]
				slay_id = piece_slays_other(next_pos, color_id, layout)
			if 1 <= next_pos[0] <= 4 and 1 <= next_pos[1] <= 4 and (slay_id[0] == 'other'):
				next_pos_list.append(list(next_pos) + slay_id)
	return next_pos_list

def get_new_layouts(layout, color_id):
	new_layout_list = []
	for piece in layout[color_id]:
		next_pos_list = get_next_positions(layout, piece, color_id)
		for next_pos in next_pos_list:
			if piece[0] == 'P' and next_pos[0] == 4 - 3 * color_id:
				new_piece_list = ['N', 'B', 'R']
			else:
				new_piece_list = [piece[0]]
			for new_piece_id in new_piece_list:
				new_layout_entry = [[], []]
				new_layout_entry[color_id] = {piece_i: piece_i for piece_i in layout[color_id] if piece_i is not piece}
				new_piece = (new_piece_id, next_pos[0], next_pos[1])
				new_layout_entry[color_id][new_piece] = new_piece
				other_color_id = abs(color_id - 1)
				if next_pos[2] == 'other':
					new_layout_entry[other_color_id] = {piece_i: piece_i for piece_i in layout[other_color_id] if piece_i is not next_pos[-1]}
				else:
					new_layout_entry[other_color_id] = {piece_i: piece_i for piece_i in layout[other_color_id]}
				new_layout_list.append(new_layout_entry)
	return new_layout_list

def no_queen(layout, color_id):
	try:
		layout[color_id]
	except IndexError or KeyError:
		return False
	for piece_i in layout[color_id]:
		if piece_i[0] == 'Q':
			return False
	return True

def is_queen_changed(board1, board2, color_id):
	for piece in board1[color_id]:
		if piece[0] == 'Q':
			if piece in board2[color_id]:
				return False
			return True

def queen_can_be_slayed(board, color_id):
	other_color_id = abs(color_id - 1)
	for piece in board[other_color_id]:
		piece_positions_other_color = get_next_positions(board, piece, other_color_id)
		for position in piece_positions_other_color:
			if position[2] == 'other' and position[3][0] == 'Q':
				return True
	return False

def can_white_win(board_layout, turns):
	if turns > 0 and (not no_queen(board_layout, 0)):
		feasible_layouts_after_white = []
		feasible_layout_added = False
		for layout_after_white in get_new_layouts(board_layout, 0):
			if no_queen(layout_after_white, 1):
				return 'YES'
			queen_changed = is_queen_changed(board_layout, layout_after_white, 0)
			if not queen_changed or (queen_changed and (not queen_can_be_slayed(layout_after_white, 0))):
				feasible_layouts_after_white.append(layout_after_white)
				feasible_layout_added = True
		turns -= 1
		if not feasible_layout_added or turns < 2:
			return 'NO'
		for layout_after_white in feasible_layouts_after_white:
			queen_id = True
			winnable = True
			all_layouts_after_black = get_new_layouts(layout_after_white, 1)
			for layout_after_black in all_layouts_after_black:
				if no_queen(layout_after_black, 0):
					(winnable, queen_id) = (False, False)
					break
			if queen_id:
				for layout_after_black in all_layouts_after_black:
					if can_white_win(layout_after_black, turns - 1) == 'NO':
						winnable = False
						break
			if winnable:
				return 'YES'
	return 'NO'
for _ in range(int(input().strip())):
	[w, b, max_turns] = list(map(int, input().strip().split()))
	initial_layout = [{}, {}]
	for color_n in range(2):
		num_pieces = w if color_n == 0 else b
		for __ in range(num_pieces):
			piece_info = input().strip().split()
			column = 1 if piece_info[1] == 'A' else 2 if piece_info[1] == 'B' else 3 if piece_info[1] == 'C' else 4
			piece_id = (piece_info[0], int(piece_info[2]), column)
			initial_layout[color_n][piece_id] = piece_id
	print(can_white_win(initial_layout, max_turns))


def toColumnNumber(c):
	return 1 + ord(c) - ord('A')

def toBoardIndex(row, column):
	return row + column * 4

def getTile(board, row, column):
	return board[toBoardIndex(row, column)]

def getMoves(piece, board):
	(color, ptype, column, row) = piece
	moves = []
	if ptype == 'R' or ptype == 'Q':
		x = column - 1
		while 0 <= x:
			if placeable(piece, board, row, x):
				moves.append((row, x))
			if getTile(board, row, x) is not None:
				break
			x -= 1
		x = column + 1
		while x < 4:
			if placeable(piece, board, row, x):
				moves.append((row, x))
			if getTile(board, row, x) is not None:
				break
			x += 1
		y = row - 1
		while 0 <= y:
			if placeable(piece, board, y, column):
				moves.append((y, column))
			if getTile(board, y, column) is not None:
				break
			y -= 1
		y = row + 1
		while 4 > y:
			if placeable(piece, board, y, column):
				moves.append((y, column))
			if getTile(board, y, column) is not None:
				break
			y += 1
	if ptype == 'Q' or ptype == 'B':
		x = column + 1
		y = row + 1
		while x < 4 and y < 4:
			if placeable(piece, board, y, x):
				moves.append((y, x))
			if getTile(board, y, x) is not None:
				break
			x += 1
			y += 1
		x = column + 1
		y = row - 1
		while x < 4 and y >= 0:
			if placeable(piece, board, y, x):
				moves.append((y, x))
			if getTile(board, y, x) is not None:
				break
			x += 1
			y -= 1
		x = column - 1
		y = row - 1
		while x >= 0 and y >= 0:
			if placeable(piece, board, y, x):
				moves.append((y, x))
			if getTile(board, y, x) is not None:
				break
			x -= 1
			y -= 1
		x = column - 1
		y = row + 1
		while x >= 0 and y < 4:
			if placeable(piece, board, y, x):
				moves.append((y, x))
			if getTile(board, y, x) is not None:
				break
			x -= 1
			y += 1
	if ptype == 'N':
		knight_moves = []
		for i in [1, -1, 2, -2]:
			for j in [1, -1, 2, -2]:
				if abs(i) + abs(j) == 3:
					knight_moves.append((i, j))
		for m in knight_moves:
			(m_x, m_y) = m
			x = column + m_x
			y = row + m_y
			if x < 4 and x >= 0 and (y < 4) and (y >= 0) and placeable(piece, board, y, x):
				moves.append((y, x))
	if ptype == 'P':

		def appendMoves(r, c):
			if r == 0 or r == 3:
				for promotion_piece in ['R', 'N', 'B']:
					moves.append((r, c, promotion_piece))
			else:
				moves.append((r, c))
		if color == WHITE:
			next_row = row + 1
		else:
			next_row = row - 1
		if column < 3:
			next_column = column + 1
			if getTile(board, next_row, next_column) is not None and placeable(piece, board, next_row, next_column):
				appendMoves(next_row, next_column)
		if column > 0:
			next_column = column - 1
			if getTile(board, next_row, next_column) is not None and placeable(piece, board, next_row, next_column):
				appendMoves(next_row, next_column)
		if getTile(board, next_row, column) is None:
			appendMoves(next_row, column)
	return moves

def printBoard(board):
	print('-' * 10)
	for i in range(3, -1, -1):
		print(i + 1, end='')
		for j in range(0, 4):
			tile = getTile(board, i, j)
			if tile is None:
				print(' - ', end='')
			else:
				print('{:>3}'.format(('B' if BLACK == tile[0] else 'W') + tile[1]), end='')
		print()
	print('  {:<3}{:<3}{:<3}{:<3}'.format('A', 'B', 'C', 'D'))

def placeable(piece, board, row, column):
	(pcolor, _, _, _) = piece
	tile_piece = getTile(board, row, column)
	if tile_piece is None:
		return True
	tpcolor = tile_piece[0]
	return pcolor != tpcolor
WHITE = 0
BLACK = 1

def addPieceToBoard(piece, board):
	(_, _, column, row) = piece
	board[toBoardIndex(row, column)] = piece

def updateBoard(piece, move, board, white_pieces, black_pieces, turn):
	if len(move) == 3:
		(row, column, next_p_type) = move
	else:
		(row, column) = move
		next_p_type = None
	next_board = [tile for tile in board]
	next_white_pieces = [p for p in white_pieces]
	next_black_pieces = [p for p in black_pieces]
	next_turn = BLACK if turn == WHITE else WHITE
	if turn == WHITE:
		my_pieces = next_white_pieces
		op_pieces = next_black_pieces
	else:
		my_pieces = next_black_pieces
		op_pieces = next_white_pieces
	index = my_pieces.index(piece)
	(pcolor, ptype, pcolumn, prow) = piece
	if next_p_type is not None:
		ptype = next_p_type
	my_pieces[index] = (pcolor, ptype, column, row)
	next_board[toBoardIndex(prow, pcolumn)] = None
	prev_piece = getTile(board, row, column)
	if prev_piece is not None:
		op_pieces.remove(prev_piece)
	next_board[toBoardIndex(row, column)] = my_pieces[index]
	return (next_board, next_white_pieces, next_black_pieces, next_turn)

def canQueenBeCapturedInOneMove(board, turn, black_pieces, white_pieces):
	if turn == WHITE:
		my_pieces = white_pieces
		op_pieces = black_pieces
	else:
		my_pieces = black_pieces
		op_pieces = white_pieces
	op_queen = [piece for piece in op_pieces if piece[1] == 'Q'][0]
	(op_color, _, column, row) = op_queen
	fake_rook = (op_color, 'R', column, row)
	for move in getMoves(fake_rook, board):
		piece = getTile(board, move[0], move[1])
		if piece is not None:
			(pcolor, ptype, _, _) = piece
			if pcolor == turn and (ptype == 'R' or ptype == 'Q'):
				return True
	fake_bishop = (op_color, 'B', column, row)
	for move in getMoves(fake_bishop, board):
		piece = getTile(board, move[0], move[1])
		if piece is not None:
			(pcolor, ptype, _, _) = piece
			if pcolor == turn and (ptype == 'B' or ptype == 'Q'):
				return True
	fake_knight = (op_color, 'N', column, row)
	for move in getMoves(fake_knight, board):
		piece = getTile(board, move[0], move[1])
		if piece is not None:
			(pcolor, ptype, _, _) = piece
			if pcolor == turn and ptype == 'N':
				return True
	capture_row = row + (-1 if op_color == BLACK else 1)
	if capture_row <= 3 and capture_row >= 0:
		if column > 0:
			left = column - 1
			pawn = getTile(board, capture_row, left)
			if pawn is not None and pawn[0] != op_color and (pawn[1] == 'P'):
				return True
		if column < 3:
			right = column + 1
			pawn = getTile(board, capture_row, right)
			if pawn is not None and pawn[0] != op_color and (pawn[1] == 'P'):
				return True
	return False

def canWhiteWin(board, white_pieces, black_pieces, turn, remaining_moves):
	if remaining_moves == 0:
		return False
	if canQueenBeCapturedInOneMove(board, turn, black_pieces, white_pieces):
		return WHITE == turn
	if turn == WHITE:
		my_pieces = white_pieces
		op_pieces = black_pieces
	else:
		my_pieces = black_pieces
		op_pieces = white_pieces
	if turn == WHITE:
		for piece in my_pieces:
			for move in getMoves(piece, board):
				(next_board, next_white_pieces, next_black_pieces, next_turn) = updateBoard(piece, move, board, white_pieces, black_pieces, turn)
				result = canWhiteWin(next_board, next_white_pieces, next_black_pieces, next_turn, remaining_moves - 1)
				if result:
					return True
		return False
	else:
		for piece in my_pieces:
			for move in getMoves(piece, board):
				(next_board, next_white_pieces, next_black_pieces, next_turn) = updateBoard(piece, move, board, white_pieces, black_pieces, turn)
				result = canWhiteWin(next_board, next_white_pieces, next_black_pieces, next_turn, remaining_moves - 1)
				if not result:
					return False
		return True
games = int(input())
for game in range(games):
	(w, b, m) = [int(s) for s in input().split()]
	white_pieces = []
	black_pieces = []
	board = [None] * 4 * 5
	turn = WHITE
	for i in range(w):
		(ptype, column, row) = input().split()
		piece = (WHITE, ptype, toColumnNumber(column) - 1, int(row) - 1)
		white_pieces.append(piece)
		addPieceToBoard(piece, board)
	for j in range(b):
		(ptype, column, row) = input().split()
		piece = (BLACK, ptype, toColumnNumber(column) - 1, int(row) - 1)
		black_pieces.append(piece)
		addPieceToBoard(piece, board)
	result = False
	m = m - (m + 1) % 2
	for i in range(1, m + 1):
		result = canWhiteWin(board, white_pieces, black_pieces, turn, m)
		if result:
			break
	if result:
		print('YES')
	else:
		print('NO')


import os
import sys

def isTherePiece(color, i, j):
	for (p, k, l) in color:
		if i == k and j == l:
			return [p, i, j]
	return ['None', i, j]

def isInGrid(i, j, n, m):
	return i >= 0 and j >= 0 and (i < n) and (j < m)

def movesFromList(l, repeat, p, i, j, color, others, n, m, isw):
	mfl = []
	if p == 'P':
		dir = +1 if isw else -1
		if isInGrid(i, j + dir, n, m) and isTherePiece(color, i, j + dir)[0] == 'None' and (isTherePiece(others, i, j + dir)[0] == 'None'):
			if j + dir == m - 1 or j + dir == 0:
				mfl.append(['N', i, j + dir])
				mfl.append(['B', i, j + dir])
				mfl.append(['R', i, j + dir])
			else:
				mfl.append([p, i, j + dir])
		for jump in (-1, 1):
			itp = isTherePiece(others, i + jump, j + dir)
			if isInGrid(i + jump, j + dir, n, m) and itp[0] != 'None':
				if itp[0] == 'Q':
					return [1, [p, i + jump, j + dir]]
				if j + dir == m - 1 or j + dir == 0:
					mfl.insert(0, ['N', i + jump, j + dir])
					mfl.insert(0, ['B', i + jump, j + dir])
					mfl.insert(0, ['R', i + jump, j + dir])
				else:
					mfl.insert(0, [p, i + jump, j + dir])
		return [0, mfl]
	for (x, y) in l:
		k = i + x
		l = j + y
		while isInGrid(k, l, n, m) and isTherePiece(color, k, l)[0] == 'None':
			itp = isTherePiece(others, k, l)
			if itp[0] != 'None':
				mfl.insert(0, [p, k, l])
				if itp[0] == 'Q':
					return [1, [p, k, l]]
				break
			else:
				mfl.append([p, k, l])
			if not repeat:
				break
			k += x
			l += y
	return [0, mfl]

def possibleMovesPiece(p, i, j, color, others, n, m, isw):
	pms = []
	if p == 'Q':
		pms = movesFromList([[1, 1], [1, -1], [-1, -1], [-1, 1], [1, 0], [0, -1], [-1, 0], [0, 1]], True, p, i, j, color, others, n, m, isw)
	elif p == 'N':
		pms = movesFromList([[2, 1], [2, -1], [-2, 1], [-2, -1], [1, 2], [1, -2], [-1, 2], [-1, -2]], False, p, i, j, color, others, n, m, isw)
	elif p == 'R':
		pms = movesFromList([[1, 0], [0, -1], [-1, 0], [0, 1]], True, p, i, j, color, others, n, m, isw)
	elif p == 'B':
		pms = movesFromList([[1, 1], [1, -1], [-1, -1], [-1, 1]], True, p, i, j, color, others, n, m, isw)
	elif p == 'P':
		pms = movesFromList([[1, 1], [1, -1], [-1, -1], [-1, 1]], True, p, i, j, color, others, n, m, isw)
	return ([p, i, j], pms)

def possibleMoves(color, others, n, m, isw):
	pms = []
	for (p, i, j) in color:
		pmp = possibleMovesPiece(p, i, j, color, others, n, m, isw)
		if pmp[1][0] == 1:
			return [pmp]
		pms += [pmp]
	return pms

def play(color, others, moves, n, m, isw):
	if moves < 1 and isw:
		return False
	if moves < 1 and (not isw):
		return True
	pms = possibleMoves(color, others, n, m, isw)
	copycolor = list((c for c in color))
	copyothers = list((o for o in others))
	if len(pms) == 1 and pms[0][1][0] == 1:
		return True
	for (b, pmp) in pms:
		for mv in pmp[1]:
			color.remove(b)
			color.append(mv)
			op = isTherePiece(others, mv[1], mv[2])
			if op[0] != 'None':
				others.remove(op)
			if not play(others, color, moves - 1, n, m, not isw):
				return True
			color = list((c for c in copycolor))
			others = list((o for o in copyothers))
	return False

def simplifiedChessEngine(whites, blacks, moves):
	n = m = 4
	if play(whites, blacks, moves, n, m, True):
		return 'YES'
	else:
		return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for i in range(w):
		whites.append(list(map(lambda x: x[0], input().rstrip().split())))
		whites[i][1] = ord(whites[i][1]) - ord('A')
		whites[i][2] = int(whites[i][2]) - 1
	blacks = []
	for i in range(b):
		blacks.append(list(map(lambda x: x[0], input().rstrip().split())))
		blacks[i][1] = ord(blacks[i][1]) - ord('A')
		blacks[i][2] = int(blacks[i][2]) - 1
	result = simplifiedChessEngine(whites, blacks, m)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys
import json

def determine(whites, blacks, move):
	test(whites, blacks, move)

def test(whites, blacks, limit):

	def assign(w, b):
		board = [['  ' for _ in range(4)] for _ in range(4)]
		for (i, j) in zip([w, b], 'WB'):
			for (a, b, c) in i:
				board[4 - int(c)]['ABCD'.index(b)] = j + a
		return board

	def display(p):
		for i in p:
			print(i)
	board = assign(whites, blacks)

	def is_valid(x, y):
		return 0 <= x < 4 and 0 <= y < 4

	def get_knight_moves(i, j, turn):
		moves = []
		for (k, l) in [(2, -1), (2, 1), (-2, -1), (-2, 1), (-1, -2), (-1, 2), (1, -2), (1, 2)]:
			(ni, nj) = (i + k, j + l)
			if is_valid(ni, nj) and (board[ni][nj] == '  ' or board[ni][nj][0] == 'BW'[turn]):
				moves.append(((i, j), (ni, nj, '')))
		return moves

	def get_bishop_moves(i, j, turn):
		moves = set()
		for (k, l) in [(1, -1), (1, 1), (-1, 1), (-1, -1)]:
			(x, y) = (i + k, j + l)
			while is_valid(x, y) and board[x][y] == '  ':
				moves.add(((i, j), (x, y, '')))
				x += k
				y += l
			if is_valid(x, y) and board[x][y][0] == 'BW'[turn]:
				moves.add(((i, j), (x, y, '')))
		return list(moves)

	def get_rook_moves(i, j, turn):
		moves = set()
		for (k, l) in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
			(x, y) = (i + k, j + l)
			while is_valid(x, y) and board[x][y] == '  ':
				moves.add(((i, j), (x, y, '')))
				x += k
				y += l
			if is_valid(x, y) and board[x][y][0] == 'BW'[turn]:
				moves.add(((i, j), (x, y, '')))
		return list(moves)

	def get_queen_moves(i, j, turn):
		return get_bishop_moves(i, j, turn) + get_rook_moves(i, j, turn)

	def get_pawn_moves(i, j, turn):
		moves = []
		if not turn:
			for (k, l) in [(-1, -1), (-1, 1)]:
				(ni, nj) = (i + k, j + l)
				if is_valid(ni, nj) and board[ni][nj][0] == 'B':
					if ni == 0:
						for n in 'BRN':
							moves.append(((i, j), (ni, nj, 'W' + n)))
					else:
						moves.append(((i, j), (ni, nj, '')))
			(ni, nj) = (i - 1, j)
			if board[ni][nj] == '  ':
				if ni == 0:
					for k in 'BRN':
						moves.append(((i, j), (ni, nj, 'W' + k)))
				else:
					moves.append(((i, j), (ni, nj, '')))
		else:
			for (k, l) in [(1, -1), (1, 1)]:
				(ni, nj) = (i + k, j + l)
				if is_valid(ni, nj) and board[ni][nj][0] == 'W':
					if ni == 3:
						for n in 'BRN':
							moves.append(((i, j), (ni, nj, 'B' + n)))
					else:
						moves.append(((i, j), (ni, nj, '')))
			(ni, nj) = (i + 1, j)
			if board[ni][nj] == '  ':
				if ni == 3:
					for k in 'BRN':
						moves.append(((i, j), (ni, nj, 'B' + k)))
				else:
					moves.append(((i, j), (ni, nj, '')))
		return moves

	def get_moves(board, turn):
		moves = []
		for (i, j) in enumerate(board):
			for (k, l) in enumerate(j):
				if l[0] == 'WB'[turn]:
					if l[1] == 'N':
						moves.extend(get_knight_moves(i, k, turn))
					elif l[1] == 'Q':
						moves.extend(get_queen_moves(i, k, turn))
					elif l[1] == 'B':
						moves.extend(get_bishop_moves(i, k, turn))
					elif l[1] == 'R':
						moves.extend(get_rook_moves(i, k, turn))
					elif l[1] == 'P':
						moves.extend(get_pawn_moves(i, k, turn))
		return moves

	def make_move(i, j, k, l, promotion):
		(board[k][l], board[i][j]) = (promotion or board[i][j], '  ')

	def take_queen(moves, i, j):
		return any((k == i and l == j for (_, (k, l, _)) in moves))

	def find_queen(board, turn):
		for (i, j) in enumerate(board):
			for (k, l) in enumerate(j):
				if l[0] == 'WB'[turn] and l[1] == 'Q':
					return (i, k)
		return None

	def derive(board, turn, move_count, visited={}):
		if move_count == limit:
			return False
		possible_moves = get_moves(board, turn)
		if take_queen(possible_moves, *find_queen(board, turn ^ 1)):
			return not turn
		if not turn:
			string = json.dumps(board)
			for (source, destination) in possible_moves:
				copy = json.loads(string)
				make_move(*source, *destination)
				if derive(board, turn ^ 1, move_count + 1, visited):
					return True
				board[:] = copy
			return False
		else:
			string = json.dumps(board)
			for (source, destination) in possible_moves:
				copy = json.loads(string)
				make_move(*source, *destination)
				if not derive(board, turn ^ 1, move_count + 1, visited):
					return False
				board[:] = copy
			return True
	print('YES' if derive(board, 0, 0) else 'NO')
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	determine(whites, blacks, m)


from collections import namedtuple, defaultdict
trans = {'A': 0, 'B': 1, 'C': 2, 'D': 3}
trans_r = {0: 'A', 1: 'B', 2: 'C', 3: 'D'}

def translate(c, r):
	return (int(trans[c]), int(r) - 1)
Piece = namedtuple('Piece', ['side', 'type', 'pos'])
Pos = namedtuple('Position', ['c', 'r'])
pieces = {p: {t: {s: Piece(s, t, p) for s in 'WB'} for t in 'RBQNP'} for p in (Pos(c, r) for c in range(4) for r in range(4))}

def move_paths(c, r):
	orthogonals = (tuple(((c, r_) for r_ in range(r + 1, 4))), tuple(((c, r_) for r_ in reversed(range(r)))), tuple(((c_, r) for c_ in range(c + 1, 4))), tuple(((c_, r) for c_ in reversed(range(c)))))
	diagonals = (tuple(zip(range(c + 1, 4), range(r + 1, 4))), tuple(zip(range(c + 1, 4), reversed(range(r)))), tuple(zip(reversed(range(c)), range(r + 1, 4))), tuple(zip(reversed(range(c)), reversed(range(r)))))
	knights = [(c + 2, r + 1), (c + 2, r - 1), (c - 2, r + 1), (c - 2, r - 1), (c + 1, r + 2), (c - 1, r + 2), (c + 1, r - 2), (c - 1, r - 2)]
	knights = [((c, r),) for (c, r) in knights if 0 <= c < 4 and 0 <= r < 4]
	return {'R': orthogonals, 'B': diagonals, 'Q': orthogonals + diagonals, 'N': knights}
all_move_paths = {(c, r): move_paths(c, r) for c in range(4) for r in range(4)}
promotion_types = 'BNR'
pawn_capture_moves = defaultdict(tuple)
for (c_from, c_to) in ((0, (1,)), (1, (0, 2)), (2, (1, 3)), (3, (2,))):
	for r in range(2):
		pawn_capture_moves[pieces[c_from, r]['P']['W']] = tuple((pieces[to, r + 1]['P']['W'] for to in c_to))
		pawn_capture_moves[pieces[c_from, 3 - r]['P']['B']] = tuple((pieces[to, 2 - r]['P']['B'] for to in c_to))
	pawn_capture_moves[pieces[c_from, 2]['P']['W']] = tuple((pieces[to, 3][typ]['W'] for to in c_to for typ in promotion_types))
	pawn_capture_moves[pieces[c_from, 1]['P']['B']] = tuple((pieces[to, 0][typ]['B'] for to in c_to for typ in promotion_types))
pawn_advance_moves = defaultdict(tuple)
for c in range(4):
	for r in range(2):
		pawn_advance_moves[pieces[c, r]['P']['W']] = (pieces[c, r + 1]['P']['W'],)
		pawn_advance_moves[pieces[c, 3 - r]['P']['B']] = (pieces[c, 2 - r]['P']['B'],)
	pawn_advance_moves[pieces[c, 2]['P']['W']] = tuple((pieces[c, 3][typ]['W'] for typ in promotion_types))
	pawn_advance_moves[pieces[c, 1]['P']['B']] = tuple((pieces[c, 0][typ]['B'] for typ in promotion_types))

def rank(piece):
	if piece.side == 'W':
		return piece.pos[1] + 1
	else:
		return 4 - piece.pos[1]

def win_with_move(grid, pos_from, piece, turn, m):
	grid_ = grid.copy()
	grid_[piece.pos] = piece
	del grid_[pos_from]
	return not forced_win(grid_, 'B' if turn == 'W' else 'W', m - 1)

def forced_win(grid, turn, m):
	for piece in (p for p in grid.values() if p.side == turn):
		if piece.type == 'P':
			if m > 1:
				for new_piece in pawn_advance_moves[piece]:
					if new_piece.pos not in grid and win_with_move(grid, piece.pos, new_piece, turn, m):
						return True
			for new_piece in pawn_capture_moves[piece]:
				other_p = grid.get(new_piece.pos, None)
				if other_p and other_p.side != turn:
					if other_p.type == 'Q':
						return True
					elif m > 1 and win_with_move(grid, piece.pos, new_piece, turn, m):
						return True
			continue
		for path in all_move_paths[piece.pos][piece.type]:
			for pos in path:
				other_p = grid.get(pos, None)
				if other_p and other_p.side == turn:
					break
				elif other_p and other_p.side != turn and (other_p.type == 'Q'):
					return True
				elif m > 1 and win_with_move(grid, piece.pos, Piece(piece.side, piece.type, pos), turn, m):
					return True
				if other_p:
					break
	return False
for _ in range(int(input().strip())):
	(w, b, m) = [int(x) for x in input().strip().split(' ')]
	white = [Piece('W', t, translate(c, r)) for (t, c, r) in (input().strip().split(' ') for _ in range(w))]
	black = [Piece('B', t, translate(c, r)) for (t, c, r) in (input().strip().split(' ') for _ in range(b))]
	grid = {p.pos: p for p in white + black}
	if m % 2 == 0:
		m -= 1
	print('YES' if forced_win(grid, 'W', m) else 'NO')


import math
import os
import random
import re
import sys
import json

def determine(whites, blacks, move):
	test(whites, blacks, move)

def test(whites, blacks, limit):

	def assign(w, b):
		board = [['  ' for _ in range(4)] for _ in range(4)]
		for (i, j) in zip([w, b], 'WB'):
			for (a, b, c) in i:
				board[4 - int(c)]['ABCD'.index(b)] = j + a
		return board

	def display(p):
		for i in p:
			print(i)
	board = assign(whites, blacks)

	def is_valid(x, y):
		return 0 <= x < 4 and 0 <= y < 4

	def get_knight_moves(i, j, turn):
		moves = []
		for (k, l) in [(2, -1), (2, 1), (-2, -1), (-2, 1), (-1, -2), (-1, 2), (1, -2), (1, 2)]:
			(ni, nj) = (i + k, j + l)
			if is_valid(ni, nj) and (board[ni][nj] == '  ' or board[ni][nj][0] == 'BW'[turn]):
				moves.append(((i, j), (ni, nj, '')))
		return moves

	def get_bishop_moves(i, j, turn):
		moves = set()
		for (k, l) in [(1, -1), (1, 1), (-1, 1), (-1, -1)]:
			(x, y) = (i + k, j + l)
			while is_valid(x, y) and board[x][y] == '  ':
				moves.add(((i, j), (x, y, '')))
				x += k
				y += l
			if is_valid(x, y) and board[x][y][0] == 'BW'[turn]:
				moves.add(((i, j), (x, y, '')))
		return list(moves)

	def get_rook_moves(i, j, turn):
		moves = set()
		for (k, l) in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
			(x, y) = (i + k, j + l)
			while is_valid(x, y) and board[x][y] == '  ':
				moves.add(((i, j), (x, y, '')))
				x += k
				y += l
			if is_valid(x, y) and board[x][y][0] == 'BW'[turn]:
				moves.add(((i, j), (x, y, '')))
		return list(moves)

	def get_queen_moves(i, j, turn):
		return get_bishop_moves(i, j, turn) + get_rook_moves(i, j, turn)

	def get_pawn_moves(i, j, turn):
		moves = []
		if not turn:
			for (k, l) in [(-1, -1), (-1, 1)]:
				(ni, nj) = (i + k, j + l)
				if is_valid(ni, nj) and board[ni][nj][0] == 'B':
					if ni == 0:
						for n in 'BRN':
							moves.append(((i, j), (ni, nj, 'W' + n)))
					else:
						moves.append(((i, j), (ni, nj, '')))
			(ni, nj) = (i - 1, j)
			if board[ni][nj] == '  ':
				if ni == 0:
					for k in 'BRN':
						moves.append(((i, j), (ni, nj, 'W' + k)))
				else:
					moves.append(((i, j), (ni, nj, '')))
		else:
			for (k, l) in [(1, -1), (1, 1)]:
				(ni, nj) = (i + k, j + l)
				if is_valid(ni, nj) and board[ni][nj][0] == 'W':
					if ni == 3:
						for n in 'BRN':
							moves.append(((i, j), (ni, nj, 'B' + n)))
					else:
						moves.append(((i, j), (ni, nj, '')))
			(ni, nj) = (i + 1, j)
			if board[ni][nj] == '  ':
				if ni == 3:
					for k in 'BRN':
						moves.append(((i, j), (ni, nj, 'B' + k)))
				else:
					moves.append(((i, j), (ni, nj, '')))
		return moves

	def get_moves(board, turn):
		moves = []
		for (i, j) in enumerate(board):
			for (k, l) in enumerate(j):
				if l[0] == 'WB'[turn]:
					if l[1] == 'N':
						moves.extend(get_knight_moves(i, k, turn))
					elif l[1] == 'Q':
						moves.extend(get_queen_moves(i, k, turn))
					elif l[1] == 'B':
						moves.extend(get_bishop_moves(i, k, turn))
					elif l[1] == 'R':
						moves.extend(get_rook_moves(i, k, turn))
					elif l[1] == 'P':
						moves.extend(get_pawn_moves(i, k, turn))
		return moves

	def make_move(i, j, k, l, promotion):
		(board[k][l], board[i][j]) = (promotion or board[i][j], '  ')

	def take_queen(moves, i, j):
		return any((k == i and l == j for (_, (k, l, _)) in moves))

	def find_queen(board, turn):
		for (i, j) in enumerate(board):
			for (k, l) in enumerate(j):
				if l[0] == 'WB'[turn] and l[1] == 'Q':
					return (i, k)
		return None

	def derive(board, turn, move_count, visited={}):
		if move_count == limit:
			return False
		possible_moves = get_moves(board, turn)
		if take_queen(possible_moves, *find_queen(board, turn ^ 1)):
			return not turn
		if not turn:
			string = json.dumps(board)
			for (source, destination) in possible_moves:
				copy = json.loads(string)
				make_move(*source, *destination)
				if derive(board, turn ^ 1, move_count + 1, visited):
					return True
				board[:] = copy
			return False
		else:
			string = json.dumps(board)
			for (source, destination) in possible_moves:
				copy = json.loads(string)
				make_move(*source, *destination)
				if not derive(board, turn ^ 1, move_count + 1, visited):
					return False
				board[:] = copy
			return True
	print('YES' if derive(board, 0, 0) else 'NO')
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	determine(whites, blacks, m)


import os
import sys
import copy
memoi = {}

def move_rook(t, whites, blacks):
	move = []
	for i in range(t[0] - 1, 0, -1):
		if (i, t[1]) in whites:
			break
		elif (i, t[1]) in blacks:
			move.append((i, t[1]))
			break
		else:
			move.append((i, t[1]))
	for i in range(t[0] + 1, 5):
		if (i, t[1]) in whites:
			break
		elif (i, t[1]) in blacks:
			move.append((i, t[1]))
			break
		else:
			move.append((i, t[1]))
	for i in range(t[1] - 1, 0, -1):
		if (t[0], i) in whites:
			break
		elif (t[0], i) in blacks:
			move.append((t[0], i))
			break
		else:
			move.append((t[0], i))
	for i in range(t[1] + 1, 5):
		if (t[0], i) in whites:
			break
		elif (t[0], i) in blacks:
			move.append((t[0], i))
			break
		else:
			move.append((t[0], i))
	return move

def move_bishop(t, whites, blacks):
	move = []
	for (i, j) in zip(range(t[0] - 1, 0, -1), range(t[1] - 1, 0, -1)):
		if (i, j) in whites:
			break
		elif (i, j) in blacks:
			move.append((i, j))
			break
		else:
			move.append((i, j))
	for (i, j) in zip(range(t[0] - 1, 0, -1), range(t[1] + 1, 5)):
		if (i, j) in whites:
			break
		elif (i, j) in blacks:
			move.append((i, j))
			break
		else:
			move.append((i, j))
	for (i, j) in zip(range(t[0] + 1, 5), range(t[1] - 1, 0, -1)):
		if (i, j) in whites:
			break
		elif (i, j) in blacks:
			move.append((i, j))
			break
		else:
			move.append((i, j))
	for (i, j) in zip(range(t[0] + 1, 5), range(t[1] + 1, 5)):
		if (i, j) in whites:
			break
		elif (i, j) in blacks:
			move.append((i, j))
			break
		else:
			move.append((i, j))
	return move

def move_knight(t, whites, blacks):
	move = []
	if t[0] > 2:
		if t[1] > 1:
			if (t[0] - 2, t[1] - 1) not in whites:
				move.append((t[0] - 2, t[1] - 1))
			if t[1] > 2:
				if (t[0] - 1, t[1] - 2) not in whites:
					move.append((t[0] - 1, t[1] - 2))
				if t[0] == 3:
					if (t[0] + 1, t[1] - 2) not in whites:
						move.append((t[0] + 1, t[1] - 2))
		if t[1] < 4:
			if (t[0] - 2, t[1] + 1) not in whites:
				move.append((t[0] - 2, t[1] + 1))
			if t[1] < 3:
				if (t[0] - 1, t[1] + 2) not in whites:
					move.append((t[0] - 1, t[1] + 2))
				if t[0] == 3:
					if (t[0] + 1, t[1] + 2) not in whites:
						move.append((t[0] + 1, t[1] + 2))
	else:
		if t[1] > 1:
			if (t[0] + 2, t[1] - 1) not in whites:
				move.append((t[0] + 2, t[1] - 1))
			if t[1] > 2:
				if (t[0] + 1, t[1] - 2) not in whites:
					move.append((t[0] + 1, t[1] - 2))
				if t[0] == 2:
					if (t[0] - 1, t[1] - 2) not in whites:
						move.append((t[0] - 1, t[1] - 2))
		if t[1] < 4:
			if (t[0] + 2, t[1] + 1) not in whites:
				move.append((t[0] + 2, t[1] + 1))
			if t[1] < 3:
				if (t[0] + 1, t[1] + 2) not in whites:
					move.append((t[0] + 1, t[1] + 2))
				if t[0] == 2:
					if (t[0] - 1, t[1] + 2) not in whites:
						move.append((t[0] - 1, t[1] + 2))
	return move

def move_pawn(t, whites, blacks, wb):
	move = []
	if wb:
		if (t[0] + 1, t[1] + 1) in blacks.keys():
			move.append((t[0] + 1, t[1] + 1))
		if (t[0] - 1, t[1] + 1) in blacks.keys():
			move.append((t[0] - 1, t[1] + 1))
		if (t[0], t[1] + 1) not in blacks.keys() and (t[0], t[1] + 1) not in whites.keys():
			move.append((t[0], t[1] + 1))
		return move
	else:
		if (t[0] - 1, t[1] - 1) in blacks.keys():
			move.append((t[0] - 1, t[1] - 1))
		if (t[0] + 1, t[1] - 1) in blacks.keys():
			move.append((t[0] + 1, t[1] - 1))
		if (t[0], t[1] - 1) not in blacks.keys() and (t[0], t[1] - 1) not in whites.keys():
			move.append((t[0], t[1] - 1))
		return move

def simplifiedChessEngine(whites, blacks, m):
	if m == 0:
		return 'YES'
	for key in blacks.keys():
		if blacks[key] == 'Q':
			PBQ = copy.deepcopy(key)
			break
	moves = []
	for key in whites.keys():
		if whites[key] == 'N':
			moves.append(move_knight(key, whites, blacks))
			if PBQ in moves[-1]:
				return 'YES'
		if whites[key] == 'Q':
			moves.append(move_rook(key, whites, blacks) + move_bishop(key, whites, blacks))
			if PBQ in moves[-1]:
				return 'YES'
		if whites[key] == 'R':
			moves.append(move_rook(key, whites, blacks))
			if PBQ in moves[-1]:
				return 'YES'
		if whites[key] == 'B':
			moves.append(move_bishop(key, whites, blacks))
			if PBQ in moves[-1]:
				return 'YES'
		if whites[key] == 'P':
			moves.append(move_pawn(key, whites, blacks, m % 2))
			if PBQ in moves[-1]:
				return 'YES'
	for (count, key) in enumerate(whites.keys()):
		kw = copy.deepcopy(whites)
		kb = copy.deepcopy(blacks)
		del kw[key]
		for i in moves[count]:
			dd = {}
			if i in kb.keys():
				dd[i] = kb[i]
				del kb[i]
			if whites[key] == 'P' and key[1] == 3 and (m % 2 == 1):
				for piece in ['B', 'N', 'R']:
					kw[i] = piece
					if simplifiedChessEngine(kb, kw, m - 1) == 'NO':
						return 'YES'
			elif whites[key] == 'P' and key[1] == 2 and (m % 2 == 0):
				for piece in ['B', 'N', 'R']:
					kw[i] = piece
					if simplifiedChessEngine(kb, kw, m - 1) == 'NO':
						return 'YES'
			else:
				kw[i] = whites[key]
				if simplifiedChessEngine(kb, kw, m - 1) == 'NO':
					return 'YES'
			del kw[i]
			kb.update(dd)
	return 'NO'
g = int(input())
result = []
for g_itr in range(g):
	(w, b, m) = map(int, input().split())
	whites = []
	for _ in range(w):
		whites.append(list(map(lambda x: x[0], input().rstrip().split())))
	blacks = []
	for _ in range(b):
		blacks.append(list(map(lambda x: x[0], input().rstrip().split())))
	white = {}
	black = {}
	for k in whites:
		white[ord(k[1]) - 64, ord(k[2]) - 48] = k[0]
	for k in blacks:
		black[ord(k[1]) - 64, ord(k[2]) - 48] = k[0]
	m -= 0 if m % 2 else 1
	result.append(simplifiedChessEngine(white, black, m))
for i in result:
	print(i)


class Piece(object):

	def __init__(self, color, ptype, row, col):
		self.color = color
		self.ptype = ptype
		self.row = row
		self.col = col
		self.captured = False

	def valid_moves(self, board):
		pass

	def move_will_promote(self):
		return False

	def can_move_to(self, r, c):
		if r == self.row and c == self.col:
			return False
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if board.mat[r][c] is None:
			return True
		elif board.mat[r][c].color != self.color:
			return True
		return False

	def _straight_moves(self, board):
		possible = []
		for row in range(1, 4):
			(r, c) = (self.row - row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for row in range(1, 4):
			(r, c) = (self.row + row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col - col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col + col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

	def _diagonal_moves(self, board):
		possible = []
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

class Rook(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board)
		return possible

class Queen(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board) + self._diagonal_moves(board)
		return possible

class Knight(Piece):

	def valid_moves(self, board):
		(r, c) = (self.row, self.col)
		possible = [(r - 2, c - 1), (r - 2, c + 1), (r - 1, c + 2), (r - 1, c - 2), (r + 1, c + 2), (r + 1, c - 2), (r + 2, c - 1), (r + 2, c + 1)]
		return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]

class Bishop(Piece):

	def valid_moves(self, board):
		possible = self._diagonal_moves(board)
		return possible

class Pawn(Piece):

	def can_move_to(self, r, c):
		if self.ptype == 'P':
			if r == self.row and c == self.col:
				return False
			if r < 0 or r > 3:
				return False
			if c < 0 or c > 3:
				return False
			if self.color == 'W' and r < self.row:
				return False
			elif self.color == 'B' and self.row < r:
				return False
			if c == self.col:
				if board.mat[r][c] is None:
					return True
			elif board.mat[r][c] is not None and board.mat[r][c].color != self.color:
				return True
			return False
		else:
			return Piece.can_move_to(self, r, c)

	def move_will_promote(self):
		if self.ptype == 'P':
			if self.color == 'W' and self.row == 2:
				return True
			elif self.color == 'B' and self.row == 1:
				return True
		return False

	def valid_moves(self, board):
		if self.ptype == 'P':
			(r, c) = (self.row, self.col)
			possible = [(r - 1, c), (r - 1, c - 1), (r - 1, c + 1), (r + 1, c), (r + 1, c - 1), (r + 1, c + 1)]
			return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]
		elif self.ptype == 'N':
			return Knight.valid_moves(self, board)
		elif self.ptype == 'R':
			return Rook.valid_moves(self, board)
		elif self.ptype == 'B':
			return Bishop.valid_moves(self, board)

	def promote_to(self, pType):
		self.ptype = pType

	def undo_promote(self):
		self.ptype = 'P'

class Board(object):

	def __init__(self):
		self.mat = [[None for i in range(4)] for u in range(4)]
		self.white = []
		self.black = []

	def add_piece(self, color, ptype, row, col):
		if ptype == 'Q':
			piece = Queen(color, ptype, row, col)
		elif ptype == 'N':
			piece = Knight(color, ptype, row, col)
		elif ptype == 'B':
			piece = Bishop(color, ptype, row, col)
		elif ptype == 'R':
			piece = Rook(color, ptype, row, col)
		elif ptype == 'P':
			piece = Pawn(color, ptype, row, col)
		else:
			raise Exception('Invalid type')
		self.mat[row][col] = piece
		if color == 'W':
			self.white.append(piece)
		elif color == 'B':
			self.black.append(piece)

	def move_to(self, piece, row, col):
		self.mat[piece.row][piece.col] = None
		if self.mat[row][col] is not None:
			captured = self.mat[row][col]
			captured.captured = True
		self.mat[row][col] = piece
		piece.row = row
		piece.col = col

	def undo_move(self, captured, piece, prev_r, prev_c):
		self.move_to(piece, prev_r, prev_c)
		if captured:
			(cr, cc) = (captured.row, captured.col)
			self.mat[cr][cc] = captured
			captured.captured = False

	def is_open(self, r, c):
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if self.mat[r][c] is None:
			return True
		return False

	def white_can_win(self, turns_left):
		if turns_left <= 0:
			return False
		white_pieces = self.white
		q = []
		for piece in white_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return True
				q.insert(0, (piece, r, c))
		for (piece, r, c) in q:
			(prev_r, prev_c) = (piece.row, piece.col)
			captured = self.mat[r][c]
			willPromote = piece.move_will_promote()
			self.move_to(piece, r, c)
			if willPromote:
				for pType in ['N', 'B', 'R']:
					piece.promote_to(pType)
					if self.black_cannot_avoid_loss(turns_left - 1):
						piece.undo_promote()
						self.undo_move(captured, piece, prev_r, prev_c)
						return True
					piece.undo_promote()
			elif self.black_cannot_avoid_loss(turns_left - 1):
				self.undo_move(captured, piece, prev_r, prev_c)
				return True
			self.undo_move(captured, piece, prev_r, prev_c)
		return False

	def black_cannot_avoid_loss(self, turns_left):
		if turns_left <= 0:
			return False
		black_pieces = self.black
		for piece in black_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				(prev_r, prev_c) = (piece.row, piece.col)
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return False
				willPromote = piece.move_will_promote()
				self.move_to(piece, r, c)
				if willPromote:
					for pType in ['N', 'B', 'R']:
						piece.promote_to(pType)
						if not self.white_can_win(turns_left - 1):
							piece.undo_promote()
							self.undo_move(captured, piece, prev_r, prev_c)
							return False
						piece.undo_promote()
				elif not self.white_can_win(turns_left - 1):
					self.undo_move(captured, piece, prev_r, prev_c)
					return False
				self.undo_move(captured, piece, prev_r, prev_c)
		return True
games = int(input())
for g in range(games):
	board = Board()
	cols = 'ABCD'
	(w, b, m) = map(int, input().split(' '))
	for i in range(w):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('W', ptype, row, col)
	for i in range(b):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('B', ptype, row, col)
	print('YES' if board.white_can_win(m) else 'NO')


class Piece(object):

	def __init__(self, color, ptype, row, col):
		self.color = color
		self.ptype = ptype
		self.row = row
		self.col = col
		self.captured = False

	def valid_moves(self, board):
		pass

	def move_will_promote(self):
		return False

	def can_move_to(self, r, c):
		if r == self.row and c == self.col:
			return False
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if board.mat[r][c] is None:
			return True
		elif board.mat[r][c].color != self.color:
			return True
		return False

	def _straight_moves(self, board):
		possible = []
		for row in range(1, 4):
			(r, c) = (self.row - row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for row in range(1, 4):
			(r, c) = (self.row + row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col - col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col + col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

	def _diagonal_moves(self, board):
		possible = []
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

class Rook(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board)
		return possible

class Queen(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board) + self._diagonal_moves(board)
		return possible

class Knight(Piece):

	def valid_moves(self, board):
		(r, c) = (self.row, self.col)
		possible = [(r - 2, c - 1), (r - 2, c + 1), (r - 1, c + 2), (r - 1, c - 2), (r + 1, c + 2), (r + 1, c - 2), (r + 2, c - 1), (r + 2, c + 1)]
		return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]

class Bishop(Piece):

	def valid_moves(self, board):
		possible = self._diagonal_moves(board)
		return possible

class Pawn(Piece):

	def can_move_to(self, r, c):
		if self.ptype == 'P':
			if r == self.row and c == self.col:
				return False
			if r < 0 or r > 3:
				return False
			if c < 0 or c > 3:
				return False
			if self.color == 'W' and r < self.row:
				return False
			elif self.color == 'B' and self.row < r:
				return False
			if c == self.col:
				if board.mat[r][c] is None:
					return True
			elif board.mat[r][c] is not None and board.mat[r][c].color != self.color:
				return True
			return False
		else:
			return Piece.can_move_to(self, r, c)

	def move_will_promote(self):
		if self.ptype == 'P':
			if self.color == 'W' and self.row == 2:
				return True
			elif self.color == 'B' and self.row == 1:
				return True
		return False

	def valid_moves(self, board):
		if self.ptype == 'P':
			(r, c) = (self.row, self.col)
			possible = [(r - 1, c), (r - 1, c - 1), (r - 1, c + 1), (r + 1, c), (r + 1, c - 1), (r + 1, c + 1)]
			return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]
		elif self.ptype == 'N':
			return Knight.valid_moves(self, board)
		elif self.ptype == 'R':
			return Rook.valid_moves(self, board)
		elif self.ptype == 'B':
			return Bishop.valid_moves(self, board)

	def promote_to(self, pType):
		self.ptype = pType

	def undo_promote(self):
		self.ptype = 'P'

class Board(object):

	def __init__(self):
		self.mat = [[None for i in range(4)] for u in range(4)]
		self.white = []
		self.black = []

	def add_piece(self, color, ptype, row, col):
		if ptype == 'Q':
			piece = Queen(color, ptype, row, col)
		elif ptype == 'N':
			piece = Knight(color, ptype, row, col)
		elif ptype == 'B':
			piece = Bishop(color, ptype, row, col)
		elif ptype == 'R':
			piece = Rook(color, ptype, row, col)
		elif ptype == 'P':
			piece = Pawn(color, ptype, row, col)
		else:
			raise Exception('Invalid type')
		self.mat[row][col] = piece
		if color == 'W':
			self.white.append(piece)
		elif color == 'B':
			self.black.append(piece)

	def move_to(self, piece, row, col):
		self.mat[piece.row][piece.col] = None
		if self.mat[row][col] is not None:
			captured = self.mat[row][col]
			captured.captured = True
		self.mat[row][col] = piece
		piece.row = row
		piece.col = col

	def undo_move(self, captured, piece, prev_r, prev_c):
		self.move_to(piece, prev_r, prev_c)
		if captured:
			(cr, cc) = (captured.row, captured.col)
			self.mat[cr][cc] = captured
			captured.captured = False

	def is_open(self, r, c):
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if self.mat[r][c] is None:
			return True
		return False

	def white_can_win(self, turns_left):
		if turns_left <= 0:
			return False
		white_pieces = self.white
		q = []
		for piece in white_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return True
				q.insert(0, (piece, r, c))
		for (piece, r, c) in q:
			(prev_r, prev_c) = (piece.row, piece.col)
			captured = self.mat[r][c]
			willPromote = piece.move_will_promote()
			self.move_to(piece, r, c)
			if willPromote:
				for pType in ['N', 'B', 'R']:
					piece.promote_to(pType)
					if self.black_cannot_avoid_loss(turns_left - 1):
						piece.undo_promote()
						self.undo_move(captured, piece, prev_r, prev_c)
						return True
					piece.undo_promote()
			elif self.black_cannot_avoid_loss(turns_left - 1):
				self.undo_move(captured, piece, prev_r, prev_c)
				return True
			self.undo_move(captured, piece, prev_r, prev_c)
		return False

	def black_cannot_avoid_loss(self, turns_left):
		if turns_left <= 0:
			return False
		black_pieces = self.black
		for piece in black_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				(prev_r, prev_c) = (piece.row, piece.col)
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return False
				willPromote = piece.move_will_promote()
				self.move_to(piece, r, c)
				if willPromote:
					for pType in ['N', 'B', 'R']:
						piece.promote_to(pType)
						if not self.white_can_win(turns_left - 1):
							piece.undo_promote()
							self.undo_move(captured, piece, prev_r, prev_c)
							return False
						piece.undo_promote()
				elif not self.white_can_win(turns_left - 1):
					self.undo_move(captured, piece, prev_r, prev_c)
					return False
				self.undo_move(captured, piece, prev_r, prev_c)
		return True
games = int(input())
for g in range(games):
	board = Board()
	cols = 'ABCD'
	(w, b, m) = map(int, input().split(' '))
	for i in range(w):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('W', ptype, row, col)
	for i in range(b):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('B', ptype, row, col)
	print('YES' if board.white_can_win(m) else 'NO')


from string import ascii_uppercase
from collections import namedtuple, defaultdict
from itertools import chain, repeat
from enum import Enum
Piece = namedtuple('Piece', ['side', 'type', 'pos'])
Pos = namedtuple('Position', ['c', 'r'])
Path = namedtuple('Path', ['capture', 'path'])
(W, B) = 'WB'

def gen_move_table(n):
	promos = 'BNR'
	pieces = {(c, r): {s + t: Piece(s, t, (c, r)) for s in 'WB' for t in 'RBQNP'} for c in range(n) for r in range(n)}
	P = lambda s, t, p: pieces[p][s + t]

	def move_paths(c, r):
		orthogonals = (tuple(((c, r_) for r_ in range(r + 1, n))), tuple(((c, r_) for r_ in reversed(range(r)))), tuple(((c_, r) for c_ in range(c + 1, n))), tuple(((c_, r) for c_ in reversed(range(c)))))
		diagonals = (tuple(zip(range(c + 1, n), range(r + 1, n))), tuple(zip(range(c + 1, n), reversed(range(r)))), tuple(zip(reversed(range(c)), range(r + 1, n))), tuple(zip(reversed(range(c)), reversed(range(r)))))
		knights = [(c + 2, r + 1), (c + 2, r - 1), (c - 2, r + 1), (c - 2, r - 1), (c + 1, r + 2), (c - 1, r + 2), (c + 1, r - 2), (c - 1, r - 2)]
		knights = tuple((((c_, r_),) for (c_, r_) in knights if 0 <= c_ < n and 0 <= r_ < n))
		return {'R': orthogonals, 'B': diagonals, 'Q': orthogonals + diagonals, 'N': knights}
	all_paths = defaultdict(tuple)
	for p_from in ((c, r) for c in range(n) for r in range(n)):
		for (t, paths) in move_paths(*p_from).items():
			for s in 'WB':
				all_paths[Piece(s, t, p_from)] = tuple((Path(1, tuple((P(s, t, p_to) for p_to in path))) for path in paths))
	ct = {0: ((0, 0), (2, 1)), n - 1: ((2, n - 2), (0, n - 1))}
	ct.update({x: ((2, x - 1), (0, x), (2, x + 1)) for x in range(1, n - 1)})
	for c in range(n):
		for r in range(n - 2):
			all_paths[P(W, 'P', (c, r))] = tuple((Path(cap, (P(W, 'P', (to, r + 1)),)) for (cap, to) in ct[c]))
			all_paths[P(B, 'P', (c, n - 1 - r))] = tuple((Path(cap, (P(B, 'P', (to, n - 2 - r)),)) for (cap, to) in ct[c]))
		all_paths[P(W, 'P', (c, n - 2))] = tuple((Path(cap, (P(W, typ, (to, n - 1)),)) for (cap, to) in ct[c] for typ in promos))
		all_paths[P(B, 'P', (c, 1))] = tuple((Path(cap, (P(B, typ, (to, 0)),)) for (cap, to) in ct[c] for typ in promos))
	return all_paths
all_paths = gen_move_table(4)

def win_with_move(grid, pos_from, piece, turn, m):
	if m == 1:
		return False
	grid_ = grid.copy()
	grid_[piece.pos] = piece
	del grid_[pos_from]
	return not forced_win(grid_, 'B' if turn == 'W' else 'W', m - 1)

def forced_win(grid, turn, m):
	next_turn = 'B' if turn == 'W' else 'W'
	for piece in (p for p in grid.values() if p.side == turn):
		for (capture, path) in all_paths[piece]:
			for new_piece in path:
				other_p = grid.get(new_piece.pos, None)
				if other_p:
					if other_p.side != turn and capture:
						if other_p.type == 'Q':
							return True
						if m > 1:
							grid_ = grid.copy()
							grid_[new_piece.pos] = new_piece
							del grid_[piece.pos]
							if not forced_win(grid_, next_turn, m - 1):
								return True
					break
				elif m > 1 and capture != 2:
					grid_ = grid.copy()
					grid_[new_piece.pos] = new_piece
					del grid_[piece.pos]
					if not forced_win(grid_, next_turn, m - 1):
						return True
	return False
trans = dict(zip(ascii_uppercase, range(8)))
trans_r = dict(zip(range(8), ascii_uppercase))

def translate(c, r):
	return (int(trans[c]), int(r) - 1)
for _ in range(int(input().strip())):
	(w, b, m) = [int(x) for x in input().strip().split(' ')]
	white = [Piece('W', t, translate(c, r)) for (t, c, r) in (input().strip().split(' ') for _ in range(w))]
	black = [Piece('B', t, translate(c, r)) for (t, c, r) in (input().strip().split(' ') for _ in range(b))]
	grid = {p.pos: p for p in white + black}
	if m % 2 == 0:
		m -= 1
	print('YES' if forced_win(grid, 'W', m) else 'NO')


class Piece(object):

	def __init__(self, color, ptype, row, col):
		self.color = color
		self.ptype = ptype
		self.row = row
		self.col = col
		self.captured = False

	def valid_moves(self, board):
		pass

	def move_will_promote(self):
		return False

	def can_move_to(self, r, c):
		if r == self.row and c == self.col:
			return False
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if board.mat[r][c] is None:
			return True
		elif board.mat[r][c].color != self.color:
			return True
		return False

	def _straight_moves(self, board):
		possible = []
		for row in range(1, 4):
			(r, c) = (self.row - row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for row in range(1, 4):
			(r, c) = (self.row + row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col - col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col + col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

	def _diagonal_moves(self, board):
		possible = []
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

class Rook(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board)
		return possible

class Queen(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board) + self._diagonal_moves(board)
		return possible

class Knight(Piece):

	def valid_moves(self, board):
		(r, c) = (self.row, self.col)
		possible = [(r - 2, c - 1), (r - 2, c + 1), (r - 1, c + 2), (r - 1, c - 2), (r + 1, c + 2), (r + 1, c - 2), (r + 2, c - 1), (r + 2, c + 1)]
		return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]

class Bishop(Piece):

	def valid_moves(self, board):
		possible = self._diagonal_moves(board)
		return possible

class Pawn(Piece):

	def can_move_to(self, r, c):
		if self.ptype == 'P':
			if r == self.row and c == self.col:
				return False
			if r < 0 or r > 3:
				return False
			if c < 0 or c > 3:
				return False
			if self.color == 'W' and r < self.row:
				return False
			elif self.color == 'B' and self.row < r:
				return False
			if c == self.col:
				if board.mat[r][c] is None:
					return True
			elif board.mat[r][c] is not None and board.mat[r][c].color != self.color:
				return True
			return False
		else:
			return Piece.can_move_to(self, r, c)

	def move_will_promote(self):
		if self.ptype == 'P':
			if self.color == 'W' and self.row == 2:
				return True
			elif self.color == 'B' and self.row == 1:
				return True
		return False

	def valid_moves(self, board):
		if self.ptype == 'P':
			(r, c) = (self.row, self.col)
			possible = [(r - 1, c), (r - 1, c - 1), (r - 1, c + 1), (r + 1, c), (r + 1, c - 1), (r + 1, c + 1)]
			return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]
		elif self.ptype == 'N':
			return Knight.valid_moves(self, board)
		elif self.ptype == 'R':
			return Rook.valid_moves(self, board)
		elif self.ptype == 'B':
			return Bishop.valid_moves(self, board)

	def promote_to(self, pType):
		self.ptype = pType

	def undo_promote(self):
		self.ptype = 'P'

class Board(object):

	def __init__(self):
		self.mat = [[None for i in range(4)] for u in range(4)]
		self.white = []
		self.black = []

	def add_piece(self, color, ptype, row, col):
		if ptype == 'Q':
			piece = Queen(color, ptype, row, col)
		elif ptype == 'N':
			piece = Knight(color, ptype, row, col)
		elif ptype == 'B':
			piece = Bishop(color, ptype, row, col)
		elif ptype == 'R':
			piece = Rook(color, ptype, row, col)
		elif ptype == 'P':
			piece = Pawn(color, ptype, row, col)
		else:
			raise Exception('Invalid type')
		self.mat[row][col] = piece
		if color == 'W':
			self.white.append(piece)
		elif color == 'B':
			self.black.append(piece)

	def move_to(self, piece, row, col):
		self.mat[piece.row][piece.col] = None
		if self.mat[row][col] is not None:
			captured = self.mat[row][col]
			captured.captured = True
		self.mat[row][col] = piece
		piece.row = row
		piece.col = col

	def undo_move(self, captured, piece, prev_r, prev_c):
		self.move_to(piece, prev_r, prev_c)
		if captured:
			(cr, cc) = (captured.row, captured.col)
			self.mat[cr][cc] = captured
			captured.captured = False

	def is_open(self, r, c):
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if self.mat[r][c] is None:
			return True
		return False

	def white_can_win(self, turns_left):
		if turns_left <= 0:
			return False
		white_pieces = self.white
		q = []
		for piece in white_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return True
				q.insert(0, (piece, r, c))
		for (piece, r, c) in q:
			(prev_r, prev_c) = (piece.row, piece.col)
			captured = self.mat[r][c]
			willPromote = piece.move_will_promote()
			self.move_to(piece, r, c)
			if willPromote:
				for pType in ['N', 'B', 'R']:
					piece.promote_to(pType)
					if self.black_cannot_avoid_loss(turns_left - 1):
						piece.undo_promote()
						self.undo_move(captured, piece, prev_r, prev_c)
						return True
					piece.undo_promote()
			elif self.black_cannot_avoid_loss(turns_left - 1):
				self.undo_move(captured, piece, prev_r, prev_c)
				return True
			self.undo_move(captured, piece, prev_r, prev_c)
		return False

	def black_cannot_avoid_loss(self, turns_left):
		if turns_left <= 0:
			return False
		black_pieces = self.black
		for piece in black_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				(prev_r, prev_c) = (piece.row, piece.col)
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return False
				willPromote = piece.move_will_promote()
				self.move_to(piece, r, c)
				if willPromote:
					for pType in ['N', 'B', 'R']:
						piece.promote_to(pType)
						if not self.white_can_win(turns_left - 1):
							piece.undo_promote()
							self.undo_move(captured, piece, prev_r, prev_c)
							return False
						piece.undo_promote()
				elif not self.white_can_win(turns_left - 1):
					self.undo_move(captured, piece, prev_r, prev_c)
					return False
				self.undo_move(captured, piece, prev_r, prev_c)
		return True
games = int(input())
for g in range(games):
	board = Board()
	cols = 'ABCD'
	(w, b, m) = map(int, input().split(' '))
	for i in range(w):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('W', ptype, row, col)
	for i in range(b):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('B', ptype, row, col)
	print('YES' if board.white_can_win(m) else 'NO')


class Piece(object):

	def __init__(self, color, ptype, row, col):
		self.color = color
		self.ptype = ptype
		self.row = row
		self.col = col
		self.captured = False

	def valid_moves(self, board):
		pass

	def move_will_promote(self):
		return False

	def can_move_to(self, r, c):
		if r == self.row and c == self.col:
			return False
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if board.mat[r][c] is None:
			return True
		elif board.mat[r][c].color != self.color:
			return True
		return False

	def _straight_moves(self, board):
		possible = []
		for row in range(1, 4):
			(r, c) = (self.row - row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for row in range(1, 4):
			(r, c) = (self.row + row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col - col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col + col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

	def _diagonal_moves(self, board):
		possible = []
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

class Rook(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board)
		return possible

class Queen(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board) + self._diagonal_moves(board)
		return possible

class Knight(Piece):

	def valid_moves(self, board):
		(r, c) = (self.row, self.col)
		possible = [(r - 2, c - 1), (r - 2, c + 1), (r - 1, c + 2), (r - 1, c - 2), (r + 1, c + 2), (r + 1, c - 2), (r + 2, c - 1), (r + 2, c + 1)]
		return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]

class Bishop(Piece):

	def valid_moves(self, board):
		possible = self._diagonal_moves(board)
		return possible

class Pawn(Piece):

	def can_move_to(self, r, c):
		if self.ptype == 'P':
			if r == self.row and c == self.col:
				return False
			if r < 0 or r > 3:
				return False
			if c < 0 or c > 3:
				return False
			if self.color == 'W' and r < self.row:
				return False
			elif self.color == 'B' and self.row < r:
				return False
			if c == self.col:
				if board.mat[r][c] is None:
					return True
			elif board.mat[r][c] is not None and board.mat[r][c].color != self.color:
				return True
			return False
		else:
			return Piece.can_move_to(self, r, c)

	def move_will_promote(self):
		if self.ptype == 'P':
			if self.color == 'W' and self.row == 2:
				return True
			elif self.color == 'B' and self.row == 1:
				return True
		return False

	def valid_moves(self, board):
		if self.ptype == 'P':
			(r, c) = (self.row, self.col)
			possible = [(r - 1, c), (r - 1, c - 1), (r - 1, c + 1), (r + 1, c), (r + 1, c - 1), (r + 1, c + 1)]
			return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]
		elif self.ptype == 'N':
			return Knight.valid_moves(self, board)
		elif self.ptype == 'R':
			return Rook.valid_moves(self, board)
		elif self.ptype == 'B':
			return Bishop.valid_moves(self, board)

	def promote_to(self, pType):
		self.ptype = pType

	def undo_promote(self):
		self.ptype = 'P'

class Board(object):

	def __init__(self):
		self.mat = [[None for i in range(4)] for u in range(4)]
		self.white = []
		self.black = []

	def add_piece(self, color, ptype, row, col):
		if ptype == 'Q':
			piece = Queen(color, ptype, row, col)
		elif ptype == 'N':
			piece = Knight(color, ptype, row, col)
		elif ptype == 'B':
			piece = Bishop(color, ptype, row, col)
		elif ptype == 'R':
			piece = Rook(color, ptype, row, col)
		elif ptype == 'P':
			piece = Pawn(color, ptype, row, col)
		else:
			raise Exception('Invalid type')
		self.mat[row][col] = piece
		if color == 'W':
			self.white.append(piece)
		elif color == 'B':
			self.black.append(piece)

	def move_to(self, piece, row, col):
		self.mat[piece.row][piece.col] = None
		if self.mat[row][col] is not None:
			captured = self.mat[row][col]
			captured.captured = True
		self.mat[row][col] = piece
		piece.row = row
		piece.col = col

	def undo_move(self, captured, piece, prev_r, prev_c):
		self.move_to(piece, prev_r, prev_c)
		if captured:
			(cr, cc) = (captured.row, captured.col)
			self.mat[cr][cc] = captured
			captured.captured = False

	def is_open(self, r, c):
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if self.mat[r][c] is None:
			return True
		return False

	def white_can_win(self, turns_left):
		if turns_left <= 0:
			return False
		white_pieces = self.white
		q = []
		for piece in white_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return True
				q.insert(0, (piece, r, c))
		for (piece, r, c) in q:
			(prev_r, prev_c) = (piece.row, piece.col)
			captured = self.mat[r][c]
			willPromote = piece.move_will_promote()
			self.move_to(piece, r, c)
			if willPromote:
				for pType in ['N', 'B', 'R']:
					piece.promote_to(pType)
					if self.black_cannot_avoid_loss(turns_left - 1):
						piece.undo_promote()
						self.undo_move(captured, piece, prev_r, prev_c)
						return True
					piece.undo_promote()
			elif self.black_cannot_avoid_loss(turns_left - 1):
				self.undo_move(captured, piece, prev_r, prev_c)
				return True
			self.undo_move(captured, piece, prev_r, prev_c)
		return False

	def black_cannot_avoid_loss(self, turns_left):
		if turns_left <= 0:
			return False
		black_pieces = self.black
		for piece in black_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				(prev_r, prev_c) = (piece.row, piece.col)
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return False
				willPromote = piece.move_will_promote()
				self.move_to(piece, r, c)
				if willPromote:
					for pType in ['N', 'B', 'R']:
						piece.promote_to(pType)
						if not self.white_can_win(turns_left - 1):
							piece.undo_promote()
							self.undo_move(captured, piece, prev_r, prev_c)
							return False
						piece.undo_promote()
				elif not self.white_can_win(turns_left - 1):
					self.undo_move(captured, piece, prev_r, prev_c)
					return False
				self.undo_move(captured, piece, prev_r, prev_c)
		return True
games = int(input())
for g in range(games):
	board = Board()
	cols = 'ABCD'
	(w, b, m) = map(int, input().split(' '))
	for i in range(w):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('W', ptype, row, col)
	for i in range(b):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('B', ptype, row, col)
	print('YES' if board.white_can_win(m) else 'NO')


class Piece(object):

	def __init__(self, color, ptype, row, col):
		self.color = color
		self.ptype = ptype
		self.row = row
		self.col = col
		self.captured = False

	def valid_moves(self, board):
		pass

	def move_will_promote(self):
		return False

	def can_move_to(self, r, c):
		if r == self.row and c == self.col:
			return False
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if board.mat[r][c] is None:
			return True
		elif board.mat[r][c].color != self.color:
			return True
		return False

	def _straight_moves(self, board):
		possible = []
		for row in range(1, 4):
			(r, c) = (self.row - row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for row in range(1, 4):
			(r, c) = (self.row + row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col - col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col + col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

	def _diagonal_moves(self, board):
		possible = []
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

class Rook(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board)
		return possible

class Queen(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board) + self._diagonal_moves(board)
		return possible

class Knight(Piece):

	def valid_moves(self, board):
		(r, c) = (self.row, self.col)
		possible = [(r - 2, c - 1), (r - 2, c + 1), (r - 1, c + 2), (r - 1, c - 2), (r + 1, c + 2), (r + 1, c - 2), (r + 2, c - 1), (r + 2, c + 1)]
		return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]

class Bishop(Piece):

	def valid_moves(self, board):
		possible = self._diagonal_moves(board)
		return possible

class Pawn(Piece):

	def can_move_to(self, r, c):
		if self.ptype == 'P':
			if r == self.row and c == self.col:
				return False
			if r < 0 or r > 3:
				return False
			if c < 0 or c > 3:
				return False
			if self.color == 'W' and r < self.row:
				return False
			elif self.color == 'B' and self.row < r:
				return False
			if c == self.col:
				if board.mat[r][c] is None:
					return True
			elif board.mat[r][c] is not None and board.mat[r][c].color != self.color:
				return True
			return False
		else:
			return Piece.can_move_to(self, r, c)

	def move_will_promote(self):
		if self.ptype == 'P':
			if self.color == 'W' and self.row == 2:
				return True
			elif self.color == 'B' and self.row == 1:
				return True
		return False

	def valid_moves(self, board):
		if self.ptype == 'P':
			(r, c) = (self.row, self.col)
			possible = [(r - 1, c), (r - 1, c - 1), (r - 1, c + 1), (r + 1, c), (r + 1, c - 1), (r + 1, c + 1)]
			return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]
		elif self.ptype == 'N':
			return Knight.valid_moves(self, board)
		elif self.ptype == 'R':
			return Rook.valid_moves(self, board)
		elif self.ptype == 'B':
			return Bishop.valid_moves(self, board)

	def promote_to(self, pType):
		self.ptype = pType

	def undo_promote(self):
		self.ptype = 'P'

class Board(object):

	def __init__(self):
		self.mat = [[None for i in range(4)] for u in range(4)]
		self.white = []
		self.black = []

	def add_piece(self, color, ptype, row, col):
		if ptype == 'Q':
			piece = Queen(color, ptype, row, col)
		elif ptype == 'N':
			piece = Knight(color, ptype, row, col)
		elif ptype == 'B':
			piece = Bishop(color, ptype, row, col)
		elif ptype == 'R':
			piece = Rook(color, ptype, row, col)
		elif ptype == 'P':
			piece = Pawn(color, ptype, row, col)
		else:
			raise Exception('Invalid type')
		self.mat[row][col] = piece
		if color == 'W':
			self.white.append(piece)
		elif color == 'B':
			self.black.append(piece)

	def move_to(self, piece, row, col):
		self.mat[piece.row][piece.col] = None
		if self.mat[row][col] is not None:
			captured = self.mat[row][col]
			captured.captured = True
		self.mat[row][col] = piece
		piece.row = row
		piece.col = col

	def undo_move(self, captured, piece, prev_r, prev_c):
		self.move_to(piece, prev_r, prev_c)
		if captured:
			(cr, cc) = (captured.row, captured.col)
			self.mat[cr][cc] = captured
			captured.captured = False

	def is_open(self, r, c):
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if self.mat[r][c] is None:
			return True
		return False

	def white_can_win(self, turns_left):
		if turns_left <= 0:
			return False
		white_pieces = self.white
		q = []
		for piece in white_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return True
				q.insert(0, (piece, r, c))
		for (piece, r, c) in q:
			(prev_r, prev_c) = (piece.row, piece.col)
			captured = self.mat[r][c]
			willPromote = piece.move_will_promote()
			self.move_to(piece, r, c)
			if willPromote:
				for pType in ['N', 'B', 'R']:
					piece.promote_to(pType)
					if self.black_cannot_avoid_loss(turns_left - 1):
						piece.undo_promote()
						self.undo_move(captured, piece, prev_r, prev_c)
						return True
					piece.undo_promote()
			elif self.black_cannot_avoid_loss(turns_left - 1):
				self.undo_move(captured, piece, prev_r, prev_c)
				return True
			self.undo_move(captured, piece, prev_r, prev_c)
		return False

	def black_cannot_avoid_loss(self, turns_left):
		if turns_left <= 0:
			return False
		black_pieces = self.black
		for piece in black_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				(prev_r, prev_c) = (piece.row, piece.col)
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return False
				willPromote = piece.move_will_promote()
				self.move_to(piece, r, c)
				if willPromote:
					for pType in ['N', 'B', 'R']:
						piece.promote_to(pType)
						if not self.white_can_win(turns_left - 1):
							piece.undo_promote()
							self.undo_move(captured, piece, prev_r, prev_c)
							return False
						piece.undo_promote()
				elif not self.white_can_win(turns_left - 1):
					self.undo_move(captured, piece, prev_r, prev_c)
					return False
				self.undo_move(captured, piece, prev_r, prev_c)
		return True
games = int(input())
for g in range(games):
	board = Board()
	cols = 'ABCD'
	(w, b, m) = map(int, input().split(' '))
	for i in range(w):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('W', ptype, row, col)
	for i in range(b):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('B', ptype, row, col)
	print('YES' if board.white_can_win(m) else 'NO')


class Piece(object):

	def __init__(self, color, ptype, row, col):
		self.color = color
		self.ptype = ptype
		self.row = row
		self.col = col
		self.captured = False

	def valid_moves(self, board):
		pass

	def move_will_promote(self):
		return False

	def can_move_to(self, r, c):
		if r == self.row and c == self.col:
			return False
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if board.mat[r][c] is None:
			return True
		elif board.mat[r][c].color != self.color:
			return True
		return False

	def _straight_moves(self, board):
		possible = []
		for row in range(1, 4):
			(r, c) = (self.row - row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for row in range(1, 4):
			(r, c) = (self.row + row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col - col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col + col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

	def _diagonal_moves(self, board):
		possible = []
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

class Rook(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board)
		return possible

class Queen(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board) + self._diagonal_moves(board)
		return possible

class Knight(Piece):

	def valid_moves(self, board):
		(r, c) = (self.row, self.col)
		possible = [(r - 2, c - 1), (r - 2, c + 1), (r - 1, c + 2), (r - 1, c - 2), (r + 1, c + 2), (r + 1, c - 2), (r + 2, c - 1), (r + 2, c + 1)]
		return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]

class Bishop(Piece):

	def valid_moves(self, board):
		possible = self._diagonal_moves(board)
		return possible

class Pawn(Piece):

	def can_move_to(self, r, c):
		if self.ptype == 'P':
			if r == self.row and c == self.col:
				return False
			if r < 0 or r > 3:
				return False
			if c < 0 or c > 3:
				return False
			if self.color == 'W' and r < self.row:
				return False
			elif self.color == 'B' and self.row < r:
				return False
			if c == self.col:
				if board.mat[r][c] is None:
					return True
			elif board.mat[r][c] is not None and board.mat[r][c].color != self.color:
				return True
			return False
		else:
			return Piece.can_move_to(self, r, c)

	def move_will_promote(self):
		if self.ptype == 'P':
			if self.color == 'W' and self.row == 2:
				return True
			elif self.color == 'B' and self.row == 1:
				return True
		return False

	def valid_moves(self, board):
		if self.ptype == 'P':
			(r, c) = (self.row, self.col)
			possible = [(r - 1, c), (r - 1, c - 1), (r - 1, c + 1), (r + 1, c), (r + 1, c - 1), (r + 1, c + 1)]
			return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]
		elif self.ptype == 'N':
			return Knight.valid_moves(self, board)
		elif self.ptype == 'R':
			return Rook.valid_moves(self, board)
		elif self.ptype == 'B':
			return Bishop.valid_moves(self, board)

	def promote_to(self, pType):
		self.ptype = pType

	def undo_promote(self):
		self.ptype = 'P'

class Board(object):

	def __init__(self):
		self.mat = [[None for i in range(4)] for u in range(4)]
		self.white = []
		self.black = []

	def add_piece(self, color, ptype, row, col):
		if ptype == 'Q':
			piece = Queen(color, ptype, row, col)
		elif ptype == 'N':
			piece = Knight(color, ptype, row, col)
		elif ptype == 'B':
			piece = Bishop(color, ptype, row, col)
		elif ptype == 'R':
			piece = Rook(color, ptype, row, col)
		elif ptype == 'P':
			piece = Pawn(color, ptype, row, col)
		else:
			raise Exception('Invalid type')
		self.mat[row][col] = piece
		if color == 'W':
			self.white.append(piece)
		elif color == 'B':
			self.black.append(piece)

	def move_to(self, piece, row, col):
		self.mat[piece.row][piece.col] = None
		if self.mat[row][col] is not None:
			captured = self.mat[row][col]
			captured.captured = True
		self.mat[row][col] = piece
		piece.row = row
		piece.col = col

	def undo_move(self, captured, piece, prev_r, prev_c):
		self.move_to(piece, prev_r, prev_c)
		if captured:
			(cr, cc) = (captured.row, captured.col)
			self.mat[cr][cc] = captured
			captured.captured = False

	def is_open(self, r, c):
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if self.mat[r][c] is None:
			return True
		return False

	def white_can_win(self, turns_left):
		if turns_left <= 0:
			return False
		white_pieces = self.white
		q = []
		for piece in white_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return True
				q.insert(0, (piece, r, c))
		for (piece, r, c) in q:
			(prev_r, prev_c) = (piece.row, piece.col)
			captured = self.mat[r][c]
			willPromote = piece.move_will_promote()
			self.move_to(piece, r, c)
			if willPromote:
				for pType in ['N', 'B', 'R']:
					piece.promote_to(pType)
					if self.black_cannot_avoid_loss(turns_left - 1):
						piece.undo_promote()
						self.undo_move(captured, piece, prev_r, prev_c)
						return True
					piece.undo_promote()
			elif self.black_cannot_avoid_loss(turns_left - 1):
				self.undo_move(captured, piece, prev_r, prev_c)
				return True
			self.undo_move(captured, piece, prev_r, prev_c)
		return False

	def black_cannot_avoid_loss(self, turns_left):
		if turns_left <= 0:
			return False
		black_pieces = self.black
		for piece in black_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				(prev_r, prev_c) = (piece.row, piece.col)
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return False
				willPromote = piece.move_will_promote()
				self.move_to(piece, r, c)
				if willPromote:
					for pType in ['N', 'B', 'R']:
						piece.promote_to(pType)
						if not self.white_can_win(turns_left - 1):
							piece.undo_promote()
							self.undo_move(captured, piece, prev_r, prev_c)
							return False
						piece.undo_promote()
				elif not self.white_can_win(turns_left - 1):
					self.undo_move(captured, piece, prev_r, prev_c)
					return False
				self.undo_move(captured, piece, prev_r, prev_c)
		return True
games = int(input())
for g in range(games):
	board = Board()
	cols = 'ABCD'
	(w, b, m) = map(int, input().split(' '))
	for i in range(w):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('W', ptype, row, col)
	for i in range(b):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('B', ptype, row, col)
	print('YES' if board.white_can_win(m) else 'NO')


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


class Piece(object):

	def __init__(self, color, ptype, row, col):
		self.color = color
		self.ptype = ptype
		self.row = row
		self.col = col
		self.captured = False

	def valid_moves(self, board):
		pass

	def move_will_promote(self):
		return False

	def can_move_to(self, r, c):
		if r == self.row and c == self.col:
			return False
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if board.mat[r][c] is None:
			return True
		elif board.mat[r][c].color != self.color:
			return True
		return False

	def _straight_moves(self, board):
		possible = []
		for row in range(1, 4):
			(r, c) = (self.row - row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for row in range(1, 4):
			(r, c) = (self.row + row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col - col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col + col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

	def _diagonal_moves(self, board):
		possible = []
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

class Rook(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board)
		return possible

class Queen(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board) + self._diagonal_moves(board)
		return possible

class Knight(Piece):

	def valid_moves(self, board):
		(r, c) = (self.row, self.col)
		possible = [(r - 2, c - 1), (r - 2, c + 1), (r - 1, c + 2), (r - 1, c - 2), (r + 1, c + 2), (r + 1, c - 2), (r + 2, c - 1), (r + 2, c + 1)]
		return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]

class Bishop(Piece):

	def valid_moves(self, board):
		possible = self._diagonal_moves(board)
		return possible

class Pawn(Piece):

	def can_move_to(self, r, c):
		if self.ptype == 'P':
			if r == self.row and c == self.col:
				return False
			if r < 0 or r > 3:
				return False
			if c < 0 or c > 3:
				return False
			if self.color == 'W' and r < self.row:
				return False
			elif self.color == 'B' and self.row < r:
				return False
			if c == self.col:
				if board.mat[r][c] is None:
					return True
			elif board.mat[r][c] is not None and board.mat[r][c].color != self.color:
				return True
			return False
		else:
			return Piece.can_move_to(self, r, c)

	def move_will_promote(self):
		if self.ptype == 'P':
			if self.color == 'W' and self.row == 2:
				return True
			elif self.color == 'B' and self.row == 1:
				return True
		return False

	def valid_moves(self, board):
		if self.ptype == 'P':
			(r, c) = (self.row, self.col)
			possible = [(r - 1, c), (r - 1, c - 1), (r - 1, c + 1), (r + 1, c), (r + 1, c - 1), (r + 1, c + 1)]
			return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]
		elif self.ptype == 'N':
			return Knight.valid_moves(self, board)
		elif self.ptype == 'R':
			return Rook.valid_moves(self, board)
		elif self.ptype == 'B':
			return Bishop.valid_moves(self, board)

	def promote_to(self, pType):
		self.ptype = pType

	def undo_promote(self):
		self.ptype = 'P'

class Board(object):

	def __init__(self):
		self.mat = [[None for i in range(4)] for u in range(4)]
		self.white = []
		self.black = []

	def add_piece(self, color, ptype, row, col):
		if ptype == 'Q':
			piece = Queen(color, ptype, row, col)
		elif ptype == 'N':
			piece = Knight(color, ptype, row, col)
		elif ptype == 'B':
			piece = Bishop(color, ptype, row, col)
		elif ptype == 'R':
			piece = Rook(color, ptype, row, col)
		elif ptype == 'P':
			piece = Pawn(color, ptype, row, col)
		else:
			raise Exception('Invalid type')
		self.mat[row][col] = piece
		if color == 'W':
			self.white.append(piece)
		elif color == 'B':
			self.black.append(piece)

	def move_to(self, piece, row, col):
		self.mat[piece.row][piece.col] = None
		if self.mat[row][col] is not None:
			captured = self.mat[row][col]
			captured.captured = True
		self.mat[row][col] = piece
		piece.row = row
		piece.col = col

	def undo_move(self, captured, piece, prev_r, prev_c):
		self.move_to(piece, prev_r, prev_c)
		if captured:
			(cr, cc) = (captured.row, captured.col)
			self.mat[cr][cc] = captured
			captured.captured = False

	def is_open(self, r, c):
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if self.mat[r][c] is None:
			return True
		return False

	def white_can_win(self, turns_left):
		if turns_left <= 0:
			return False
		white_pieces = self.white
		q = []
		for piece in white_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return True
				q.insert(0, (piece, r, c))
		for (piece, r, c) in q:
			(prev_r, prev_c) = (piece.row, piece.col)
			captured = self.mat[r][c]
			willPromote = piece.move_will_promote()
			self.move_to(piece, r, c)
			if willPromote:
				for pType in ['N', 'B', 'R']:
					piece.promote_to(pType)
					if self.black_cannot_avoid_loss(turns_left - 1):
						piece.undo_promote()
						self.undo_move(captured, piece, prev_r, prev_c)
						return True
					piece.undo_promote()
			elif self.black_cannot_avoid_loss(turns_left - 1):
				self.undo_move(captured, piece, prev_r, prev_c)
				return True
			self.undo_move(captured, piece, prev_r, prev_c)
		return False

	def black_cannot_avoid_loss(self, turns_left):
		if turns_left <= 0:
			return False
		black_pieces = self.black
		for piece in black_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				(prev_r, prev_c) = (piece.row, piece.col)
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return False
				willPromote = piece.move_will_promote()
				self.move_to(piece, r, c)
				if willPromote:
					for pType in ['N', 'B', 'R']:
						piece.promote_to(pType)
						if not self.white_can_win(turns_left - 1):
							piece.undo_promote()
							self.undo_move(captured, piece, prev_r, prev_c)
							return False
						piece.undo_promote()
				elif not self.white_can_win(turns_left - 1):
					self.undo_move(captured, piece, prev_r, prev_c)
					return False
				self.undo_move(captured, piece, prev_r, prev_c)
		return True
games = int(input())
for g in range(games):
	board = Board()
	cols = 'ABCD'
	(w, b, m) = map(int, input().split(' '))
	for i in range(w):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('W', ptype, row, col)
	for i in range(b):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('B', ptype, row, col)
	print('YES' if board.white_can_win(m) else 'NO')


class Piece(object):

	def __init__(self, color, ptype, row, col):
		self.color = color
		self.ptype = ptype
		self.row = row
		self.col = col
		self.captured = False

	def valid_moves(self, board):
		pass

	def move_will_promote(self):
		return False

	def can_move_to(self, r, c):
		if r == self.row and c == self.col:
			return False
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if board.mat[r][c] is None:
			return True
		elif board.mat[r][c].color != self.color:
			return True
		return False

	def _straight_moves(self, board):
		possible = []
		for row in range(1, 4):
			(r, c) = (self.row - row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for row in range(1, 4):
			(r, c) = (self.row + row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col - col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col + col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

	def _diagonal_moves(self, board):
		possible = []
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

class Rook(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board)
		return possible

class Queen(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board) + self._diagonal_moves(board)
		return possible

class Knight(Piece):

	def valid_moves(self, board):
		(r, c) = (self.row, self.col)
		possible = [(r - 2, c - 1), (r - 2, c + 1), (r - 1, c + 2), (r - 1, c - 2), (r + 1, c + 2), (r + 1, c - 2), (r + 2, c - 1), (r + 2, c + 1)]
		return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]

class Bishop(Piece):

	def valid_moves(self, board):
		possible = self._diagonal_moves(board)
		return possible

class Pawn(Piece):

	def can_move_to(self, r, c):
		if self.ptype == 'P':
			if r == self.row and c == self.col:
				return False
			if r < 0 or r > 3:
				return False
			if c < 0 or c > 3:
				return False
			if self.color == 'W' and r < self.row:
				return False
			elif self.color == 'B' and self.row < r:
				return False
			if c == self.col:
				if board.mat[r][c] is None:
					return True
			elif board.mat[r][c] is not None and board.mat[r][c].color != self.color:
				return True
			return False
		else:
			return Piece.can_move_to(self, r, c)

	def move_will_promote(self):
		if self.ptype == 'P':
			if self.color == 'W' and self.row == 2:
				return True
			elif self.color == 'B' and self.row == 1:
				return True
		return False

	def valid_moves(self, board):
		if self.ptype == 'P':
			(r, c) = (self.row, self.col)
			possible = [(r - 1, c), (r - 1, c - 1), (r - 1, c + 1), (r + 1, c), (r + 1, c - 1), (r + 1, c + 1)]
			return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]
		elif self.ptype == 'N':
			return Knight.valid_moves(self, board)
		elif self.ptype == 'R':
			return Rook.valid_moves(self, board)
		elif self.ptype == 'B':
			return Bishop.valid_moves(self, board)

	def promote_to(self, pType):
		self.ptype = pType

	def undo_promote(self):
		self.ptype = 'P'

class Board(object):

	def __init__(self):
		self.mat = [[None for i in range(4)] for u in range(4)]
		self.white = []
		self.black = []

	def add_piece(self, color, ptype, row, col):
		if ptype == 'Q':
			piece = Queen(color, ptype, row, col)
		elif ptype == 'N':
			piece = Knight(color, ptype, row, col)
		elif ptype == 'B':
			piece = Bishop(color, ptype, row, col)
		elif ptype == 'R':
			piece = Rook(color, ptype, row, col)
		elif ptype == 'P':
			piece = Pawn(color, ptype, row, col)
		else:
			raise Exception('Invalid type')
		self.mat[row][col] = piece
		if color == 'W':
			self.white.append(piece)
		elif color == 'B':
			self.black.append(piece)

	def move_to(self, piece, row, col):
		self.mat[piece.row][piece.col] = None
		if self.mat[row][col] is not None:
			captured = self.mat[row][col]
			captured.captured = True
		self.mat[row][col] = piece
		piece.row = row
		piece.col = col

	def undo_move(self, captured, piece, prev_r, prev_c):
		self.move_to(piece, prev_r, prev_c)
		if captured:
			(cr, cc) = (captured.row, captured.col)
			self.mat[cr][cc] = captured
			captured.captured = False

	def is_open(self, r, c):
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if self.mat[r][c] is None:
			return True
		return False

	def white_can_win(self, turns_left):
		if turns_left <= 0:
			return False
		white_pieces = self.white
		q = []
		for piece in white_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return True
				q.insert(0, (piece, r, c))
		for (piece, r, c) in q:
			(prev_r, prev_c) = (piece.row, piece.col)
			captured = self.mat[r][c]
			willPromote = piece.move_will_promote()
			self.move_to(piece, r, c)
			if willPromote:
				for pType in ['N', 'B', 'R']:
					piece.promote_to(pType)
					if self.black_cannot_avoid_loss(turns_left - 1):
						piece.undo_promote()
						self.undo_move(captured, piece, prev_r, prev_c)
						return True
					piece.undo_promote()
			elif self.black_cannot_avoid_loss(turns_left - 1):
				self.undo_move(captured, piece, prev_r, prev_c)
				return True
			self.undo_move(captured, piece, prev_r, prev_c)
		return False

	def black_cannot_avoid_loss(self, turns_left):
		if turns_left <= 0:
			return False
		black_pieces = self.black
		for piece in black_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				(prev_r, prev_c) = (piece.row, piece.col)
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return False
				willPromote = piece.move_will_promote()
				self.move_to(piece, r, c)
				if willPromote:
					for pType in ['N', 'B', 'R']:
						piece.promote_to(pType)
						if not self.white_can_win(turns_left - 1):
							piece.undo_promote()
							self.undo_move(captured, piece, prev_r, prev_c)
							return False
						piece.undo_promote()
				elif not self.white_can_win(turns_left - 1):
					self.undo_move(captured, piece, prev_r, prev_c)
					return False
				self.undo_move(captured, piece, prev_r, prev_c)
		return True
games = int(input())
for g in range(games):
	board = Board()
	cols = 'ABCD'
	(w, b, m) = map(int, input().split(' '))
	for i in range(w):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('W', ptype, row, col)
	for i in range(b):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('B', ptype, row, col)
	print('YES' if board.white_can_win(m) else 'NO')


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


class Piece(object):

	def __init__(self, color, ptype, row, col):
		self.color = color
		self.ptype = ptype
		self.row = row
		self.col = col
		self.captured = False

	def valid_moves(self, board):
		pass

	def move_will_promote(self):
		return False

	def can_move_to(self, r, c):
		if r == self.row and c == self.col:
			return False
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if board.mat[r][c] is None:
			return True
		elif board.mat[r][c].color != self.color:
			return True
		return False

	def _straight_moves(self, board):
		possible = []
		for row in range(1, 4):
			(r, c) = (self.row - row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for row in range(1, 4):
			(r, c) = (self.row + row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col - col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col + col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

	def _diagonal_moves(self, board):
		possible = []
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

class Rook(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board)
		return possible

class Queen(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board) + self._diagonal_moves(board)
		return possible

class Knight(Piece):

	def valid_moves(self, board):
		(r, c) = (self.row, self.col)
		possible = [(r - 2, c - 1), (r - 2, c + 1), (r - 1, c + 2), (r - 1, c - 2), (r + 1, c + 2), (r + 1, c - 2), (r + 2, c - 1), (r + 2, c + 1)]
		return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]

class Bishop(Piece):

	def valid_moves(self, board):
		possible = self._diagonal_moves(board)
		return possible

class Pawn(Piece):

	def can_move_to(self, r, c):
		if self.ptype == 'P':
			if r == self.row and c == self.col:
				return False
			if r < 0 or r > 3:
				return False
			if c < 0 or c > 3:
				return False
			if self.color == 'W' and r < self.row:
				return False
			elif self.color == 'B' and self.row < r:
				return False
			if c == self.col:
				if board.mat[r][c] is None:
					return True
			elif board.mat[r][c] is not None and board.mat[r][c].color != self.color:
				return True
			return False
		else:
			return Piece.can_move_to(self, r, c)

	def move_will_promote(self):
		if self.ptype == 'P':
			if self.color == 'W' and self.row == 2:
				return True
			elif self.color == 'B' and self.row == 1:
				return True
		return False

	def valid_moves(self, board):
		if self.ptype == 'P':
			(r, c) = (self.row, self.col)
			possible = [(r - 1, c), (r - 1, c - 1), (r - 1, c + 1), (r + 1, c), (r + 1, c - 1), (r + 1, c + 1)]
			return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]
		elif self.ptype == 'N':
			return Knight.valid_moves(self, board)
		elif self.ptype == 'R':
			return Rook.valid_moves(self, board)
		elif self.ptype == 'B':
			return Bishop.valid_moves(self, board)

	def promote_to(self, pType):
		self.ptype = pType

	def undo_promote(self):
		self.ptype = 'P'

class Board(object):

	def __init__(self):
		self.mat = [[None for i in range(4)] for u in range(4)]
		self.white = []
		self.black = []

	def add_piece(self, color, ptype, row, col):
		if ptype == 'Q':
			piece = Queen(color, ptype, row, col)
		elif ptype == 'N':
			piece = Knight(color, ptype, row, col)
		elif ptype == 'B':
			piece = Bishop(color, ptype, row, col)
		elif ptype == 'R':
			piece = Rook(color, ptype, row, col)
		elif ptype == 'P':
			piece = Pawn(color, ptype, row, col)
		else:
			raise Exception('Invalid type')
		self.mat[row][col] = piece
		if color == 'W':
			self.white.append(piece)
		elif color == 'B':
			self.black.append(piece)

	def move_to(self, piece, row, col):
		self.mat[piece.row][piece.col] = None
		if self.mat[row][col] is not None:
			captured = self.mat[row][col]
			captured.captured = True
		self.mat[row][col] = piece
		piece.row = row
		piece.col = col

	def undo_move(self, captured, piece, prev_r, prev_c):
		self.move_to(piece, prev_r, prev_c)
		if captured:
			(cr, cc) = (captured.row, captured.col)
			self.mat[cr][cc] = captured
			captured.captured = False

	def is_open(self, r, c):
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if self.mat[r][c] is None:
			return True
		return False

	def white_can_win(self, turns_left):
		if turns_left <= 0:
			return False
		white_pieces = self.white
		q = []
		for piece in white_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return True
				q.insert(0, (piece, r, c))
		for (piece, r, c) in q:
			(prev_r, prev_c) = (piece.row, piece.col)
			captured = self.mat[r][c]
			willPromote = piece.move_will_promote()
			self.move_to(piece, r, c)
			if willPromote:
				for pType in ['N', 'B', 'R']:
					piece.promote_to(pType)
					if self.black_cannot_avoid_loss(turns_left - 1):
						piece.undo_promote()
						self.undo_move(captured, piece, prev_r, prev_c)
						return True
					piece.undo_promote()
			elif self.black_cannot_avoid_loss(turns_left - 1):
				self.undo_move(captured, piece, prev_r, prev_c)
				return True
			self.undo_move(captured, piece, prev_r, prev_c)
		return False

	def black_cannot_avoid_loss(self, turns_left):
		if turns_left <= 0:
			return False
		black_pieces = self.black
		for piece in black_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				(prev_r, prev_c) = (piece.row, piece.col)
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return False
				willPromote = piece.move_will_promote()
				self.move_to(piece, r, c)
				if willPromote:
					for pType in ['N', 'B', 'R']:
						piece.promote_to(pType)
						if not self.white_can_win(turns_left - 1):
							piece.undo_promote()
							self.undo_move(captured, piece, prev_r, prev_c)
							return False
						piece.undo_promote()
				elif not self.white_can_win(turns_left - 1):
					self.undo_move(captured, piece, prev_r, prev_c)
					return False
				self.undo_move(captured, piece, prev_r, prev_c)
		return True
games = int(input())
for g in range(games):
	board = Board()
	cols = 'ABCD'
	(w, b, m) = map(int, input().split(' '))
	for i in range(w):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('W', ptype, row, col)
	for i in range(b):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('B', ptype, row, col)
	print('YES' if board.white_can_win(m) else 'NO')


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


class Piece(object):

	def __init__(self, color, ptype, row, col):
		self.color = color
		self.ptype = ptype
		self.row = row
		self.col = col
		self.captured = False

	def valid_moves(self, board):
		pass

	def move_will_promote(self):
		return False

	def can_move_to(self, r, c):
		if r == self.row and c == self.col:
			return False
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if board.mat[r][c] is None:
			return True
		elif board.mat[r][c].color != self.color:
			return True
		return False

	def _straight_moves(self, board):
		possible = []
		for row in range(1, 4):
			(r, c) = (self.row - row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for row in range(1, 4):
			(r, c) = (self.row + row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col - col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col + col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

	def _diagonal_moves(self, board):
		possible = []
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

class Rook(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board)
		return possible

class Queen(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board) + self._diagonal_moves(board)
		return possible

class Knight(Piece):

	def valid_moves(self, board):
		(r, c) = (self.row, self.col)
		possible = [(r - 2, c - 1), (r - 2, c + 1), (r - 1, c + 2), (r - 1, c - 2), (r + 1, c + 2), (r + 1, c - 2), (r + 2, c - 1), (r + 2, c + 1)]
		return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]

class Bishop(Piece):

	def valid_moves(self, board):
		possible = self._diagonal_moves(board)
		return possible

class Pawn(Piece):

	def can_move_to(self, r, c):
		if self.ptype == 'P':
			if r == self.row and c == self.col:
				return False
			if r < 0 or r > 3:
				return False
			if c < 0 or c > 3:
				return False
			if self.color == 'W' and r < self.row:
				return False
			elif self.color == 'B' and self.row < r:
				return False
			if c == self.col:
				if board.mat[r][c] is None:
					return True
			elif board.mat[r][c] is not None and board.mat[r][c].color != self.color:
				return True
			return False
		else:
			return Piece.can_move_to(self, r, c)

	def move_will_promote(self):
		if self.ptype == 'P':
			if self.color == 'W' and self.row == 2:
				return True
			elif self.color == 'B' and self.row == 1:
				return True
		return False

	def valid_moves(self, board):
		if self.ptype == 'P':
			(r, c) = (self.row, self.col)
			possible = [(r - 1, c), (r - 1, c - 1), (r - 1, c + 1), (r + 1, c), (r + 1, c - 1), (r + 1, c + 1)]
			return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]
		elif self.ptype == 'N':
			return Knight.valid_moves(self, board)
		elif self.ptype == 'R':
			return Rook.valid_moves(self, board)
		elif self.ptype == 'B':
			return Bishop.valid_moves(self, board)

	def promote_to(self, pType):
		self.ptype = pType

	def undo_promote(self):
		self.ptype = 'P'

class Board(object):

	def __init__(self):
		self.mat = [[None for i in range(4)] for u in range(4)]
		self.white = []
		self.black = []

	def add_piece(self, color, ptype, row, col):
		if ptype == 'Q':
			piece = Queen(color, ptype, row, col)
		elif ptype == 'N':
			piece = Knight(color, ptype, row, col)
		elif ptype == 'B':
			piece = Bishop(color, ptype, row, col)
		elif ptype == 'R':
			piece = Rook(color, ptype, row, col)
		elif ptype == 'P':
			piece = Pawn(color, ptype, row, col)
		else:
			raise Exception('Invalid type')
		self.mat[row][col] = piece
		if color == 'W':
			self.white.append(piece)
		elif color == 'B':
			self.black.append(piece)

	def move_to(self, piece, row, col):
		self.mat[piece.row][piece.col] = None
		if self.mat[row][col] is not None:
			captured = self.mat[row][col]
			captured.captured = True
		self.mat[row][col] = piece
		piece.row = row
		piece.col = col

	def undo_move(self, captured, piece, prev_r, prev_c):
		self.move_to(piece, prev_r, prev_c)
		if captured:
			(cr, cc) = (captured.row, captured.col)
			self.mat[cr][cc] = captured
			captured.captured = False

	def is_open(self, r, c):
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if self.mat[r][c] is None:
			return True
		return False

	def white_can_win(self, turns_left):
		if turns_left <= 0:
			return False
		white_pieces = self.white
		q = []
		for piece in white_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return True
				q.insert(0, (piece, r, c))
		for (piece, r, c) in q:
			(prev_r, prev_c) = (piece.row, piece.col)
			captured = self.mat[r][c]
			willPromote = piece.move_will_promote()
			self.move_to(piece, r, c)
			if willPromote:
				for pType in ['N', 'B', 'R']:
					piece.promote_to(pType)
					if self.black_cannot_avoid_loss(turns_left - 1):
						piece.undo_promote()
						self.undo_move(captured, piece, prev_r, prev_c)
						return True
					piece.undo_promote()
			elif self.black_cannot_avoid_loss(turns_left - 1):
				self.undo_move(captured, piece, prev_r, prev_c)
				return True
			self.undo_move(captured, piece, prev_r, prev_c)
		return False

	def black_cannot_avoid_loss(self, turns_left):
		if turns_left <= 0:
			return False
		black_pieces = self.black
		for piece in black_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				(prev_r, prev_c) = (piece.row, piece.col)
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return False
				willPromote = piece.move_will_promote()
				self.move_to(piece, r, c)
				if willPromote:
					for pType in ['N', 'B', 'R']:
						piece.promote_to(pType)
						if not self.white_can_win(turns_left - 1):
							piece.undo_promote()
							self.undo_move(captured, piece, prev_r, prev_c)
							return False
						piece.undo_promote()
				elif not self.white_can_win(turns_left - 1):
					self.undo_move(captured, piece, prev_r, prev_c)
					return False
				self.undo_move(captured, piece, prev_r, prev_c)
		return True
games = int(input())
for g in range(games):
	board = Board()
	cols = 'ABCD'
	(w, b, m) = map(int, input().split(' '))
	for i in range(w):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('W', ptype, row, col)
	for i in range(b):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('B', ptype, row, col)
	print('YES' if board.white_can_win(m) else 'NO')


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


class Piece(object):

	def __init__(self, color, ptype, row, col):
		self.color = color
		self.ptype = ptype
		self.row = row
		self.col = col
		self.captured = False

	def valid_moves(self, board):
		pass

	def move_will_promote(self):
		return False

	def can_move_to(self, r, c):
		if r == self.row and c == self.col:
			return False
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if board.mat[r][c] is None:
			return True
		elif board.mat[r][c].color != self.color:
			return True
		return False

	def _straight_moves(self, board):
		possible = []
		for row in range(1, 4):
			(r, c) = (self.row - row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for row in range(1, 4):
			(r, c) = (self.row + row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col - col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col + col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

	def _diagonal_moves(self, board):
		possible = []
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

class Rook(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board)
		return possible

class Queen(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board) + self._diagonal_moves(board)
		return possible

class Knight(Piece):

	def valid_moves(self, board):
		(r, c) = (self.row, self.col)
		possible = [(r - 2, c - 1), (r - 2, c + 1), (r - 1, c + 2), (r - 1, c - 2), (r + 1, c + 2), (r + 1, c - 2), (r + 2, c - 1), (r + 2, c + 1)]
		return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]

class Bishop(Piece):

	def valid_moves(self, board):
		possible = self._diagonal_moves(board)
		return possible

class Pawn(Piece):

	def can_move_to(self, r, c):
		if self.ptype == 'P':
			if r == self.row and c == self.col:
				return False
			if r < 0 or r > 3:
				return False
			if c < 0 or c > 3:
				return False
			if self.color == 'W' and r < self.row:
				return False
			elif self.color == 'B' and self.row < r:
				return False
			if c == self.col:
				if board.mat[r][c] is None:
					return True
			elif board.mat[r][c] is not None and board.mat[r][c].color != self.color:
				return True
			return False
		else:
			return Piece.can_move_to(self, r, c)

	def move_will_promote(self):
		if self.ptype == 'P':
			if self.color == 'W' and self.row == 2:
				return True
			elif self.color == 'B' and self.row == 1:
				return True
		return False

	def valid_moves(self, board):
		if self.ptype == 'P':
			(r, c) = (self.row, self.col)
			possible = [(r - 1, c), (r - 1, c - 1), (r - 1, c + 1), (r + 1, c), (r + 1, c - 1), (r + 1, c + 1)]
			return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]
		elif self.ptype == 'N':
			return Knight.valid_moves(self, board)
		elif self.ptype == 'R':
			return Rook.valid_moves(self, board)
		elif self.ptype == 'B':
			return Bishop.valid_moves(self, board)

	def promote_to(self, pType):
		self.ptype = pType

	def undo_promote(self):
		self.ptype = 'P'

class Board(object):

	def __init__(self):
		self.mat = [[None for i in range(4)] for u in range(4)]
		self.white = []
		self.black = []

	def add_piece(self, color, ptype, row, col):
		if ptype == 'Q':
			piece = Queen(color, ptype, row, col)
		elif ptype == 'N':
			piece = Knight(color, ptype, row, col)
		elif ptype == 'B':
			piece = Bishop(color, ptype, row, col)
		elif ptype == 'R':
			piece = Rook(color, ptype, row, col)
		elif ptype == 'P':
			piece = Pawn(color, ptype, row, col)
		else:
			raise Exception('Invalid type')
		self.mat[row][col] = piece
		if color == 'W':
			self.white.append(piece)
		elif color == 'B':
			self.black.append(piece)

	def move_to(self, piece, row, col):
		self.mat[piece.row][piece.col] = None
		if self.mat[row][col] is not None:
			captured = self.mat[row][col]
			captured.captured = True
		self.mat[row][col] = piece
		piece.row = row
		piece.col = col

	def undo_move(self, captured, piece, prev_r, prev_c):
		self.move_to(piece, prev_r, prev_c)
		if captured:
			(cr, cc) = (captured.row, captured.col)
			self.mat[cr][cc] = captured
			captured.captured = False

	def is_open(self, r, c):
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if self.mat[r][c] is None:
			return True
		return False

	def white_can_win(self, turns_left):
		if turns_left <= 0:
			return False
		white_pieces = self.white
		q = []
		for piece in white_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return True
				q.insert(0, (piece, r, c))
		for (piece, r, c) in q:
			(prev_r, prev_c) = (piece.row, piece.col)
			captured = self.mat[r][c]
			willPromote = piece.move_will_promote()
			self.move_to(piece, r, c)
			if willPromote:
				for pType in ['N', 'B', 'R']:
					piece.promote_to(pType)
					if self.black_cannot_avoid_loss(turns_left - 1):
						piece.undo_promote()
						self.undo_move(captured, piece, prev_r, prev_c)
						return True
					piece.undo_promote()
			elif self.black_cannot_avoid_loss(turns_left - 1):
				self.undo_move(captured, piece, prev_r, prev_c)
				return True
			self.undo_move(captured, piece, prev_r, prev_c)
		return False

	def black_cannot_avoid_loss(self, turns_left):
		if turns_left <= 0:
			return False
		black_pieces = self.black
		for piece in black_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				(prev_r, prev_c) = (piece.row, piece.col)
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return False
				willPromote = piece.move_will_promote()
				self.move_to(piece, r, c)
				if willPromote:
					for pType in ['N', 'B', 'R']:
						piece.promote_to(pType)
						if not self.white_can_win(turns_left - 1):
							piece.undo_promote()
							self.undo_move(captured, piece, prev_r, prev_c)
							return False
						piece.undo_promote()
				elif not self.white_can_win(turns_left - 1):
					self.undo_move(captured, piece, prev_r, prev_c)
					return False
				self.undo_move(captured, piece, prev_r, prev_c)
		return True
games = int(input())
for g in range(games):
	board = Board()
	cols = 'ABCD'
	(w, b, m) = map(int, input().split(' '))
	for i in range(w):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('W', ptype, row, col)
	for i in range(b):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('B', ptype, row, col)
	print('YES' if board.white_can_win(m) else 'NO')


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


class Piece(object):

	def __init__(self, color, ptype, row, col):
		self.color = color
		self.ptype = ptype
		self.row = row
		self.col = col
		self.captured = False

	def valid_moves(self, board):
		pass

	def move_will_promote(self):
		return False

	def can_move_to(self, r, c):
		if r == self.row and c == self.col:
			return False
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if board.mat[r][c] is None:
			return True
		elif board.mat[r][c].color != self.color:
			return True
		return False

	def _straight_moves(self, board):
		possible = []
		for row in range(1, 4):
			(r, c) = (self.row - row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for row in range(1, 4):
			(r, c) = (self.row + row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col - col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col + col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

	def _diagonal_moves(self, board):
		possible = []
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

class Rook(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board)
		return possible

class Queen(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board) + self._diagonal_moves(board)
		return possible

class Knight(Piece):

	def valid_moves(self, board):
		(r, c) = (self.row, self.col)
		possible = [(r - 2, c - 1), (r - 2, c + 1), (r - 1, c + 2), (r - 1, c - 2), (r + 1, c + 2), (r + 1, c - 2), (r + 2, c - 1), (r + 2, c + 1)]
		return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]

class Bishop(Piece):

	def valid_moves(self, board):
		possible = self._diagonal_moves(board)
		return possible

class Pawn(Piece):

	def can_move_to(self, r, c):
		if self.ptype == 'P':
			if r == self.row and c == self.col:
				return False
			if r < 0 or r > 3:
				return False
			if c < 0 or c > 3:
				return False
			if self.color == 'W' and r < self.row:
				return False
			elif self.color == 'B' and self.row < r:
				return False
			if c == self.col:
				if board.mat[r][c] is None:
					return True
			elif board.mat[r][c] is not None and board.mat[r][c].color != self.color:
				return True
			return False
		else:
			return Piece.can_move_to(self, r, c)

	def move_will_promote(self):
		if self.ptype == 'P':
			if self.color == 'W' and self.row == 2:
				return True
			elif self.color == 'B' and self.row == 1:
				return True
		return False

	def valid_moves(self, board):
		if self.ptype == 'P':
			(r, c) = (self.row, self.col)
			possible = [(r - 1, c), (r - 1, c - 1), (r - 1, c + 1), (r + 1, c), (r + 1, c - 1), (r + 1, c + 1)]
			return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]
		elif self.ptype == 'N':
			return Knight.valid_moves(self, board)
		elif self.ptype == 'R':
			return Rook.valid_moves(self, board)
		elif self.ptype == 'B':
			return Bishop.valid_moves(self, board)

	def promote_to(self, pType):
		self.ptype = pType

	def undo_promote(self):
		self.ptype = 'P'

class Board(object):

	def __init__(self):
		self.mat = [[None for i in range(4)] for u in range(4)]
		self.white = []
		self.black = []

	def add_piece(self, color, ptype, row, col):
		if ptype == 'Q':
			piece = Queen(color, ptype, row, col)
		elif ptype == 'N':
			piece = Knight(color, ptype, row, col)
		elif ptype == 'B':
			piece = Bishop(color, ptype, row, col)
		elif ptype == 'R':
			piece = Rook(color, ptype, row, col)
		elif ptype == 'P':
			piece = Pawn(color, ptype, row, col)
		else:
			raise Exception('Invalid type')
		self.mat[row][col] = piece
		if color == 'W':
			self.white.append(piece)
		elif color == 'B':
			self.black.append(piece)

	def move_to(self, piece, row, col):
		self.mat[piece.row][piece.col] = None
		if self.mat[row][col] is not None:
			captured = self.mat[row][col]
			captured.captured = True
		self.mat[row][col] = piece
		piece.row = row
		piece.col = col

	def undo_move(self, captured, piece, prev_r, prev_c):
		self.move_to(piece, prev_r, prev_c)
		if captured:
			(cr, cc) = (captured.row, captured.col)
			self.mat[cr][cc] = captured
			captured.captured = False

	def is_open(self, r, c):
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if self.mat[r][c] is None:
			return True
		return False

	def white_can_win(self, turns_left):
		if turns_left <= 0:
			return False
		white_pieces = self.white
		q = []
		for piece in white_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return True
				q.insert(0, (piece, r, c))
		for (piece, r, c) in q:
			(prev_r, prev_c) = (piece.row, piece.col)
			captured = self.mat[r][c]
			willPromote = piece.move_will_promote()
			self.move_to(piece, r, c)
			if willPromote:
				for pType in ['N', 'B', 'R']:
					piece.promote_to(pType)
					if self.black_cannot_avoid_loss(turns_left - 1):
						piece.undo_promote()
						self.undo_move(captured, piece, prev_r, prev_c)
						return True
					piece.undo_promote()
			elif self.black_cannot_avoid_loss(turns_left - 1):
				self.undo_move(captured, piece, prev_r, prev_c)
				return True
			self.undo_move(captured, piece, prev_r, prev_c)
		return False

	def black_cannot_avoid_loss(self, turns_left):
		if turns_left <= 0:
			return False
		black_pieces = self.black
		for piece in black_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				(prev_r, prev_c) = (piece.row, piece.col)
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return False
				willPromote = piece.move_will_promote()
				self.move_to(piece, r, c)
				if willPromote:
					for pType in ['N', 'B', 'R']:
						piece.promote_to(pType)
						if not self.white_can_win(turns_left - 1):
							piece.undo_promote()
							self.undo_move(captured, piece, prev_r, prev_c)
							return False
						piece.undo_promote()
				elif not self.white_can_win(turns_left - 1):
					self.undo_move(captured, piece, prev_r, prev_c)
					return False
				self.undo_move(captured, piece, prev_r, prev_c)
		return True
games = int(input())
for g in range(games):
	board = Board()
	cols = 'ABCD'
	(w, b, m) = map(int, input().split(' '))
	for i in range(w):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('W', ptype, row, col)
	for i in range(b):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('B', ptype, row, col)
	print('YES' if board.white_can_win(m) else 'NO')


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

class Piece(object):

	def __init__(self, color, ptype, row, col):
		self.color = color
		self.ptype = ptype
		self.row = row
		self.col = col
		self.captured = False

	def valid_moves(self, board):
		pass

	def move_will_promote(self):
		return False

	def can_move_to(self, r, c):
		if r == self.row and c == self.col:
			return False
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if board.mat[r][c] is None:
			return True
		elif board.mat[r][c].color != self.color:
			return True
		return False

	def _straight_moves(self, board):
		possible = []
		for row in range(1, 4):
			(r, c) = (self.row - row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for row in range(1, 4):
			(r, c) = (self.row + row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col - col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col + col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

	def _diagonal_moves(self, board):
		possible = []
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

class Rook(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board)
		return possible

class Queen(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board) + self._diagonal_moves(board)
		return possible

class Knight(Piece):

	def valid_moves(self, board):
		(r, c) = (self.row, self.col)
		possible = [(r - 2, c - 1), (r - 2, c + 1), (r - 1, c + 2), (r - 1, c - 2), (r + 1, c + 2), (r + 1, c - 2), (r + 2, c - 1), (r + 2, c + 1)]
		return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]

class Bishop(Piece):

	def valid_moves(self, board):
		possible = self._diagonal_moves(board)
		return possible

class Pawn(Piece):

	def can_move_to(self, r, c):
		if self.ptype == 'P':
			if r == self.row and c == self.col:
				return False
			if r < 0 or r > 3:
				return False
			if c < 0 or c > 3:
				return False
			if self.color == 'W' and r < self.row:
				return False
			elif self.color == 'B' and self.row < r:
				return False
			if c == self.col:
				if board.mat[r][c] is None:
					return True
			elif board.mat[r][c] is not None and board.mat[r][c].color != self.color:
				return True
			return False
		else:
			return Piece.can_move_to(self, r, c)

	def move_will_promote(self):
		if self.ptype == 'P':
			if self.color == 'W' and self.row == 2:
				return True
			elif self.color == 'B' and self.row == 1:
				return True
		return False

	def valid_moves(self, board):
		if self.ptype == 'P':
			(r, c) = (self.row, self.col)
			possible = [(r - 1, c), (r - 1, c - 1), (r - 1, c + 1), (r + 1, c), (r + 1, c - 1), (r + 1, c + 1)]
			return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]
		elif self.ptype == 'N':
			return Knight.valid_moves(self, board)
		elif self.ptype == 'R':
			return Rook.valid_moves(self, board)
		elif self.ptype == 'B':
			return Bishop.valid_moves(self, board)

	def promote_to(self, pType):
		self.ptype = pType

	def undo_promote(self):
		self.ptype = 'P'

class Board(object):

	def __init__(self):
		self.mat = [[None for i in range(4)] for u in range(4)]
		self.white = []
		self.black = []

	def add_piece(self, color, ptype, row, col):
		if ptype == 'Q':
			piece = Queen(color, ptype, row, col)
		elif ptype == 'N':
			piece = Knight(color, ptype, row, col)
		elif ptype == 'B':
			piece = Bishop(color, ptype, row, col)
		elif ptype == 'R':
			piece = Rook(color, ptype, row, col)
		elif ptype == 'P':
			piece = Pawn(color, ptype, row, col)
		else:
			raise Exception('Invalid type')
		self.mat[row][col] = piece
		if color == 'W':
			self.white.append(piece)
		elif color == 'B':
			self.black.append(piece)

	def move_to(self, piece, row, col):
		self.mat[piece.row][piece.col] = None
		if self.mat[row][col] is not None:
			captured = self.mat[row][col]
			captured.captured = True
		self.mat[row][col] = piece
		piece.row = row
		piece.col = col

	def undo_move(self, captured, piece, prev_r, prev_c):
		self.move_to(piece, prev_r, prev_c)
		if captured:
			(cr, cc) = (captured.row, captured.col)
			self.mat[cr][cc] = captured
			captured.captured = False

	def is_open(self, r, c):
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if self.mat[r][c] is None:
			return True
		return False

	def white_can_win(self, turns_left):
		if turns_left <= 0:
			return False
		white_pieces = self.white
		q = []
		for piece in white_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return True
				q.insert(0, (piece, r, c))
		for (piece, r, c) in q:
			(prev_r, prev_c) = (piece.row, piece.col)
			captured = self.mat[r][c]
			willPromote = piece.move_will_promote()
			self.move_to(piece, r, c)
			if willPromote:
				for pType in ['N', 'B', 'R']:
					piece.promote_to(pType)
					if self.black_cannot_avoid_loss(turns_left - 1):
						piece.undo_promote()
						self.undo_move(captured, piece, prev_r, prev_c)
						return True
					piece.undo_promote()
			elif self.black_cannot_avoid_loss(turns_left - 1):
				self.undo_move(captured, piece, prev_r, prev_c)
				return True
			self.undo_move(captured, piece, prev_r, prev_c)
		return False

	def black_cannot_avoid_loss(self, turns_left):
		if turns_left <= 0:
			return False
		black_pieces = self.black
		for piece in black_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				(prev_r, prev_c) = (piece.row, piece.col)
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return False
				willPromote = piece.move_will_promote()
				self.move_to(piece, r, c)
				if willPromote:
					for pType in ['N', 'B', 'R']:
						piece.promote_to(pType)
						if not self.white_can_win(turns_left - 1):
							piece.undo_promote()
							self.undo_move(captured, piece, prev_r, prev_c)
							return False
						piece.undo_promote()
				elif not self.white_can_win(turns_left - 1):
					self.undo_move(captured, piece, prev_r, prev_c)
					return False
				self.undo_move(captured, piece, prev_r, prev_c)
		return True
games = int(input())
for g in range(games):
	board = Board()
	cols = 'ABCD'
	(w, b, m) = map(int, input().split(' '))
	for i in range(w):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('W', ptype, row, col)
	for i in range(b):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('B', ptype, row, col)
	print('YES' if board.white_can_win(m) else 'NO')


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def find_moves_for_piece(piece, movers, stills, white_moves):
	movers_pos = [p[1:] for p in movers]
	stills_pos = [p[1:] for p in stills]
	(piece_name, x, y) = piece
	if piece_name == 'Q':
		moves = find_moves_for_piece(['R', x, y], movers, stills, white_moves) + find_moves_for_piece(['B', x, y], movers, stills, white_moves)
		return [['Q', p[1], p[2]] for p in moves]
	elif piece_name == 'N':
		return [['N', pos[0], pos[1]] for pos in [[x + 2, y + 1], [x + 2, y - 1], [x - 2, y + 1], [x - 2, y - 1], [x - 1, y + 2], [x + 1, y + 2], [x - 1, y - 2], [x + 1, y - 2]] if pos not in movers_pos and -1 < pos[0] < 4 and (-1 < pos[1] < 4)]
	elif piece_name == 'B':
		moves = list()
		for (new_x, new_y) in zip(range(x + 1, 4), range(y + 1, 4)):
			if [new_x, new_y] not in movers_pos:
				moves.append(['B', new_x, new_y])
			else:
				break
			if [new_x, new_y] in stills_pos:
				break
		for (new_x, new_y) in zip(range(x - 1, -1, -1), range(y + 1, 4)):
			if [new_x, new_y] not in movers_pos:
				moves.append(['B', new_x, new_y])
			else:
				break
			if [new_x, new_y] in stills_pos:
				break
		for (new_x, new_y) in zip(range(x - 1, -1, -1), range(y - 1, -1, -1)):
			if [new_x, new_y] not in movers_pos:
				moves.append(['B', new_x, new_y])
			else:
				break
			if [new_x, new_y] in stills_pos:
				break
		for (new_x, new_y) in zip(range(x + 1, 4), range(y - 1, -1, -1)):
			if [new_x, new_y] not in movers_pos:
				moves.append(['B', new_x, new_y])
			else:
				break
			if [new_x, new_y] in stills_pos:
				break
		return moves
	elif piece_name == 'R':
		moves = list()
		for new_x in range(x + 1, 4):
			if [new_x, y] not in movers_pos:
				moves.append(['R', new_x, y])
			else:
				break
			if [new_x, y] in stills_pos:
				break
		for new_x in range(x - 1, -1, -1):
			if [new_x, y] not in movers_pos:
				moves.append(['R', new_x, y])
			else:
				break
			if [new_x, y] in stills_pos:
				break
		for new_y in range(y + 1, 4):
			if [x, new_y] not in movers_pos:
				moves.append(['R', x, new_y])
			else:
				break
			if [x, new_y] in stills_pos:
				break
		for new_y in range(y - 1, -1, -1):
			if [x, new_y] not in movers_pos:
				moves.append(['R', x, new_y])
			else:
				break
			if [x, new_y] in stills_pos:
				break
		return moves
	elif piece_name == 'P':
		moves = list()
		if white_moves:
			new_y = y + 1
		else:
			new_y = y - 1
		if [x, new_y] not in movers_pos + stills_pos:
			moves.append(['P', x, new_y])
		if [x - 1, new_y] in stills_pos:
			moves.append(['P', x - 1, new_y])
		if [x + 1, new_y] in stills_pos:
			moves.append(['P', x + 1, new_y])
		if new_y not in [0, 3]:
			return moves
		else:
			changed = list()
			[changed.extend([['N', p[1], p[2]], ['B', p[1], p[2]], ['R', p[1], p[2]]]) for p in moves]
			return changed

def chess(movers, stills, moves_left, white_moves):
	if moves_left == 0:
		if white_moves:
			return False
		else:
			return True
	moves = list()
	[moves.extend(find_moves_for_piece(p, movers, stills, white_moves)) for p in movers]
	still_queen_pos = list(filter(lambda p: p[0] == 'Q', stills))[0][1:]
	for move in moves:
		if still_queen_pos == move[1:]:
			return True
	for mover_index in range(len(movers)):
		original_piece = movers[mover_index]
		for moved_piece in find_moves_for_piece(original_piece, movers, stills, white_moves):
			movers[mover_index] = moved_piece
			taken_piece = None
			taken_index = None
			for i in range(len(stills)):
				piece = stills[i]
				if piece[1:] == moved_piece[1:]:
					taken_piece = piece
					taken_index = i
					break
			if taken_index is not None:
				stills.pop(taken_index)
			if not chess(stills, movers, moves_left - 1, not white_moves):
				movers[mover_index] = original_piece
				if taken_index is not None:
					stills.insert(taken_index, taken_piece)
				return True
			else:
				movers[mover_index] = original_piece
				if taken_index is not None:
					stills.insert(taken_index, taken_piece)
	return False
g = int(input().strip())
row_nums = {'A': 0, 'B': 1, 'C': 2, 'D': 3}
for g_itr in range(g):
	first_multiple_input = input().rstrip().split()
	w = int(first_multiple_input[0])
	b = int(first_multiple_input[1])
	m = int(first_multiple_input[2])
	white = []
	for _ in range(w):
		piece = input().split()
		white.append([piece[0], row_nums[piece[1]], int(piece[2]) - 1])
	black = []
	for _ in range(b):
		piece = input().split()
		black.append([piece[0], row_nums[piece[1]], int(piece[2]) - 1])
	if chess(white, black, m, True):
		print('YES')
	else:
		print('NO')


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input().strip())
for g_itr in range(g):
	first_multiple_input = input().rstrip().split()
	w = int(first_multiple_input[0])
	b = int(first_multiple_input[1])
	m = int(first_multiple_input[2])
	white = []
	for _ in range(w):
		white.append(input().rstrip().split())
	black = []
	for _ in range(b):
		black.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in white]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in black]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(gurram, camell, kinguu, quenuu, solderuu, dabbaa):
	if gurram == 'R':
		sill = []
		soll = 1
		while camell + soll < 4 and (not (camell + soll, kinguu) in quenuu):
			sill += [('R', camell + soll, kinguu)]
			if (camell + soll, kinguu) in solderuu:
				break
			soll += 1
		soll = 1
		while camell - soll >= 0 and (not (camell - soll, kinguu) in quenuu):
			sill += [('R', camell - soll, kinguu)]
			if (camell - soll, kinguu) in solderuu:
				break
			soll += 1
		soll = 1
		while kinguu + soll < 4 and (not (camell, kinguu + soll) in quenuu):
			sill += [('R', camell, kinguu + soll)]
			if (camell, kinguu + soll) in solderuu:
				break
			soll += 1
		soll = 1
		while kinguu - soll >= 0 and (not (camell, kinguu - soll) in quenuu):
			sill += [('R', camell, kinguu - soll)]
			if (camell, kinguu - soll) in solderuu:
				break
			soll += 1
		return sill
	elif gurram == 'B':
		sill = []
		soll = 1
		while camell + soll < 4 and kinguu + soll < 4 and (not (camell + soll, kinguu + soll) in quenuu):
			sill += [('B', camell + soll, kinguu + soll)]
			if (camell + soll, kinguu + soll) in solderuu:
				break
			soll += 1
		soll = 1
		while camell - soll >= 0 and kinguu + soll < 4 and (not (camell - soll, kinguu + soll) in quenuu):
			sill += [('B', camell - soll, kinguu + soll)]
			if (camell - soll, kinguu + soll) in solderuu:
				break
			soll += 1
		soll = 1
		while camell + soll < 4 and kinguu - soll >= 0 and (not (camell + soll, kinguu - soll) in quenuu):
			sill += [('B', camell + soll, kinguu - soll)]
			if (camell + soll, kinguu - soll) in solderuu:
				break
			soll += 1
		soll = 1
		while camell - soll >= 0 and kinguu - soll >= 0 and (not (camell - soll, kinguu - soll) in quenuu):
			sill += [('B', camell - soll, kinguu - soll)]
			if (camell - soll, kinguu - soll) in solderuu:
				break
			soll += 1
		return sill
	elif gurram == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', camell, kinguu, quenuu, solderuu, dabbaa) + validmoves('B', camell, kinguu, quenuu, solderuu, dabbaa)]
	elif gurram == 'N':
		return [('N', z[0], z[1]) for z in [(camell + 2, kinguu + 1), (camell + 2, kinguu - 1), (camell + 1, kinguu + 2), (camell + 1, kinguu - 2), (camell - 1, kinguu + 2), (camell - 1, kinguu - 2), (camell - 2, kinguu + 1), (camell - 2, kinguu - 1)] if not z in quenuu and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif gurram == 'P':
		kinguu += 1 - 2 * dabbaa
		kalkii = []
		for soll in [-1, 1]:
			if 0 <= camell + soll <= 3 and (camell + soll, kinguu) in solderuu:
				kalkii += [(camell + soll, kinguu)]
		if not (camell, kinguu) in quenuu + solderuu:
			kalkii += [(camell, kinguu)]
		if kinguu in [0, 3]:
			return [('N', x[0], x[1]) for x in kalkii] + [('B', x[0], x[1]) for x in kalkii] + [('R', x[0], x[1]) for x in kalkii]
		else:
			return [('P', x[0], x[1]) for x in kalkii]

def simplifiedChessEngine(quenuu, solderuu, jargus, dabbaa):
	if jargus == 0:
		if dabbaa:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in quenuu]
		bl = [(x[1], x[2]) for x in solderuu]
		(soll, j) = [z[1:] for z in solderuu if z[0] == 'Q'][0]
		kalkii = sum([validmoves(x[0], x[1], x[2], wh, bl, dabbaa) for x in quenuu], [])
		if (soll, j) in [x[1:] for x in kalkii]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for soll in range(len(quenuu)):
				for t in validmoves(quenuu[soll][0], quenuu[soll][1], quenuu[soll][2], wh, bl, dabbaa):
					if (t[1], t[2]) in bl:
						nextmove.append((soll, t))
					else:
						nextmove2.append((soll, t))
			for x in nextmove + nextmove2:
				(soll, t) = (x[0], x[1])
				(gurram, camell, kinguu) = quenuu[soll]
				taken = [z for z in solderuu if (z[1], z[2]) == t[1:]]
				solderuu = [z for z in solderuu if not z in taken]
				quenuu[soll] = t
				if simplifiedChessEngine(solderuu, quenuu, jargus - 1, 1 - dabbaa) == 'NO':
					quenuu[soll] = (gurram, camell, kinguu)
					solderuu += taken
					return 'YES'
				else:
					quenuu[soll] = (gurram, camell, kinguu)
					solderuu += taken
			return 'NO'
gudu = int(input())
for g_itr in range(gudu):
	naku = input().split()
	pika = int(naku[0])
	dabbaa = int(naku[1])
	kaskuu = int(naku[2])
	quenuu = []
	for _ in range(pika):
		quenuu.append(input().rstrip().split())
	solderuu = []
	for _ in range(dabbaa):
		solderuu.append(input().rstrip().split())
	quenuu = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in quenuu]
	solderuu = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in solderuu]
	result = simplifiedChessEngine(quenuu, solderuu, kaskuu, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


class Piece(object):

	def __init__(self, color, ptype, row, col):
		self.color = color
		self.ptype = ptype
		self.row = row
		self.col = col
		self.captured = False

	def valid_moves(self, board):
		pass

	def move_will_promote(self):
		return False

	def can_move_to(self, r, c):
		if r == self.row and c == self.col:
			return False
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if board.mat[r][c] is None:
			return True
		elif board.mat[r][c].color != self.color:
			return True
		return False

	def _straight_moves(self, board):
		possible = []
		for row in range(1, 4):
			(r, c) = (self.row - row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for row in range(1, 4):
			(r, c) = (self.row + row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col - col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col + col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

	def _diagonal_moves(self, board):
		possible = []
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

class Rook(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board)
		return possible

class Queen(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board) + self._diagonal_moves(board)
		return possible

class Knight(Piece):

	def valid_moves(self, board):
		(r, c) = (self.row, self.col)
		possible = [(r - 2, c - 1), (r - 2, c + 1), (r - 1, c + 2), (r - 1, c - 2), (r + 1, c + 2), (r + 1, c - 2), (r + 2, c - 1), (r + 2, c + 1)]
		return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]

class Bishop(Piece):

	def valid_moves(self, board):
		possible = self._diagonal_moves(board)
		return possible

class Pawn(Piece):

	def can_move_to(self, r, c):
		if self.ptype == 'P':
			if r == self.row and c == self.col:
				return False
			if r < 0 or r > 3:
				return False
			if c < 0 or c > 3:
				return False
			if self.color == 'W' and r < self.row:
				return False
			elif self.color == 'B' and self.row < r:
				return False
			if c == self.col:
				if board.mat[r][c] is None:
					return True
			elif board.mat[r][c] is not None and board.mat[r][c].color != self.color:
				return True
			return False
		else:
			return Piece.can_move_to(self, r, c)

	def move_will_promote(self):
		if self.ptype == 'P':
			if self.color == 'W' and self.row == 2:
				return True
			elif self.color == 'B' and self.row == 1:
				return True
		return False

	def valid_moves(self, board):
		if self.ptype == 'P':
			(r, c) = (self.row, self.col)
			possible = [(r - 1, c), (r - 1, c - 1), (r - 1, c + 1), (r + 1, c), (r + 1, c - 1), (r + 1, c + 1)]
			return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]
		elif self.ptype == 'N':
			return Knight.valid_moves(self, board)
		elif self.ptype == 'R':
			return Rook.valid_moves(self, board)
		elif self.ptype == 'B':
			return Bishop.valid_moves(self, board)

	def promote_to(self, pType):
		self.ptype = pType

	def undo_promote(self):
		self.ptype = 'P'

class Board(object):

	def __init__(self):
		self.mat = [[None for i in range(4)] for u in range(4)]
		self.white = []
		self.black = []

	def add_piece(self, color, ptype, row, col):
		if ptype == 'Q':
			piece = Queen(color, ptype, row, col)
		elif ptype == 'N':
			piece = Knight(color, ptype, row, col)
		elif ptype == 'B':
			piece = Bishop(color, ptype, row, col)
		elif ptype == 'R':
			piece = Rook(color, ptype, row, col)
		elif ptype == 'P':
			piece = Pawn(color, ptype, row, col)
		else:
			raise Exception('Invalid type')
		self.mat[row][col] = piece
		if color == 'W':
			self.white.append(piece)
		elif color == 'B':
			self.black.append(piece)

	def move_to(self, piece, row, col):
		self.mat[piece.row][piece.col] = None
		if self.mat[row][col] is not None:
			captured = self.mat[row][col]
			captured.captured = True
		self.mat[row][col] = piece
		piece.row = row
		piece.col = col

	def undo_move(self, captured, piece, prev_r, prev_c):
		self.move_to(piece, prev_r, prev_c)
		if captured:
			(cr, cc) = (captured.row, captured.col)
			self.mat[cr][cc] = captured
			captured.captured = False

	def is_open(self, r, c):
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if self.mat[r][c] is None:
			return True
		return False

	def white_can_win(self, turns_left):
		if turns_left <= 0:
			return False
		white_pieces = self.white
		q = []
		for piece in white_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return True
				q.insert(0, (piece, r, c))
		for (piece, r, c) in q:
			(prev_r, prev_c) = (piece.row, piece.col)
			captured = self.mat[r][c]
			willPromote = piece.move_will_promote()
			self.move_to(piece, r, c)
			if willPromote:
				for pType in ['N', 'B', 'R']:
					piece.promote_to(pType)
					if self.black_cannot_avoid_loss(turns_left - 1):
						piece.undo_promote()
						self.undo_move(captured, piece, prev_r, prev_c)
						return True
					piece.undo_promote()
			elif self.black_cannot_avoid_loss(turns_left - 1):
				self.undo_move(captured, piece, prev_r, prev_c)
				return True
			self.undo_move(captured, piece, prev_r, prev_c)
		return False

	def black_cannot_avoid_loss(self, turns_left):
		if turns_left <= 0:
			return False
		black_pieces = self.black
		for piece in black_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				(prev_r, prev_c) = (piece.row, piece.col)
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return False
				willPromote = piece.move_will_promote()
				self.move_to(piece, r, c)
				if willPromote:
					for pType in ['N', 'B', 'R']:
						piece.promote_to(pType)
						if not self.white_can_win(turns_left - 1):
							piece.undo_promote()
							self.undo_move(captured, piece, prev_r, prev_c)
							return False
						piece.undo_promote()
				elif not self.white_can_win(turns_left - 1):
					self.undo_move(captured, piece, prev_r, prev_c)
					return False
				self.undo_move(captured, piece, prev_r, prev_c)
		return True
games = int(input())
for g in range(games):
	board = Board()
	cols = 'ABCD'
	(w, b, m) = map(int, input().split(' '))
	for i in range(w):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('W', ptype, row, col)
	for i in range(b):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('B', ptype, row, col)
	print('YES' if board.white_can_win(m) else 'NO')


class Piece(object):

	def __init__(self, color, ptype, row, col):
		self.color = color
		self.ptype = ptype
		self.row = row
		self.col = col
		self.captured = False

	def valid_moves(self, board):
		pass

	def move_will_promote(self):
		return False

	def can_move_to(self, r, c):
		if r == self.row and c == self.col:
			return False
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if board.mat[r][c] is None:
			return True
		elif board.mat[r][c].color != self.color:
			return True
		return False

	def _straight_moves(self, board):
		possible = []
		for row in range(1, 4):
			(r, c) = (self.row - row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for row in range(1, 4):
			(r, c) = (self.row + row, self.col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col - col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for col in range(1, 4):
			(r, c) = (self.row, self.col + col)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

	def _diagonal_moves(self, board):
		possible = []
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col - rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row - rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		for rc in range(1, 4):
			(r, c) = (self.row + rc, self.col + rc)
			if self.can_move_to(r, c):
				possible.append((r, c))
			if not board.is_open(r, c):
				break
		return possible

class Rook(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board)
		return possible

class Queen(Piece):

	def valid_moves(self, board):
		possible = self._straight_moves(board) + self._diagonal_moves(board)
		return possible

class Knight(Piece):

	def valid_moves(self, board):
		(r, c) = (self.row, self.col)
		possible = [(r - 2, c - 1), (r - 2, c + 1), (r - 1, c + 2), (r - 1, c - 2), (r + 1, c + 2), (r + 1, c - 2), (r + 2, c - 1), (r + 2, c + 1)]
		return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]

class Bishop(Piece):

	def valid_moves(self, board):
		possible = self._diagonal_moves(board)
		return possible

class Pawn(Piece):

	def can_move_to(self, r, c):
		if self.ptype == 'P':
			if r == self.row and c == self.col:
				return False
			if r < 0 or r > 3:
				return False
			if c < 0 or c > 3:
				return False
			if self.color == 'W' and r < self.row:
				return False
			elif self.color == 'B' and self.row < r:
				return False
			if c == self.col:
				if board.mat[r][c] is None:
					return True
			elif board.mat[r][c] is not None and board.mat[r][c].color != self.color:
				return True
			return False
		else:
			return Piece.can_move_to(self, r, c)

	def move_will_promote(self):
		if self.ptype == 'P':
			if self.color == 'W' and self.row == 2:
				return True
			elif self.color == 'B' and self.row == 1:
				return True
		return False

	def valid_moves(self, board):
		if self.ptype == 'P':
			(r, c) = (self.row, self.col)
			possible = [(r - 1, c), (r - 1, c - 1), (r - 1, c + 1), (r + 1, c), (r + 1, c - 1), (r + 1, c + 1)]
			return [(r, c) for (r, c) in possible if self.can_move_to(r, c)]
		elif self.ptype == 'N':
			return Knight.valid_moves(self, board)
		elif self.ptype == 'R':
			return Rook.valid_moves(self, board)
		elif self.ptype == 'B':
			return Bishop.valid_moves(self, board)

	def promote_to(self, pType):
		self.ptype = pType

	def undo_promote(self):
		self.ptype = 'P'

class Board(object):

	def __init__(self):
		self.mat = [[None for i in range(4)] for u in range(4)]
		self.white = []
		self.black = []

	def add_piece(self, color, ptype, row, col):
		if ptype == 'Q':
			piece = Queen(color, ptype, row, col)
		elif ptype == 'N':
			piece = Knight(color, ptype, row, col)
		elif ptype == 'B':
			piece = Bishop(color, ptype, row, col)
		elif ptype == 'R':
			piece = Rook(color, ptype, row, col)
		elif ptype == 'P':
			piece = Pawn(color, ptype, row, col)
		else:
			raise Exception('Invalid type')
		self.mat[row][col] = piece
		if color == 'W':
			self.white.append(piece)
		elif color == 'B':
			self.black.append(piece)

	def move_to(self, piece, row, col):
		self.mat[piece.row][piece.col] = None
		if self.mat[row][col] is not None:
			captured = self.mat[row][col]
			captured.captured = True
		self.mat[row][col] = piece
		piece.row = row
		piece.col = col

	def undo_move(self, captured, piece, prev_r, prev_c):
		self.move_to(piece, prev_r, prev_c)
		if captured:
			(cr, cc) = (captured.row, captured.col)
			self.mat[cr][cc] = captured
			captured.captured = False

	def is_open(self, r, c):
		if r < 0 or r > 3:
			return False
		if c < 0 or c > 3:
			return False
		if self.mat[r][c] is None:
			return True
		return False

	def white_can_win(self, turns_left):
		if turns_left <= 0:
			return False
		white_pieces = self.white
		q = []
		for piece in white_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return True
				q.insert(0, (piece, r, c))
		for (piece, r, c) in q:
			(prev_r, prev_c) = (piece.row, piece.col)
			captured = self.mat[r][c]
			willPromote = piece.move_will_promote()
			self.move_to(piece, r, c)
			if willPromote:
				for pType in ['N', 'B', 'R']:
					piece.promote_to(pType)
					if self.black_cannot_avoid_loss(turns_left - 1):
						piece.undo_promote()
						self.undo_move(captured, piece, prev_r, prev_c)
						return True
					piece.undo_promote()
			elif self.black_cannot_avoid_loss(turns_left - 1):
				self.undo_move(captured, piece, prev_r, prev_c)
				return True
			self.undo_move(captured, piece, prev_r, prev_c)
		return False

	def black_cannot_avoid_loss(self, turns_left):
		if turns_left <= 0:
			return False
		black_pieces = self.black
		for piece in black_pieces:
			if piece.captured is True:
				continue
			valid_moves = piece.valid_moves(self)
			for (r, c) in valid_moves:
				(prev_r, prev_c) = (piece.row, piece.col)
				captured = self.mat[r][c]
				if captured is not None and captured.ptype == 'Q':
					return False
				willPromote = piece.move_will_promote()
				self.move_to(piece, r, c)
				if willPromote:
					for pType in ['N', 'B', 'R']:
						piece.promote_to(pType)
						if not self.white_can_win(turns_left - 1):
							piece.undo_promote()
							self.undo_move(captured, piece, prev_r, prev_c)
							return False
						piece.undo_promote()
				elif not self.white_can_win(turns_left - 1):
					self.undo_move(captured, piece, prev_r, prev_c)
					return False
				self.undo_move(captured, piece, prev_r, prev_c)
		return True
games = int(input())
for g in range(games):
	board = Board()
	cols = 'ABCD'
	(w, b, m) = map(int, input().split(' '))
	for i in range(w):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('W', ptype, row, col)
	for i in range(b):
		(ptype, col, row) = input().split(' ')
		col = cols.index(col)
		row = int(row) - 1
		board.add_piece('B', ptype, row, col)
	print('YES' if board.white_can_win(m) else 'NO')


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)


import math
import os
import random
import re
import sys

def validmoves(piece, posx, posy, whites, blacks, b):
	if piece == 'R':
		L = []
		i = 1
		while posx + i < 4 and (not (posx + i, posy) in whites):
			L += [('R', posx + i, posy)]
			if (posx + i, posy) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and (not (posx - i, posy) in whites):
			L += [('R', posx - i, posy)]
			if (posx - i, posy) in blacks:
				break
			i += 1
		i = 1
		while posy + i < 4 and (not (posx, posy + i) in whites):
			L += [('R', posx, posy + i)]
			if (posx, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posy - i >= 0 and (not (posx, posy - i) in whites):
			L += [('R', posx, posy - i)]
			if (posx, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'B':
		L = []
		i = 1
		while posx + i < 4 and posy + i < 4 and (not (posx + i, posy + i) in whites):
			L += [('B', posx + i, posy + i)]
			if (posx + i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy + i < 4 and (not (posx - i, posy + i) in whites):
			L += [('B', posx - i, posy + i)]
			if (posx - i, posy + i) in blacks:
				break
			i += 1
		i = 1
		while posx + i < 4 and posy - i >= 0 and (not (posx + i, posy - i) in whites):
			L += [('B', posx + i, posy - i)]
			if (posx + i, posy - i) in blacks:
				break
			i += 1
		i = 1
		while posx - i >= 0 and posy - i >= 0 and (not (posx - i, posy - i) in whites):
			L += [('B', posx - i, posy - i)]
			if (posx - i, posy - i) in blacks:
				break
			i += 1
		return L
	elif piece == 'Q':
		return [('Q', z[1], z[2]) for z in validmoves('R', posx, posy, whites, blacks, b) + validmoves('B', posx, posy, whites, blacks, b)]
	elif piece == 'N':
		return [('N', z[0], z[1]) for z in [(posx + 2, posy + 1), (posx + 2, posy - 1), (posx + 1, posy + 2), (posx + 1, posy - 2), (posx - 1, posy + 2), (posx - 1, posy - 2), (posx - 2, posy + 1), (posx - 2, posy - 1)] if not z in whites and 0 <= z[0] <= 3 and (0 <= z[1] <= 3)]
	elif piece == 'P':
		posy += 1 - 2 * b
		l = []
		for i in [-1, 1]:
			if 0 <= posx + i <= 3 and (posx + i, posy) in blacks:
				l += [(posx + i, posy)]
		if not (posx, posy) in whites + blacks:
			l += [(posx, posy)]
		if posy in [0, 3]:
			return [('N', x[0], x[1]) for x in l] + [('B', x[0], x[1]) for x in l] + [('R', x[0], x[1]) for x in l]
		else:
			return [('P', x[0], x[1]) for x in l]

def simplifiedChessEngine(whites, blacks, moves, b):
	if moves == 0:
		if b:
			return 'YES'
		else:
			return 'NO'
	else:
		wh = [(x[1], x[2]) for x in whites]
		bl = [(x[1], x[2]) for x in blacks]
		(i, j) = [z[1:] for z in blacks if z[0] == 'Q'][0]
		l = sum([validmoves(x[0], x[1], x[2], wh, bl, b) for x in whites], [])
		if (i, j) in [x[1:] for x in l]:
			return 'YES'
		else:
			nextmove = []
			nextmove2 = []
			for i in range(len(whites)):
				for t in validmoves(whites[i][0], whites[i][1], whites[i][2], wh, bl, b):
					if (t[1], t[2]) in bl:
						nextmove.append((i, t))
					else:
						nextmove2.append((i, t))
			for x in nextmove + nextmove2:
				(i, t) = (x[0], x[1])
				(piece, posx, posy) = whites[i]
				taken = [z for z in blacks if (z[1], z[2]) == t[1:]]
				blacks = [z for z in blacks if not z in taken]
				whites[i] = t
				if simplifiedChessEngine(blacks, whites, moves - 1, 1 - b) == 'NO':
					whites[i] = (piece, posx, posy)
					blacks += taken
					return 'YES'
				else:
					whites[i] = (piece, posx, posy)
					blacks += taken
			return 'NO'
g = int(input())
for g_itr in range(g):
	wbm = input().split()
	w = int(wbm[0])
	b = int(wbm[1])
	m = int(wbm[2])
	whites = []
	for _ in range(w):
		whites.append(input().rstrip().split())
	blacks = []
	for _ in range(b):
		blacks.append(input().rstrip().split())
	whites = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in whites]
	blacks = [[x[0], ord(x[1]) - 65, int(x[2]) - 1] for x in blacks]
	result = simplifiedChessEngine(whites, blacks, m, 0)
	print(result)