You are given an 8\times 8 chessboard. Your enemy king is at point (X,Y) (1 ≤ X,Y ≤ 8).

You need to place some number of [queen] pieces on the chessboard such that the following conditions are satisfied:

The king should not be under attack by any of the queens that you have placed.
The [king] should not be able to make any move.

Note that a king can move one unit in any of the 8 direction while a queen can attack horizontally, vertically as well as diagonally.

Find the minimum number of queens required to satisfy the given conditions.
Print the chessboard where 0 denotes empty cells, 1 denotes the king, and 2 denotes the queen(s) placed by you in some position(s).

In case of multiple answers, print any.

------ Input Format ------ 

- The first line of input will contain a single integer T, denoting the number of test cases.
- The only line of each test case contains two space-separated integers A and B — the coordinates of the king.
    
------ Output Format ------ 

For each test case, output a 8\times 8 grid consisting of King, Queen(s) and blank cells.

Print the chessboard in 8 lines where each line has 8 space-separated integers where 0 denotes empty cells, 1 denotes the king, and 2 denotes the queen(s) placed by you in some position(s).

------ Constraints ------ 

$1 ≤ T ≤ 64$
$1 ≤ A,B ≤ 8$

----- Sample Input 1 ------ 
1
2 1

----- Sample Output 1 ------ 
0 0 2 0 0 0 0 0
1 0 0 0 0 0 0 0
0 0 2 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
----- explanation 1 ------ 
Test case $1$: We can place $2$ queens at $(1, 3)$ and $(3, 3)$ respectively. Thus, the king at $(2, 1)$ has no safe squares to go to and is not under attack. 

![image]

Difficulty: MEDIUM_HARD
Tags: Complete search

Constructive algorithms

Ad-hoc
Skill types: Complete search

Starter code:

Solutions:
t = int(input())
for _ in range(t):
	(x, y) = map(int, input().split())
	x -= 1
	y -= 1
	ans = [[0 for _ in range(8)] for _ in range(8)]
	ans[x][y] = 1
	a = [[0, 0], [0, 7], [7, 0], [7, 7]]
	b = [[-2, -1], [-2, +1], [-1, -2], [-1, +2], [+1, -2], [+1, +2], [+2, -1], [+2, +1]]
	if [x, y] in a:
		for i in range(len(b)):
			if 0 <= x + b[i][0] <= 7 and 0 <= y + b[i][1] <= 7:
				ans[x + b[i][0]][y + b[i][1]] = 2
				break
	elif x == 0:
		ans[x + 2][y - 1] = 2
		ans[x + 2][y + 1] = 2
	elif x == 7:
		ans[x - 2][y - 1] = 2
		ans[x - 2][y + 1] = 2
	elif y == 0:
		ans[x + 1][y + 2] = 2
		ans[x - 1][y + 2] = 2
	elif y == 7:
		ans[x + 1][y - 2] = 2
		ans[x - 1][y - 2] = 2
	elif x == 1 and y == 1:
		ans[x + 2][y - 1] = 2
		ans[x - 1][y + 3] = 2
	elif x == 1 and y == 6:
		ans[x - 1][y - 3] = 2
		ans[x + 2][y + 1] = 2
	elif x == 6 and y == 1:
		ans[x - 2][y - 1] = 2
		ans[x + 1][y + 3] = 2
	elif x == 6 and y == 6:
		ans[x - 2][y + 1] = 2
		ans[x + 1][y - 3] = 2
	elif y == 1 or y == 6:
		ans[x - 2][y + 1] = 2
		ans[x + 2][y - 1] = 2
	elif x == 1 or x == 6:
		ans[x - 1][y - 2] = 2
		ans[x + 1][y + 2] = 2
	else:
		ans[x - 1][y - 2] = 2
		ans[x + 1][y + 2] = 2
	for i in range(8):
		print(*ans[i])


t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	ans = []
	if a == 0 and b == 0 or (a == 0 and b == 7) or (a == 7 and b == 0) or (a == 7 and b == 7):
		count = 1
	elif a == 1 and b == 1 or (a == 1 and b == 6) or (a == 6 and b == 1) or (a == 6 and b == 6):
		count = 0
		if a == 1 and b == 1:
			ans = [[0, 4], [3, 0]]
		elif a == 1 and b == 6:
			ans = [[0, 4], [4, 7]]
		elif a == 6 and b == 1:
			ans = [[3, 0], [7, 3]]
		else:
			ans = [[4, 7], [7, 3]]
	else:
		count = 2
	temp = count
	arr = [[1, 2], [2, 1], [-1, 2], [2, -1]]
	arr_sec = [[-1, -2], [-2, -1], [1, 2], [2, 1], [-1, 2], [-2, 1], [1, -2], [2, -1]]
	if count == 2:
		if a >= 1 and a <= 6 and (b >= 1) and (b <= 6):
			for i in arr:
				if count > 0:
					if a + i[0] >= 0 and a + i[0] <= 7 and (b + i[1] >= 0) and (b + i[1] <= 7) and (a - i[0] >= 0) and (a - i[0] <= 7) and (b - i[1] >= 0) and (b - i[1] <= 7):
						ans.append([a + i[0], b + i[1]])
						ans.append([a - i[0], b - i[1]])
						count -= 2
				else:
					break
		else:
			for i in arr_sec:
				if count == 0:
					break
				if a + i[0] >= 0 and b + i[1] >= 0 and (a + i[0] <= 7) and (b + i[1] <= 7):
					ans.append([a + i[0], b + i[1]])
					count -= 1
	else:
		for i in arr_sec:
			if count == 0:
				break
			if a + i[0] >= 0 and b + i[1] >= 0 and (a + i[0] <= 7) and (b + i[1] <= 7):
				ans.append([a + i[0], b + i[1]])
				count -= 1
	mat = []
	for i in range(8):
		row = []
		for j in range(8):
			row.append(0)
		mat.append(row)
	mat[a][b] = 1
	for i in ans:
		mat[i[0]][i[1]] = 2
	for i in mat:
		print(*i)


for _ in range(int(input())):
	(x, y) = map(int, input().split())
	board = []
	for i in range(8):
		temp = []
		for j in range(8):
			temp.append(0)
		board.append(temp)
	a = []
	if (x == 1 or x == 8) and (y == 1 or y == 8):
		if x == 1:
			if y == 1:
				a.append([3, 2])
			else:
				a.append([3, 7])
		elif y == 1:
			a.append([6, 2])
		else:
			a.append([6, 7])
	elif x == 1:
		a.append([3, y - 1])
		a.append([3, y + 1])
	elif x == 8:
		a.append([6, y - 1])
		a.append([6, y + 1])
	elif y == 1:
		a.append([x - 1, 3])
		a.append([x + 1, 3])
	elif y == 8:
		a.append([x - 1, 6])
		a.append([x + 1, 6])
	elif x == 2:
		if y == 2:
			a.append([x + 3, y - 1])
			a.append([x - 1, y + 2])
		elif y == 7:
			a.append([x + 3, y + 1])
			a.append([x - 1, y - 2])
		else:
			a.append([x + 1, y - 2])
			a.append([x - 1, y + 2])
	elif x == 7:
		if y == 2:
			a.append([x - 3, y - 1])
			a.append([x + 1, y + 2])
		elif y == 7:
			a.append([x - 3, y + 1])
			a.append([x + 1, y - 2])
		else:
			a.append([x + 1, y - 2])
			a.append([x - 1, y + 2])
	else:
		a.append([x + 2, y - 1])
		a.append([x - 2, y + 1])
	board[x - 1][y - 1] = 1
	for i in a:
		board[i[0] - 1][i[1] - 1] = 2
	for i in range(8):
		for j in range(8):
			print(board[i][j], end=' ')
		print('')


T = int(input())
for t in range(T):
	(A, B) = map(int, input().split())
	(A, B) = (A - 1, B - 1)
	grid = [[0 for j in range(8)] for i in range(8)]
	grid[A][B] = 1
	if A in {0, 7} and B in {0, 7}:
		if A == 0 and B == 0:
			grid[1][2] = 2
		elif A == 0 and B == 7:
			grid[1][5] = 2
		elif A == 7 and B == 0:
			grid[6][2] = 2
		elif A == 7 and B == 7:
			grid[6][5] = 2
	elif A in range(1, 7) and B in {0, 7}:
		if B == 0:
			grid[A - 1][2] = 2
			grid[A + 1][2] = 2
		elif B == 7:
			grid[A - 1][5] = 2
			grid[A + 1][5] = 2
	elif A in {0, 7} and B in range(1, 7):
		if A == 0:
			grid[2][B - 1] = 2
			grid[2][B + 1] = 2
		elif A == 7:
			grid[5][B - 1] = 2
			grid[5][B + 1] = 2
	elif A in range(1, 5) and B in range(1, 6):
		grid[A - 1][B + 2] = 2
		grid[A + 3][B - 1] = 2
	elif A in range(1, 5) and B == 6:
		grid[A - 1][B - 2] = 2
		grid[A + 3][B + 1] = 2
	elif A == 5 and B in range(1, 7):
		grid[A - 2][B - 1] = 2
		grid[A + 2][B + 1] = 2
	elif A == 6 and B in range(1, 5):
		grid[A - 2][B - 1] = 2
		grid[A + 1][B + 3] = 2
	elif A == 6 and B in {5, 6}:
		grid[A - 3][B + 1] = 2
		grid[A + 1][B - 2] = 2
	for i in range(8):
		print(*grid[i])


def main():
	t = int(input())
	for i in range(t):
		(x, y) = tuple(map(int, input().split()))
		solve(x - 1, y - 1)

def solve(x, y):
	m = [[0] * 8 for i in range(8)]
	m[x][y] = 1
	if x in range(2, 6) and y in range(1, 7):
		m[x - 2][y - 1] = 2
		m[x + 2][y + 1] = 2
	elif x in range(1, 7) and y in range(2, 6):
		m[x - 1][y - 2] = 2
		m[x + 1][y + 2] = 2
	elif x in (0, 7):
		if y in (0, 7):
			u = x + 2 if x == 0 else x - 2
			v = y + 1 if y == 0 else y - 1
			m[u][v] = 2
		else:
			u = x + 2 if x == 0 else x - 2
			m[u][y - 1] = 2
			m[u][y + 1] = 2
	elif y in (0, 7):
		if x in (0, 7):
			u = x + 1 if x == 0 else x - 1
			v = y + 2 if y == 0 else y - 2
			m[u][v] = 2
		else:
			v = y + 2 if y == 0 else y - 2
			m[x - 1][v] = 2
			m[x + 1][v] = 2
	else:
		if (x, y) == (1, 1):
			m[3][0] = 2
			m[0][4] = 2
		if (x, y) == (6, 1):
			m[7][4] = 2
			m[4][0] = 2
		if (x, y) == (6, 6):
			m[4][7] = 2
			m[7][3] = 2
		if (x, y) == (1, 6):
			m[0][3] = 2
			m[3][7] = 2
	for row in m:
		print(' '.join(map(str, row)))
main()


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	arr = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	arr[a][b] = 1
	if a == 0 and b == 0 or (a == 0 and b == 7) or (a == 7 and b == 0) or (a == 7 and b == 7):
		if a == 0 and b == 0:
			arr[2][1] = 2
		elif a == 0 and b == 7:
			arr[2][6] = 2
		elif a == 7 and b == 0:
			arr[5][1] = 2
		elif a == 7 and b == 7:
			arr[5][6] = 2
	elif b == 0 or a == 0 or b == 7 or (a == 7):
		if b == 0:
			arr[a - 1][2] = 2
			arr[a + 1][2] = 2
		elif a == 0:
			arr[2][b - 1] = 2
			arr[2][b + 1] = 2
		elif b == 7:
			arr[a - 1][5] = 2
			arr[a + 1][5] = 2
		elif a == 7:
			arr[5][b + 1] = 2
			arr[5][b - 1] = 2
	elif b + 3 <= 7:
		if a + 2 <= 7:
			arr[a - 1][b + 3] = 2
			arr[a + 2][b - 1] = 2
		else:
			arr[a + 1][b + 3] = 2
			arr[a - 2][b - 1] = 2
	elif a + 3 <= 7:
		arr[a - 1][b - 2] = 2
		arr[a + 3][b + 1] = 2
	else:
		arr[a + 1][b - 3] = 2
		arr[a - 2][b + 1] = 2
	for x in arr:
		print(*x)
	print()


from ctypes import pointer
mapInput = lambda : map(int, input().split())
listInput = lambda : list(mapInput())

class Point:

	def __init__(self, x, y):
		self.x = x
		self.y = y

	def __add__(self, p):
		return Point(self.x + p.x, self.y + p.y)

	def __eq__(self, p):
		return self.x == p.x and self.y == p.y

	def isValid(self):
		return self.x >= 1 and self.x <= 8 and (self.y >= 1) and (self.y <= 8)
offsets = [Point(1, 2), Point(-1, 2), Point(1, -2), Point(-1, -2), Point(2, 1), Point(-2, 1), Point(2, -1), Point(-2, -1)]
for _ in range(int(input())):
	(y, x) = mapInput()
	kingPos = Point(x, y)
	queenPos = []
	if x == 1 and y == 1 or (x == 1 and y == 8) or (x == 8 and y == 1) or (x == 8 and y == 8):
		for offset in offsets:
			if (kingPos + offset).isValid():
				queenPos.append(kingPos + offset)
				break
	elif x == 1 or y == 1 or x == 8 or (y == 8):
		if (kingPos + offsets[0]).isValid() and (kingPos + offsets[1]).isValid():
			queenPos.append(kingPos + offsets[0])
			queenPos.append(kingPos + offsets[1])
		elif (kingPos + offsets[2]).isValid() and (kingPos + offsets[3]).isValid():
			queenPos.append(kingPos + offsets[2])
			queenPos.append(kingPos + offsets[3])
		elif (kingPos + offsets[4]).isValid() and (kingPos + offsets[6]).isValid():
			queenPos.append(kingPos + offsets[4])
			queenPos.append(kingPos + offsets[6])
		else:
			queenPos.append(kingPos + offsets[5])
			queenPos.append(kingPos + offsets[7])
	elif x == 2 and y == 2 or (x == 2 and y == 7) or (x == 7 and y == 2) or (x == 7 and y == 7):
		if x == 2 and y == 2:
			queenPos.append(kingPos + Point(2, -1))
			queenPos.append(kingPos + Point(-1, 3))
		elif x == 2 and y == 7:
			queenPos.append(kingPos + Point(-1, -2))
			queenPos.append(kingPos + Point(3, 1))
		elif x == 7 and y == 2:
			queenPos.append(kingPos + Point(-3, -1))
			queenPos.append(kingPos + Point(1, 2))
		else:
			queenPos.append(kingPos + Point(-2, 1))
			queenPos.append(kingPos + Point(1, -3))
	elif (kingPos + offsets[0]).isValid() and (kingPos + offsets[3]).isValid():
		queenPos.append(kingPos + offsets[0])
		queenPos.append(kingPos + offsets[3])
	else:
		queenPos.append(kingPos + offsets[4])
		queenPos.append(kingPos + offsets[7])
	outputString = ''
	for y in range(1, 9):
		for x in range(1, 9):
			if Point(x, y) == kingPos:
				outputString += '1'
			elif Point(x, y) in queenPos:
				outputString += '2'
			else:
				outputString += '0'
			if x != 8:
				outputString += ' '
		outputString += '\n'
	print(outputString)


t = int(input())
for k in range(t):
	rc = list(map(int, input().split()))
	r = rc[0] - 1
	c = rc[1] - 1
	corner = [0, 7]
	adjcor = [1, 6]
	matrix = []
	for i in range(8):
		a = []
		for j in range(8):
			a.append(0)
		matrix.append(a)
	matrix[r][c] = 1
	if r in corner and c in corner:
		if r <= 4:
			r = r + 1
		if r > 4:
			r = r - 1
		if c <= 4:
			c = c + 2
		if c > 4:
			c = c - 2
		matrix[r][c] = 2
	elif r in adjcor and c in adjcor:
		if r == 1:
			matrix[0][4] = 2
			if c == 1:
				matrix[3][0] = 2
			else:
				matrix[4][7] = 2
		else:
			matrix[7][3] = 2
			if c == 1:
				matrix[3][0] = 2
			else:
				matrix[4][7] = 2
	elif r == 0:
		matrix[2][c + 1] = 2
		matrix[2][c - 1] = 2
	elif c == 0:
		matrix[r + 1][2] = 2
		matrix[r - 1][2] = 2
	elif r == 7:
		matrix[5][c + 1] = 2
		matrix[5][c - 1] = 2
	elif c == 7:
		matrix[r + 1][5] = 2
		matrix[r - 1][5] = 2
	elif c == 1 or c == 6:
		matrix[r - 2][c - 1] = 2
		matrix[r + 2][c + 1] = 2
	elif r >= 1 and r <= 6:
		matrix[r - 1][c - 2] = 2
		matrix[r + 1][c + 2] = 2
	for i in range(8):
		for j in range(8):
			print(matrix[i][j], end=' ')
		print()
	print()


t = int(input())
for _ in range(t):
	(x, y) = map(int, input().split())
	(x, y) = (x - 1, y - 1)
	B = [[0 for i in range(8)] for j in range(8)]
	B[x][y] = 1
	if x == 0:
		if y == 0:
			B[1][2] = 2
		elif y == 7:
			B[1][5] = 2
		else:
			B[x + 2][y - 1] = 2
			B[x + 2][y + 1] = 2
	elif x == 7:
		if y == 0:
			B[6][2] = 2
		elif y == 7:
			B[6][5] = 2
		else:
			B[x - 2][y - 1] = 2
			B[x - 2][y + 1] = 2
	elif y == 0:
		B[x + 1][y + 2] = 2
		B[x - 1][y + 2] = 2
	elif y == 7:
		B[x - 1][y - 2] = 2
		B[x + 1][y - 2] = 2
	elif x == 1:
		if y == 1:
			B[x - 1][y + 3] = 2
			B[x + 2][y - 1] = 2
		elif y == 6:
			B[x - 1][y - 2] = 2
			B[x + 3][y + 1] = 2
		else:
			B[x - 1][y - 2] = 2
			B[x + 1][y + 2] = 2
	elif x == 6:
		if y == 1:
			B[x + 1][y + 3] = 2
			B[x - 2][y - 1] = 2
		elif y == 6:
			B[x + 1][y - 3] = 2
			B[x - 2][y + 1] = 2
		else:
			B[x - 1][y - 2] = 2
			B[x + 1][y + 2] = 2
	elif y == 1:
		B[x - 2][y - 1] = 2
		B[x + 2][y + 1] = 2
	elif y == 6:
		B[x - 2][y + 1] = 2
		B[x + 2][y - 1] = 2
	else:
		B[x - 1][y - 2] = 2
		B[x + 1][y + 2] = 2
	for i in range(8):
		print(*B[i])


for i in range(int(input())):
	(a, b) = map(int, input().split(' '))
	a -= 1
	b -= 1
	c = [[0] * 8 for _ in range(8)]
	c[a][b] = 1
	if a == 1 and b == 1:
		c[a - 1][b + 3] = 2
		c[a + 2][b - 1] = 2
	elif a == 1 and b == 6:
		c[a - 1][b - 2] = 2
		c[a + 3][b + 1] = 2
	elif a == 6 and b == 1:
		c[a + 1][b + 3] = 2
		c[a - 2][b - 1] = 2
	elif a == 6 and b == 6:
		c[a + 1][b - 3] = 2
		c[a - 2][b + 1] = 2
	elif a == 0 and b == 0:
		c[a + 1][b + 2] = 2
	elif a == 0 and b == 7:
		c[a + 1][b - 2] = 2
	elif a == 7 and b == 0:
		c[a - 1][b + 2] = 2
	elif a == 7 and b == 7:
		c[a - 1][b - 2] = 2
	elif a == 0:
		c[a + 2][b + 1] = 2
		c[a + 2][b - 1] = 2
	elif b == 0:
		c[a + 1][b + 2] = 2
		c[a - 1][b + 2] = 2
	elif a == 7:
		c[a - 2][b + 1] = 2
		c[a - 2][b - 1] = 2
	elif b == 7:
		c[a + 1][b - 2] = 2
		c[a - 1][b - 2] = 2
	elif a == 1:
		c[a - 1][b - 2] = 2
		c[a + 1][b + 2] = 2
	elif a == 6:
		c[a + 1][b - 2] = 2
		c[a - 1][b + 2] = 2
	else:
		c[a + 2][b - 1] = 2
		c[a - 2][b + 1] = 2
	for j in c:
		for k in j:
			print(k, end=' ')
		print()


def print_chessboard(chessboard):
	for i in range(8):
		for j in range(8):
			print(chessboard[i][j], end=' ')
		print()

def minqueens(chessboard, a, b):
	if a == 1 and b == 1:
		chessboard[1][2] = 2
		return
	if a == 1 and b == 8:
		chessboard[1][5] = 2
		return
	if a == 8 and b == 1:
		chessboard[6][2] = 2
		return
	if a == 8 and b == 8:
		chessboard[6][5] = 2
		return
	if a == 1:
		chessboard[2][b] = 2
		chessboard[2][b - 2] = 2
		return
	if a == 8:
		chessboard[5][b] = 2
		chessboard[5][b - 2] = 2
		return
	if b == 1:
		chessboard[a][2] = 2
		chessboard[a - 2][2] = 2
		return
	if b == 8:
		chessboard[a][5] = 2
		chessboard[a - 2][5] = 2
		return
	if a == 7 and b == 7:
		chessboard[7][4] = 2
		chessboard[5][4] = 2
		chessboard[4][7] = 2
		return
	if b == 7:
		chessboard[a][b - 3] = 2
		chessboard[a - 2][b - 3] = 2
		chessboard[a + 1][b] = 2
		return
	if a == 7:
		chessboard[a][b + 1] = 2
		chessboard[a - 2][b + 1] = 2
		chessboard[4][b - 2] = 2
		return
	chessboard[a][b + 1] = 2
	chessboard[a - 2][b + 1] = 2
	chessboard[a + 1][b - 2] = 2
	return
for _ in range(int(input())):
	(a, b) = list(map(int, input().split()))
	B = [[0 for i in range(8)] for j in range(8)]
	B[a - 1][b - 1] = 1
	if a == 1 and b == 1:
		B[1][2] = 2
	elif a == 1 and b == 8:
		B[1][5] = 2
	elif a == 8 and b == 1:
		B[6][2] = 2
	elif a == 8 and b == 8:
		B[6][5] = 2
	elif a == 1:
		B[a + 1][b - 2] = 2
		B[a + 1][b] = 2
	elif a == 8:
		B[a - 3][b - 2] = 2
		B[a - 3][b] = 2
	elif b == 1:
		B[a][b + 1] = 2
		B[a - 2][b + 1] = 2
	elif b == 8:
		B[a - 2][b - 3] = 2
		B[a][b - 3] = 2
	elif a == 2 and b == 2:
		B[a - 2][b + 2] = 2
		B[a + 1][b - 2] = 2
	elif a == 2 and b == 7:
		B[a - 2][b - 3] = 2
		B[a + 2][b] = 2
	elif a == 7 and b == 2:
		B[a][b + 2] = 2
		B[a - 3][b - 2] = 2
	elif a == 7 and b == 7:
		B[a][b - 4] = 2
		B[a - 3][b] = 2
	elif a == 2:
		B[a - 2][b - 3] = 2
		B[a][b + 1] = 2
	elif a == 7:
		B[a - 2][b - 3] = 2
		B[a][b + 1] = 2
	elif b == 2:
		B[a - 3][b - 2] = 2
		B[a + 1][b] = 2
	elif b == 7:
		B[a - 3][b] = 2
		B[a + 1][b - 2] = 2
	else:
		B[a - 2][b - 3] = 2
		B[a][b + 1] = 2
	for i in range(8):
		print(*B[i])


test = int(input())
for t in range(test):
	(a, b) = map(int, input().split())
	B = [[0 for i in range(8)] for j in range(8)]
	B[a - 1][b - 1] = 1
	if a == 1 and b == 1:
		B[1][2] = 2
	elif a == 1 and b == 8:
		B[1][5] = 2
	elif a == 8 and b == 1:
		B[6][2] = 2
	elif a == 8 and b == 8:
		B[6][5] = 2
	elif a == 1:
		B[a + 1][b - 2] = 2
		B[a + 1][b] = 2
	elif a == 8:
		B[a - 3][b - 2] = 2
		B[a - 3][b] = 2
	elif b == 1:
		B[a][b + 1] = 2
		B[a - 2][b + 1] = 2
	elif b == 8:
		B[a - 2][b - 3] = 2
		B[a][b - 3] = 2
	elif a == 2 and b == 2:
		B[a - 2][b + 2] = 2
		B[a + 1][b - 2] = 2
	elif a == 2 and b == 7:
		B[a - 2][b - 3] = 2
		B[a + 2][b] = 2
	elif a == 7 and b == 2:
		B[a][b + 2] = 2
		B[a - 3][b - 2] = 2
	elif a == 7 and b == 7:
		B[a][b - 4] = 2
		B[a - 3][b] = 2
	elif a == 2:
		B[a - 2][b - 3] = 2
		B[a][b + 1] = 2
	elif a == 7:
		B[a - 2][b - 3] = 2
		B[a][b + 1] = 2
	elif b == 2:
		B[a - 3][b - 2] = 2
		B[a + 1][b] = 2
	elif b == 7:
		B[a - 3][b] = 2
		B[a + 1][b - 2] = 2
	else:
		B[a - 2][b - 3] = 2
		B[a][b + 1] = 2
	for i in range(8):
		print(*B[i])


def innercircle(x, y):
	x2 = -1
	y2 = -1
	if (x == 2 or x == 7) and (y > 2 and y < 7):
		x1 = x - 1
		y1 = y - 2
		x2 = x + 1
		y2 = y + 2
	elif x > 2 and x < 7 and (y >= 2) and (y <= 7):
		x1 = x - 2
		y1 = y - 1
		x2 = x + 2
		y2 = y + 1
	elif x == 1 and y > 1 and (y < 8):
		x1 = x + 2
		y1 = y - 1
		x2 = x + 3
		y2 = y + 1
	elif x == 8 and (y > 1 and y < 8):
		x1 = x - 2
		y1 = y - 1
		x2 = x - 3
		y2 = y + 1
	elif y == 1 and (x > 1 and x < 8):
		x1 = x - 1
		y1 = y + 2
		x2 = x + 1
		y2 = y + 3
	elif y == 8 and (x > 1 and x < 8):
		x1 = x - 1
		y1 = y - 2
		x2 = x + 1
		y2 = y - 3
	elif x == 1 and y == 1:
		x1 = 3
		y1 = 2
	elif x == 1 and y == 8:
		x1 = 3
		y1 = 7
	elif x == 8 and y == 1:
		x1 = 6
		y1 = 2
	elif x == 8 and y == 8:
		x1 = 6
		y1 = 7
	elif x == 2 and y == 2:
		x1 = 1
		y1 = 4
		x2 = 5
		y2 = 1
	elif x == 2 and y == 7:
		x1 = 1
		y1 = 5
		x2 = 5
		y2 = 8
	elif x == 7 and y == 2:
		x1 = 5
		y1 = 1
		x2 = 8
		y2 = 5
	elif x == 7 and y == 7:
		x1 = 4
		y1 = 8
		x2 = 8
		y2 = 5
	for i in range(1, 9):
		print()
		for j in range(1, 9):
			if i == x1:
				if j == y1:
					print(2, end=' ')
				else:
					print(0, end=' ')
			elif i == x2:
				if j == y2:
					print(2, end=' ')
				else:
					print(0, end=' ')
			elif i == x:
				if j == y:
					print(1, end=' ')
				else:
					print(0, end=' ')
			else:
				print(0, end=' ')
for i in range(int(input())):
	(a, b) = map(int, input().split())
	innercircle(a, b)


T = int(input())
board = [[0 for i in range(8)] for j in range(8)]
for _ in range(T):
	ans = 0
	(x, y) = map(int, input().split())
	board = [[0 for i in range(8)] for j in range(8)]
	x -= 1
	y -= 1
	board[x][y] = 1
	queenx = 0
	queeny = 0
	if x == 0 and y == 0 or (x == 0 and y == 7) or (x == 7 and y == 0) or (x == 7 and y == 7):
		ans = 1
		if x == 0 and y == 0:
			board[1][2] = 2
		if x == 0 and y == 7:
			board[1][5] = 2
		if x == 7 and y == 0:
			board[6][2] = 2
		if x == 7 and y == 7:
			board[6][5] = 2
	elif x == 0 or x == 7 or y == 0 or (y == 7):
		ans = 2
		if x == 0:
			board[2][y - 1] = 2
			board[2][y + 1] = 2
		if x == 7:
			board[5][y - 1] = 2
			board[5][y + 1] = 2
		if y == 0:
			board[x + 1][2] = 2
			board[x - 1][2] = 2
		if y == 7:
			board[x + 1][5] = 2
			board[x - 1][5] = 2
	else:
		ans = 2
		if x - 3 >= 0:
			if y - 2 >= 0:
				board[x + 1][y - 2] = 2
				board[x - 3][y + 1] = 2
			else:
				board[x + 1][y + 2] = 2
				board[x - 3][y - 1] = 2
		elif y - 2 >= 0:
			board[x - 1][y - 2] = 2
			board[x + 3][y + 1] = 2
		else:
			board[x - 1][y + 2] = 2
			board[x + 3][y - 1] = 2
	for i in range(8):
		for j in range(8):
			print(board[i][j], end=' ')
		print('')


def rotate_point(r, c):
	return (c, 8 - r - 1)

def rotate_grid(grid):
	r_grid = [[grid[r][c] for c in range(8)] for r in range(8)]
	for r in range(8):
		for c in range(8):
			(r2, c2) = rotate_point(r, c)
			r_grid[r2][c2] = grid[r][c]
	return r_grid

def get_queens(r, c):
	if r == 0 and c == 0:
		return [(1, 2)]
	elif r == 0:
		return [(2, c - 1), (2, c + 1)]
	elif c == 0:
		return [(r - 1, 2), (r + 1, 2)]
	else:
		return [(r + 3, c - 1), (r - 1, c + 2)]

def get_square(r, c):
	if r < 4 and c < 4:
		return 0
	elif r < 4:
		return 1
	elif c >= 4:
		return 2
	return 3

def g(r, c):
	grid = [[0 for i in range(8)] for j in range(8)]
	square = get_square(r, c)
	num_rotations = (4 - square) % 4
	(r2, c2) = (r, c)
	for i in range(num_rotations):
		(r2, c2) = rotate_point(r2, c2)
	grid[r2][c2] = 1
	queens = get_queens(r2, c2)
	for (qr, qc) in queens:
		grid[qr][qc] = 2
	for i in range(square):
		grid = rotate_grid(grid)
	return grid

def print_grid(grid):
	for r in range(len(grid)):
		row = ' '.join(map(str, grid[r]))
		print(row)
t = int(input())
for i in range(t):
	(a, b) = [int(el) for el in input().split(' ')]
	r = a - 1
	c = b - 1
	print_grid(g(r, c))


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def st():
	return input().rstrip('\n')

def lis():
	return list(map(int, input().split()))

def ma():
	return map(int, input().split())

def valid(x, y):
	if x <= 0 or x > 8 or y <= 0 or (y > 8):
		return 0
	return 1
t = inp()
while t:
	t -= 1
	(x, y) = ma()
	res = [[0 for i in range(8)] for j in range(8)]
	res[x - 1][y - 1] = 1
	if x == 1 and y == 1:
		res[2][1] = 2
	elif x == 1 and y == 8:
		res[2][6] = 2
	elif x == 8 and y == 1:
		res[5][1] = 2
	elif x == 8 and y == 8:
		res[5][6] = 2
	elif x in [1, 8] or y in [1, 8]:
		if x == 1:
			res[x + 1][y - 2] = 2
			res[x + 1][y] = 2
		if x == 8:
			res[x - 3][y - 2] = 2
			res[x - 3][y] = 2
		if y == 1:
			res[x - 2][y + 1] = 2
			res[x][y + 1] = 2
		if y == 8:
			res[x - 2][y - 3] = 2
			res[x][y - 3] = 2
	else:
		flag = 0
		for i1 in [-1, 1]:
			for i2 in [-2, 2]:
				for i3 in [-3, 3]:
					for i4 in [-1, 1]:
						(x1, x2) = (x + i1, x + i3)
						(y1, y2) = (y + i2, y + i4)
						if valid(x1, y1) and valid(x2, y2) and (flag == 0) and (i1 > 0 and i3 < 0 or (i1 < 0 and i3 > 0)) and (i2 > 0 and i4 < 0 or (i2 < 0 and i4 > 0)):
							res[x1 - 1][y1 - 1] = 2
							res[x2 - 1][y2 - 1] = 2
							flag = 1
	for i in res:
		print(*i)


T = int(input())
for t in range(T):
	(x, y) = map(int, input().split())
	ans = [[0 for i in range(8)] for j in range(8)]
	extremes = [[1, 1], [8, 8], [8, 1], [1, 8]]
	threes = [[2, 2], [7, 2], [2, 7], [7, 7]]
	ans[x - 1][y - 1] = 1
	if [x, y] in extremes:
		(a, b) = (0, 0)
		if x < 4:
			a = 2
		else:
			a = 5
		if y < 4:
			b = 1
		else:
			b = 6
		ans[a][b] = 2
	elif [x, y] in threes:
		a = b = 0
		if y < 4:
			b = 3
		else:
			b = 4
		if x < 4:
			a = 0
		else:
			a = 7
		ans[a][b] = 2
		if y < 4:
			b = 0
		else:
			b = 7
		if x < 4:
			a = 4
		else:
			a = 3
		ans[a][b] = 2
	elif x == 1 or x == 8 or y == 1 or (y == 8):
		if x == 1:
			ans[2][y - 2] = 2
			ans[2][y] = 2
		elif x == 8:
			ans[5][y - 2] = 2
			ans[5][y] = 2
		elif y == 1:
			ans[x - 2][2] = 2
			ans[x][2] = 2
		else:
			ans[x - 2][5] = 2
			ans[x][5] = 2
	elif y == 2 or y == 7:
		ans[x - 3][y - 2] = 2
		ans[x + 1][y] = 2
	else:
		ans[x - 2][y - 3] = 2
		ans[x][y + 1] = 2
	for i in range(8):
		print(*ans[i])


t = int(input())
for _ in range(t):
	(A, B) = map(int, input().split())
	board = []
	for i in range(8):
		board.append([0] * 8)
	board[A - 1][B - 1] = 1
	if A == 1 and B == 1:
		board[1][2] = 2
	elif A == 1 and B == 8:
		board[1][5] = 2
	elif A == 8 and B == 1:
		board[6][2] = 2
	elif A == 8 and B == 8:
		board[6][5] = 2
	elif A == 8 and 2 <= B <= 7:
		board[A - 3][B - 2] = 2
		board[A - 3][B] = 2
	elif A == 1 and 2 <= B <= 7:
		board[A + 1][B - 2] = 2
		board[A + 1][B] = 2
	elif 2 <= A <= 7 and B == 1:
		board[A - 2][B + 1] = 2
		board[A][B + 1] = 2
	elif 2 <= A <= 7 and B == 8:
		board[A - 2][B - 3] = 2
		board[A][B - 3] = 2
	elif 2 <= A <= 4 and 2 <= B <= 4:
		board[A + 1][B - 2] = 2
		board[A - 2][B + 2] = 2
	elif 2 <= A <= 4 and 5 <= B <= 7:
		board[A + 1][B] = 2
		board[A - 2][B - 4] = 2
	elif 5 <= A <= 7 and 2 <= B <= 4:
		board[A - 3][B - 2] = 2
		board[A][B + 2] = 2
	elif 5 <= A <= 7 and 5 <= B <= 7:
		board[A][B - 4] = 2
		board[A - 3][B] = 2
	for row in board:
		for sqr in row:
			print(sqr, end=' ')
		print()


from math import ceil, floor

def valid(x, y):
	if x <= 0 or x > 8 or y <= 0 or (y > 8):
		return 0
	return 1

def ma():
	return map(int, input().split())

def solve2():
	(x, y) = ma()
	res = [[0 for i in range(8)] for j in range(8)]
	res[x - 1][y - 1] = 1
	if x == 1 and y == 1:
		res[2][1] = 2
	elif x == 1 and y == 8:
		res[2][6] = 2
	elif x == 8 and y == 1:
		res[5][1] = 2
	elif x == 8 and y == 8:
		res[5][6] = 2
	elif x in [1, 8] or y in [1, 8]:
		if x == 1:
			res[x + 1][y - 2] = 2
			res[x + 1][y] = 2
		if x == 8:
			res[x - 3][y - 2] = 2
			res[x - 3][y] = 2
		if y == 1:
			res[x - 2][y + 1] = 2
			res[x][y + 1] = 2
		if y == 8:
			res[x - 2][y - 3] = 2
			res[x][y - 3] = 2
	else:
		f = 0
		for i in [-1, 1]:
			for j in [-2, 2]:
				for k in [-3, 3]:
					for l in [-1, 1]:
						(x1, x2, y1, y2) = (x + i, x + k, y + j, y + l)
						if valid(x1, y1) and valid(x2, y2) and (f == 0) and (i > 0 and k < 0 or (i < 0 and k > 0)) and (j > 0 and l < 0 or (j < 0 and l > 0)):
							res[x1 - 1][y1 - 1] = 2
							res[x2 - 1][y2 - 1] = 2
							f = 1
	for i in res:
		print(*i)

def solve1():
	for _ in range(int(input())):
		solve2()
solve1()


t = int(input())
for i in range(0, t):
	(a, b) = map(int, input().split(' '))
	a = a - 1
	b = b - 1
	L = [['0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '0']]
	L[a][b] = '1'
	if a == 0 and b == 0:
		L[a + 2][b + 1] = '2'
	elif a == 7 and b == 0:
		L[a - 2][b + 1] = '2'
	elif a == 0 and b == 7:
		L[a + 2][b - 1] = '2'
	elif a == 7 and b == 7:
		L[a - 2][b - 1] = '2'
	elif a < 7 and b == 0:
		L[a + 1][b + 2] = '2'
		L[a - 1][b + 2] = '2'
	elif a == 0 and b < 7:
		L[a + 2][b + 1] = '2'
		L[a + 2][b - 1] = '2'
	elif a < 7 and b == 7:
		L[a + 1][b - 2] = '2'
		L[a - 1][b - 2] = '2'
	elif a == 7 and b < 7:
		L[a - 2][b + 1] = '2'
		L[a - 2][b - 1] = '2'
	elif a <= 3 and b <= 3:
		L[a + 3][b - 1] = '2'
		L[a - 1][b + 2] = '2'
	elif a > 3 and b <= 3:
		L[a - 3][b - 1] = '2'
		L[a + 1][b + 2] = '2'
	elif a <= 3 and b > 3:
		L[a + 3][b + 1] = '2'
		L[a - 1][b - 2] = '2'
	elif a > 3 and b > 3:
		L[a - 3][b + 1] = '2'
		L[a + 1][b - 2] = '2'
	for j in range(0, 8):
		print(' '.join(L[j]))


for Queen in range(int(input())):
	(x, y) = map(int, input().split())
	(x, y) = (x - 1, y - 1)
	l = []
	for i in range(8):
		l.append([0] * 8)
	l[x][y] = 1
	if x == 0 and y == 0:
		l[2][1] = 2
	elif x == 7 and y == 0:
		l[6][2] = 2
	elif x == 0 and y == 7:
		l[2][6] = 2
	elif x == 7 and y == 7:
		l[6][5] = 2
	elif x == 0:
		l[2][y + 1] = 2
		l[2][y - 1] = 2
	elif y == 0:
		l[x + 1][2] = 2
		l[x - 1][2] = 2
	elif x == 7:
		l[5][y + 1] = 2
		l[5][y - 1] = 2
	elif y == 7:
		l[x + 1][5] = 2
		l[x - 1][5] = 2
	elif x > 2 and y > 2:
		l[x + 1][y - 2] = 2
		l[x - 3][y + 1] = 2
	elif x < 3 and y < 3:
		l[x - 1][y + 2] = 2
		l[x + 3][y - 1] = 2
	elif y > 2 and x < 3:
		l[x - 1][y - 2] = 2
		l[x + 3][y + 1] = 2
	elif y < 3 and x > 2:
		l[x + 1][y + 2] = 2
		l[x - 3][y - 1] = 2
	for i in l:
		print(*i)


for t in range(int(input())):
	(a, b) = map(int, input().split())
	arr = [[0] * 8 for i in range(8)]
	arr[a - 1][b - 1] = 1
	if a in (1, 8) and b in (1, 8):
		a = a - 1 if a == 8 else a + 1
		b = b - 2 if b == 8 else b + 2
		arr[a - 1][b - 1] = 2
	elif a == 1:
		arr[a + 1][b - 2] = 2
		arr[a + 1][b] = 2
	elif a == 8:
		arr[a - 3][b - 2] = 2
		arr[a - 3][b] = 2
	elif b == 1:
		arr[a][b + 1] = 2
		arr[a - 2][b + 1] = 2
	elif b == 8:
		arr[a - 2][b - 3] = 2
		arr[a][b - 3] = 2
	elif a == 2 and b == 2:
		arr[a - 2][b + 2] = 2
		arr[a + 1][b - 2] = 2
	elif a == 2 and b == 7:
		arr[a - 2][b - 3] = 2
		arr[a + 2][b] = 2
	elif a == 7 and b == 2:
		arr[a][b + 2] = 2
		arr[a - 3][b - 2] = 2
	elif a == 7 and b == 7:
		arr[a][b - 4] = 2
		arr[a - 3][b] = 2
	elif a == 2:
		arr[a - 2][b - 3] = 2
		arr[a][b + 1] = 2
	elif a == 7:
		arr[a - 2][b - 3] = 2
		arr[a][b + 1] = 2
	elif b == 2:
		arr[a - 3][b - 2] = 2
		arr[a + 1][b] = 2
	elif b == 7:
		arr[a - 3][b] = 2
		arr[a + 1][b - 2] = 2
	else:
		arr[a - 2][b - 3] = 2
		arr[a][b + 1] = 2
	for r in arr:
		print(*r)


t = int(input())
for j in range(t):
	(a, b) = map(int, input().split())
	l = []
	for k in range(8):
		ans = []
		for p in range(8):
			ans.append(0)
		l.append(ans)
	a = a - 1
	b = b - 1
	l[a][b] = 1
	if a == 0 and b == 0:
		l[2][1] = 2
	elif a == 7 and b == 7:
		l[5][6] = 2
	elif a == 7 and b == 0:
		l[5][1] = 2
	elif a == 0 and b == 7:
		l[2][6] = 2
	elif a == 0:
		l[2][b - 1] = 2
		l[2][b + 1] = 2
	elif a == 7:
		l[5][b - 1] = 2
		l[5][b + 1] = 2
	elif b == 0:
		l[a + 1][2] = 2
		l[a - 1][2] = 2
	elif b == 7:
		l[a - 1][5] = 2
		l[a + 1][5] = 2
	elif a > 3 and b > 3:
		l[a + 1][b - 2] = 2
		l[a - 3][b + 1] = 2
	elif b < 4 and a < 4:
		l[a - 1][b + 2] = 2
		l[a + 3][b - 1] = 2
	elif b > 3 and a < 4:
		l[a - 1][b - 2] = 2
		l[a + 3][b + 1] = 2
	elif b < 4 and a > 3:
		l[a + 1][b + 2] = 2
		l[a - 3][b - 1] = 2
	for p in range(8):
		for k in range(8):
			print(l[p][k], end=' ')
		print()


def chess(x, y):
	mat = [[0 for i in range(9)] for j in range(9)]
	mat[x][y] = 1
	if x == 1 and y == 1:
		mat[x + 2][y + 1] = 2
	elif x == 1 and y == 8:
		mat[x + 2][y - 1] = 2
	elif x == 8 and y == 8:
		mat[x - 2][y - 1] = 2
	elif x == 8 and y == 1:
		mat[x - 2][y + 1] = 2
	elif x in [1, 8] or y in [1, 8]:
		if x == 1:
			mat[x + 2][y - 1] = 2
			mat[x + 2][y + 1] = 2
		elif x == 8:
			mat[x - 2][y - 1] = 2
			mat[x - 2][y + 1] = 2
		elif y == 1:
			mat[x + 1][y + 2] = 2
			mat[x - 1][y + 2] = 2
		else:
			mat[x + 1][y - 2] = 2
			mat[x - 1][y - 2] = 2
	elif x in [2, 7] and y in [2, 7]:
		if x == 2 and y == 2:
			mat[x - 1][y + 2] = 2
			mat[x + 3][y - 1] = 2
		elif x == 2 and y == 7:
			mat[x - 1][y - 2] = 2
			mat[x + 3][y + 1] = 2
		elif x == 7 and y == 2:
			mat[x + 1][y + 2] = 2
			mat[x - 3][y - 1] = 2
		elif x == 7 and y == 7:
			mat[x - 2][y + 1] = 2
			mat[x + 1][y - 3] = 2
	elif x - 1 > 0 and y - 2 > 0 and (x + 1 < 9) and (y + 2 < 9):
		mat[x - 1][y - 2] = 2
		mat[x + 1][y + 2] = 2
	else:
		mat[x - 2][y - 1] = 2
		mat[x + 2][y + 1] = 2
	for i in range(1, 9):
		for j in range(1, 9):
			print(mat[i][j], end=' ')
		print('', end='\n')
t = int(input())
for i in range(t):
	(x, y) = map(int, input().split())
	chess(x, y)


def valid(x, y):
	return 0 if x <= 0 or x > 8 or y <= 0 or (y > 8) else 1
for _ in range(int(input())):
	(x, y) = map(int, input().split())
	res = [[0 for i in range(8)] for j in range(8)]
	res[x - 1][y - 1] = 1
	if x == 1 and y == 1:
		res[2][1] = 2
	elif x == 1 and y == 8:
		res[2][6] = 2
	elif x == 8 and y == 1:
		res[5][1] = 2
	elif x == 8 and y == 8:
		res[5][6] = 2
	elif x in [1, 8] or y in [1, 8]:
		if x == 1:
			res[x + 1][y - 2] = 2
			res[x + 1][y] = 2
		if x == 8:
			res[x - 3][y - 2] = 2
			res[x - 3][y] = 2
		if y == 1:
			res[x - 2][y + 1] = 2
			res[x][y + 1] = 2
		if y == 8:
			res[x - 2][y - 3] = 2
			res[x][y - 3] = 2
	else:
		flag = 0
		for i1 in [-1, 1]:
			for i2 in [-2, 2]:
				for i3 in [-3, 3]:
					for i4 in [-1, 1]:
						(x1, x2) = (x + i1, x + i3)
						(y1, y2) = (y + i2, y + i4)
						if valid(x1, y1) and valid(x2, y2) and (flag == 0) and (i1 > 0 and i3 < 0 or (i1 < 0 and i3 > 0)) and (i2 > 0 and i4 < 0 or (i2 < 0 and i4 > 0)):
							res[x1 - 1][y1 - 1] = 2
							res[x2 - 1][y2 - 1] = 2
							flag = 1
	for i in res:
		print(*i)


def main():
	for _ in range(int(input())):
		(x, y) = map(int, input().split(' '))
		lst = [[0] * 8 for _ in range(8)]
		lst[x - 1][y - 1] = 1
		x -= 1
		y -= 1
		if x == 0 and y == 0:
			lst[2][1] = 2
		elif x == 7 and y == 0:
			lst[5][1] = 2
		elif x == 7 and y == 7:
			lst[5][6] = 2
		elif x == 0 and y == 7:
			lst[2][6] = 2
		elif y == 0:
			lst[x - 1][2] = 2
			lst[x + 1][2] = 2
		elif x == 7:
			lst[5][y - 1] = 2
			lst[5][y + 1] = 2
		elif y == 7:
			lst[x - 1][5] = 2
			lst[x + 1][5] = 2
		elif x == 0:
			lst[2][y - 1] = 2
			lst[2][y + 1] = 2
		elif y == 1 and x < 4:
			lst[x - 1][3] = 2
			lst[x + 3][0] = 2
		elif y == 1 and x >= 4:
			lst[x + 1][3] = 2
			lst[x - 3][0] = 2
		elif x == 6 and y < 4:
			lst[x - 2][y - 1] = 2
			lst[x + 1][y + 3] = 2
		elif x == 6 and y >= 4:
			lst[x - 2][y + 1] = 2
			lst[x + 1][y - 3] = 2
		elif y == 6 and x < 4:
			lst[x + 2][y + 1] = 2
			lst[x - 1][y - 3] = 2
		elif y == 6 and x >= 4:
			lst[x - 2][y + 1] = 2
			lst[x + 1][y - 3] = 2
		elif x == 1 and y < 4:
			lst[x + 3][y - 1] = 2
			lst[x - 1][y + 2] = 2
		elif x == 1 and y >= 4:
			lst[x - 1][y - 3] = 2
			lst[x + 2][y + 1] = 2
		elif x <= 3:
			lst[x - 1][y + 2] = 2
			lst[x + 3][y - 1] = 2
		elif x > 3:
			lst[x + 1][y + 2] = 2
			lst[x - 3][y - 1] = 2
		for i in range(8):
			for j in range(8):
				print(lst[i][j], end=' ')
			print()
main()


t = int(input())
for _ in range(t):
	(a, b) = map(int, input().split())
	temp = [[i for i in range(8)] for j in range(8)]
	for i in range(8):
		for j in range(8):
			temp[i][j] = 0
	temp[a - 1][b - 1] = 1
	if a == 1 and b == 1:
		temp[1][2] = 2
	elif a == 8 and b == 1:
		temp[6][2] = 2
	elif a == 1 and b == 8:
		temp[1][5] = 2
	elif a == 8 and b == 8:
		temp[6][5] = 2
	elif a == 1:
		temp[2][b - 2] = 2
		temp[2][b] = 2
	elif a == 8:
		temp[5][b - 2] = 2
		temp[5][b] = 2
	elif b == 1:
		temp[a - 2][b + 1] = 2
		temp[a][b + 1] = 2
	elif b == 8:
		temp[a - 2][b - 3] = 2
		temp[a][b - 3] = 2
	elif a == 2 and b == 2:
		temp[0][4] = 2
		temp[3][0] = 2
	elif a == 7 and b == 2:
		temp[7][4] = 2
		temp[4][0] = 2
	elif a == 2 and b == 7:
		temp[0][4] = 2
		temp[4][7] = 2
	elif a == 7 and b == 7:
		temp[4][7] = 2
		temp[7][3] = 2
	elif a == 2:
		temp[a][b + 1] = 2
		temp[a - 2][b - 3] = 2
	elif a == 7:
		temp[a][b + 1] = 2
		temp[a - 2][b - 3] = 2
	elif b == 2:
		temp[a - 3][b - 2] = 2
		temp[a + 1][b] = 2
	elif b == 7:
		temp[a - 3][b] = 2
		temp[a + 1][b - 2] = 2
	else:
		temp[a - 2][b - 3] = 2
		temp[a][b + 1] = 2
	for i in range(8):
		for j in range(8):
			print(temp[i][j], end=' ')
		print()


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	c = None
	d = None
	if a == 1 or a == 8:
		c = (2 if a == 1 else 7, b + 2 if b + 2 < 9 else b - 2)
		d = (3 if a == 1 else 6, b - 1 if 2 <= b <= 7 else 0)
	elif b == 1 or b == 8:
		c = (a + 2 if a + 2 < 9 else a - 2, 2 if b == 1 else 7)
		d = (a + 1 if 2 <= a <= 7 else 0, 3 if b == 1 else 6)
	elif a == 2 or a == 7:
		c = (1 if a == 2 else 8, b + 2 if b + 2 < 9 else b - 2)
		d = (a + 3 if a == 2 else a - 3, b - 1 if b < 7 else 8)
	elif b == 2 or b == 7:
		c = (a - 2, 1 if b == 2 else 8)
		d = (a + 1, b + 3 if b == 2 else b - 3)
	else:
		c = (a - 1 if a == 3 else a + 1, b + 2)
		d = (a + 3 if a == 3 else a - 3, b - 1)
	for i in range(1, 9):
		for j in range(1, 9):
			if i == a and j == b:
				print('1', end=' ')
			elif (i, j) == c or (i, j) == d:
				print('2', end=' ')
			else:
				print('0', end=' ')
		print()


from collections import defaultdict
import sys

def solve():
	(a, b) = map(int, input().split())
	(a, b) = (a - 1, b - 1)
	hs = defaultdict(lambda : defaultdict(int))
	hs[a][b] = 1
	ans = []
	if b == 0:
		if a == 0:
			hs[a + 1][b + 2] = 2
		elif a == 7:
			hs[a - 1][b + 2] = 2
		else:
			hs[a - 1][b + 2] = 2
			hs[a + 1][b + 2] = 2
	elif b == 7:
		if a == 0:
			hs[a + 1][b - 2] = 2
		elif a == 7:
			hs[a - 1][b - 2] = 2
		else:
			hs[a - 1][b - 2] = 2
			hs[a + 1][b - 2] = 2
	elif a == 0:
		if b == 0:
			hs[a + 2][b + 1] = 2
		elif b == 7:
			hs[a + 2][b - 1] = 2
		else:
			hs[a + 2][b - 1] = 2
			hs[a + 2][b + 1] = 2
	elif a == 7:
		if b == 0:
			hs[a - 2][b + 1] = 2
		elif b == 7:
			hs[a - 2][b - 1] = 2
		else:
			hs[a - 2][b - 1] = 2
			hs[a - 2][b + 1] = 2
	elif a == 1 and b == 1:
		hs[a - 1][b + 3] = 2
		hs[a + 2][b - 1] = 2
	elif a == 6 and b == 1:
		hs[a + 1][b + 3] = 2
		hs[a - 2][b - 1] = 2
	elif a == 1 and b == 6:
		hs[a - 1][b - 3] = 2
		hs[a + 2][b + 1] = 2
	elif a == 6 and b == 6:
		hs[a + 1][b - 3] = 2
		hs[a - 2][b + 1] = 2
	elif b - 2 >= 0 and b + 2 <= 7:
		hs[a - 1][b + 2] = 2
		hs[a + 1][b - 2] = 2
	else:
		hs[a - 2][b + 1] = 2
		hs[a + 2][b - 1] = 2
	for x in range(0, 8):
		A = [hs[x][i] for i in range(0, 8)]
		print(' '.join(map(str, A)))
for _ in range(int(input())):
	solve()


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	mat = [[0 for _ in range(8)] for _ in range(8)]
	mat[a - 1][b - 1] = 1
	set1 = {(0, 0), (0, 7), (7, 0), (7, 7)}
	set2 = {(1, 1), (6, 1), (6, 6), (1, 6)}
	if (a - 1, b - 1) in set1:
		mat[abs(a - 1 - 2)][abs(b - 1 - 1)] = 2
	elif (a - 1, b - 1) in set2:
		dict1 = dict()
		dict1[1] = 3
		dict1[6] = 4
		dict3 = dict()
		dict3[1] = 0
		dict3[6] = 7
		r = dict1[a - 1]
		c = dict3[b - 1]
		mat[r][c] = 2
		r = dict3[a - 1]
		dict4 = dict()
		dict4[1] = 4
		dict4[6] = 3
		c = dict4[b - 1]
		mat[r][c] = 2
	elif a - 1 in (0, 7) or b - 1 in (0, 7):
		dict2 = dict()
		dict2[0] = 2
		dict2[7] = 5
		if a - 1 in (0, 7):
			r = dict2[a - 1]
			mat[r][b - 1 - 1] = 2
			mat[r][b - 1 + 1] = 2
		else:
			c = dict2[b - 1]
			mat[a - 1 - 1][c] = 2
			mat[a - 1 + 1][c] = 2
	elif a - 1 in range(2, 6):
		mat[a - 1 - 2][b - 1 - 1] = 2
		mat[a - 1 + 2][b - 1 + 1] = 2
	else:
		mat[a - 1 + 1][b - 1 - 2] = 2
		mat[a - 1 - 1][b - 1 + 2] = 2
	for i in range(8):
		for j in range(8):
			print(mat[i][j], end=' ')
		print()


t = int(input())
while t:
	a = [int(item) for item in input('').split(' ')]
	i = a[0]
	j = a[1]
	lst = [[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]]
	lst[i][j] = 1
	if i == 1 and j == 1 or (i == 1 and j == 8) or (i == 8 and j == 1) or (i == 8 and j == 8):
		if i + 2 >= 1 and i + 2 <= 8:
			if j + 1 >= 1 and j + 1 <= 8:
				lst[i + 2][j + 1] = 2
			elif j - 1 >= 1 and j - 1 <= 8:
				lst[i + 2][j - 1] = 2
		elif i - 2 >= 1 and i - 2 <= 8:
			if j + 1 >= 1 and j + 1 <= 8:
				lst[i - 2][j + 1] = 2
			elif j - 1 >= 1 and j - 1 <= 8:
				lst[i - 2][j - 1] = 2
	elif i == 1:
		if j + 2 >= 1 and j + 2 <= 8:
			lst[i + 1][j + 2] = 2
			lst[i + 2][j - 1] = 2
		elif j - 2 >= 1 and j - 2 <= 8:
			lst[i + 1][j - 2] = 2
			lst[i + 2][j + 1] = 2
	elif i == 8:
		if j + 2 >= 1 and j + 2 <= 8:
			lst[i - 1][j + 2] = 2
			lst[i - 2][j - 1] = 2
		elif j - 2 >= 1 and j - 2 <= 8:
			lst[i - 1][j - 2] = 2
			lst[i - 2][j + 1] = 2
	elif j == 1:
		if i + 2 >= 1 and i + 2 <= 8:
			lst[i + 2][j + 1] = 2
			lst[i - 1][j + 2] = 2
		elif i - 2 >= 1 and i - 2 <= 8:
			lst[i - 2][j + 1] = 2
			lst[i + 1][j - 2] = 2
	elif j == 8:
		if i + 2 >= 1 and i + 2 <= 8:
			lst[i + 2][j - 1] = 2
			lst[i - 1][j - 2] = 2
		elif i - 2 >= 1 and i - 2 <= 8:
			lst[i - 2][j - 1] = 2
			lst[i + 1][j - 2] = 2
	elif i == 2 and j == 2:
		lst[1][5] = 2
		lst[4][1] = 2
	elif i == 2 and j == 7:
		lst[1][4] = 2
		lst[4][8] = 2
	elif i == 7 and j == 2:
		lst[5][1] = 2
		lst[8][5] = 2
	elif i == 7 and j == 7:
		lst[5][8] = 2
		lst[8][4] = 2
	elif i - 2 >= 1 and i - 2 <= 8 and (j + 1 >= 1) and (j + 1 <= 8) and (i + 2 >= 1) and (i + 2 <= 8) and (j - 1 >= 1) and (j - 1 <= 8):
		lst[i - 2][j + 1] = 2
		lst[i + 2][j - 1] = 2
	elif i - 1 >= 1 and i - 1 <= 8 and (j - 2 >= 1) and (j - 2 <= 8) and (i + 1 >= 1) and (i + 1 <= 8) and (j + 2 >= 1) and (j + 2 <= 8):
		lst[i - 1][j - 2] = 2
		lst[i + 1][j + 2] = 2
	for ele in lst[1:]:
		k = 1
		while k < 9:
			print(ele[k], end=' ')
			k = k + 1
		print()
	t = t - 1


from collections import Counter
import math

def solve():
	(x, y) = map(int, input().split(' '))
	lst = [[0] * 8 for _ in range(8)]
	lst[x - 1][y - 1] = 1
	x -= 1
	y -= 1
	if x == 0 and y == 0:
		lst[2][1] = 2
	elif x == 7 and y == 0:
		lst[5][1] = 2
	elif x == 7 and y == 7:
		lst[5][6] = 2
	elif x == 0 and y == 7:
		lst[2][6] = 2
	elif y == 0:
		lst[x - 1][2] = 2
		lst[x + 1][2] = 2
	elif x == 7:
		lst[5][y - 1] = 2
		lst[5][y + 1] = 2
	elif y == 7:
		lst[x - 1][5] = 2
		lst[x + 1][5] = 2
	elif x == 0:
		lst[2][y - 1] = 2
		lst[2][y + 1] = 2
	elif y == 1 and x < 4:
		lst[x - 1][3] = 2
		lst[x + 3][0] = 2
	elif y == 1 and x >= 4:
		lst[x + 1][3] = 2
		lst[x - 3][0] = 2
	elif x == 6 and y < 4:
		lst[x - 2][y - 1] = 2
		lst[x + 1][y + 3] = 2
	elif x == 6 and y >= 4:
		lst[x - 2][y + 1] = 2
		lst[x + 1][y - 3] = 2
	elif y == 6 and x < 4:
		lst[x + 2][y + 1] = 2
		lst[x - 1][y - 3] = 2
	elif y == 6 and x >= 4:
		lst[x - 2][y + 1] = 2
		lst[x + 1][y - 3] = 2
	elif x == 1 and y < 4:
		lst[x + 3][y - 1] = 2
		lst[x - 1][y + 2] = 2
	elif x == 1 and y >= 4:
		lst[x - 1][y - 3] = 2
		lst[x + 2][y + 1] = 2
	elif x <= 3:
		lst[x - 1][y + 2] = 2
		lst[x + 3][y - 1] = 2
	elif x > 3:
		lst[x + 1][y + 2] = 2
		lst[x - 3][y - 1] = 2
	for i in range(8):
		for j in range(8):
			print(lst[i][j], end=' ')
		print()
t = int(input())
for _ in range(t):
	solve()


for _ in range(int(input())):
	(i, j) = [int(x) for x in input().split()]
	grid = [[0 for i in range(8)] for j in range(8)]
	grid[i - 1][j - 1] = 1
	if i == 1 and j == 1:
		grid[2 - 1][3 - 1] = 2
	elif i == 1 and j == 8:
		grid[2 - 1][6 - 1] = 2
	elif i == 8 and j == 1:
		grid[7 - 1][3 - 1] = 2
	elif i == 8 and j == 8:
		grid[7 - 1][6 - 1] = 2
	elif i == 1:
		grid[i + 2 - 1][j - 1 - 1] = 2
		grid[i + 2 - 1][j + 1 - 1] = 2
	elif i == 8:
		grid[i - 2 - 1][j - 1 - 1] = 2
		grid[i - 2 - 1][j + 1 - 1] = 2
	elif j == 1:
		grid[i - 1 - 1][j + 2 - 1] = 2
		grid[i + 1 - 1][j + 2 - 1] = 2
	elif j == 8:
		grid[i - 1 - 1][j - 2 - 1] = 2
		grid[i + 1 - 1][j - 2 - 1] = 2
	elif i == 2 and j == 2:
		grid[i - 1 - 1][j + 3 - 1] = 2
		grid[i + 2 - 1][j - 1 - 1] = 2
	elif i == 2 and j == 7:
		grid[i - 1 - 1][j - 2 - 1] = 2
		grid[i + 3 - 1][j + 1 - 1] = 2
	elif i == 7 and j == 2:
		grid[i + 1 - 1][j + 3 - 1] = 2
		grid[i - 2 - 1][j - 1 - 1] = 2
	elif i == 7 and j == 7:
		grid[i + 1 - 1][j - 3 - 1] = 2
		grid[i - 2 - 1][j + 1 - 1] = 2
	elif i == 2:
		grid[i - 1 - 1][j - 2 - 1] = 2
		grid[i + 1 - 1][j + 2 - 1] = 2
	elif i == 7:
		grid[i - 1 - 1][j - 2 - 1] = 2
		grid[i + 1 - 1][j + 2 - 1] = 2
	elif j == 2:
		grid[i - 2 - 1][j - 2] = 2
		grid[i + 2 - 1][j] = 2
	elif j == 7:
		grid[i - 2 - 1][j + 1 - 1] = 2
		grid[i + 2 - 1][j - 1 - 1] = 2
	else:
		grid[i - 1 - 1][j - 2 - 1] = 2
		grid[i + 1 - 1][j + 2 - 1] = 2
	for i in range(8):
		print(*grid[i])


import sys, math, bisect, heapq, collections, itertools, functools
inum = lambda : int(sys.stdin.readline())
imap = lambda : map(int, sys.stdin.readline().split())
itup = lambda : tuple(map(int, sys.stdin.readline().split()))
isor = lambda : sorted(map(int, sys.stdin.readline().split()))
isor2 = lambda nums: sorted(nums, key=lambda num1: (num1[0], num1[1]))
ilis = lambda : [int(zz) for zz in sys.stdin.readline().split()]
dd = lambda : collections.defaultdict(lambda : 0)
adjlist = lambda : collections.defaultdict(lambda : [])
lcm = lambda num1, num2: num1 * num2 // math.gcd(num1, num2)
npr = lambda num1, num2: math.factorial(num1) // math.factorial(num1 - num2)
lowerpower = lambda num1, num2: pow(num2, math.floor(math.log(num1, num2)))
upperpower = lambda num1, num2: pow(num2, math.ceil(math.log(num1, num2)))
isperfectsquare = lambda num1: math.ceil(math.log(num1, 2)) == math.floor(math.log(num1, 2))
ncr = lambda num1, num2: math.factorial(num1) // (math.factorial(num2) * math.factorial(num1 - num2))
filtereven = lambda nums: list(filter(lambda num1: num1 % 2 == 0, nums))
filteroddd = lambda nums: list(filter(lambda num1: num1 % 2 != 0, nums))
fact = lambda num1: functools.reduce(lambda num2, num3: num2 * num3, range(1, num1 + 1))
freqarray = lambda nums: collections.Counter(nums)
bitsetarray = lambda nums: [int(num1) for num1 in bin(nums)[2:]]
lengthbitset = lambda num1: math.ceil(math.log(num1 + 0.1, 2))
bit_check = lambda num1, num2: num1 & 1 << num2
bit_set = lambda num1, num2: num1 | 1 << num2
bit_unset = lambda num1, num2: num1 & ~(1 << num2)
bit_toggle = lambda num1, num2: num1 ^ 1 << num2
minheap = lambda nums: 1 if heapq.heapify(nums) == None else 0
maxheap = lambda nums: 1 if heapq._heapify_max(nums) == None else 0
lowerbound = lambda nums, key, start, stop: bisect.bisect_left(nums, key, lo=start, hi=stop)
upperbound = lambda nums, key, start, stop: bisect.bisect_right(nums, key, lo=start, hi=stop)
inorder = lambda root: inorder(root.left) + [root.val] + inorder(root.right) if root else []
prefixsumarray = lambda nums: list(itertools.accumulate(nums, lambda num1, num2: num1 + num2))
suffixsumarray = lambda nums: prefixsum(reversed(nums))
grid = lambda rows, cols: [[0 for j in range(cols)] for idx in range(rows)]
palindromeindices = lambda num1: [(num1 // 2 - i - (0 if num1 & 1 else 1), num1 // 2 + i) for i in range(num1 // 2 + (1 if num1 & 1 else 0))]
onum = lambda num1: '%d' % num1
omap = lambda num1, num2: '%d %d' % (num1, num2)
olis = lambda nums: ' '.join((str(nums[idx]) for idx in range(len(nums))))
odict = lambda nums: '\n'.join((str('%idx: %s' % (num1[0], ' '.join((str(num2) for num2 in num1[1])))) for num1 in zip(nums.keys(), nums.values())))
ogrid = lambda grid: '\n'.join((' '.join((str(num1) for num1 in nums)) for nums in grid))
modd = int(1000000000.0) + 7
tcs = 1 if not True else int(input())
for tc in range(1, tcs + 1):
	(x, y) = imap()
	board = grid(8, 8)
	board[x - 1][y - 1] = 1
	if x == 1 and y == 1:
		(a, b) = (3, 2)
		board[a - 1][b - 1] = 2
	elif x == 1 and y == 8:
		(a, b) = (3, 7)
		board[a - 1][b - 1] = 2
	elif x == 8 and y == 1:
		(a, b) = (6, 2)
		board[a - 1][b - 1] = 2
	elif x == 8 and y == 8:
		(a, b) = (6, 7)
		board[a - 1][b - 1] = 2
	elif x == 2 and y == 2:
		(a, b) = (1, 4)
		board[a - 1][b - 1] = 2
		(a, b) = (5, 1)
		board[a - 1][b - 1] = 2
	elif x == 2 and y == 7:
		(a, b) = (1, 5)
		board[a - 1][b - 1] = 2
		(a, b) = (5, 8)
		board[a - 1][b - 1] = 2
	elif x == 7 and y == 2:
		(a, b) = (4, 1)
		board[a - 1][b - 1] = 2
		(a, b) = (8, 4)
		board[a - 1][b - 1] = 2
	elif x == 7 and y == 7:
		(a, b) = (5, 8)
		board[a - 1][b - 1] = 2
		(a, b) = (8, 4)
		board[a - 1][b - 1] = 2
	else:
		x -= 1
		y -= 1

		def isValid(x, y):
			return 0 <= x <= 7 and 0 <= y <= 7
		points = [(x - 2, y - 1), (x + 2, y + 1), (x - 1, y - 2), (x + 1, y + 2), (x + 1, y - 2), (x - 1, y + 2), (x + 2, y - 1), (x - 2, y + 1)]
		if x == 0 or x == 7 or y == 0 or (y == 7):
			cnt = 0
			for val in points:
				if isValid(*val):
					cnt += 1
					(x, y) = val
					board[x][y] = 2
					if cnt == 2:
						break
		else:
			for i in range(0, len(points), 2):
				(x1, y1) = points[i]
				(x2, y2) = points[i + 1]
				if isValid(x1, y1) and isValid(x2, y2):
					board[x1][y1] = 2
					board[x2][y2] = 2
					break
	print(ogrid(board))


T = int(input())
while T:
	(X, Y) = map(int, input().split(' '))
	mat = []
	for i in range(8):
		mat.append([0, 0, 0, 0, 0, 0, 0, 0])
	mat[X - 1][Y - 1] = 1
	if X == 1 and Y == 1 or (X == 1 and Y == 8) or (X == 8 and Y == 8) or (X == 8 and Y == 1):
		if X == 1:
			X1 = 3
		if X == 8:
			X1 = 6
		if Y == 1:
			Y1 = 2
		if Y == 8:
			Y1 = 7
		mat[X1 - 1][Y1 - 1] = 2
	elif X == 1 and Y > 1 or (X > 1 and Y == 8) or (X == 8 and Y > 1) or (X > 1 and Y == 1):
		if X == 1:
			X1 = 3
			X2 = 3
			Y1 = Y - 1
			Y2 = Y + 1
		if X == 8:
			X1 = 6
			X2 = 6
			Y1 = Y - 1
			Y2 = Y + 1
		if Y == 1:
			Y1 = 3
			Y2 = 3
			X1 = X - 1
			X2 = X + 1
		if Y == 8:
			Y1 = 6
			Y2 = 6
			X1 = X - 1
			X2 = X + 1
		mat[X1 - 1][Y1 - 1] = 2
		mat[X2 - 1][Y2 - 1] = 2
	elif X == 2 and Y == 2 or (X == 2 and Y == 7) or (X == 7 and Y == 2) or (X == 7 and Y == 7):
		if X == 2 and Y == 2:
			X1 = 5
			X2 = 1
			Y1 = 1
			Y2 = 4
		if X == 2 and Y == 7:
			X1 = 1
			X2 = 5
			Y1 = 5
			Y2 = 8
		if X == 7 and Y == 7:
			X1 = 5
			X2 = 8
			Y1 = 8
			Y2 = 4
		if X == 7 and Y == 2:
			X1 = 8
			X2 = 4
			Y1 = 4
			Y2 = 1
		mat[X1 - 1][Y1 - 1] = 2
		mat[X2 - 1][Y2 - 1] = 2
	else:
		if X == 2 or X == 7:
			X1 = X + 1
			Y1 = Y - 2
			X2 = X - 1
			Y2 = Y + 2
		elif Y == 2 or Y == 7:
			Y1 = Y + 1
			X1 = X - 2
			Y2 = Y - 1
			X2 = X + 2
		else:
			X1 = X + 1
			Y1 = Y - 2
			X2 = X - 1
			Y2 = Y + 2
		mat[X1 - 1][Y1 - 1] = 2
		mat[X2 - 1][Y2 - 1] = 2
	for i in range(8):
		print(*mat[i], sep=' ')
	T -= 1


t = int(input())

def print_(x1, y1, x2=-1, y2=-1):
	for i in range(0, 8):
		for j in range(0, 8):
			if i == x and j == y:
				print('1', end=' ')
			elif i == x1 and j == y1 or (i == x2 and j == y2):
				print('2', end=' ')
			else:
				print('0', end=' ')
		print()
while t > 0:
	t = t - 1
	s = input().strip()
	(x, y) = list(map(int, s.split(' ')))
	x -= 1
	y -= 1
	if 1 <= x <= 6 and 2 <= y <= 5:
		print_(x - 1, y - 2, x + 1, y + 2)
		continue
	if 1 <= y <= 6 and 2 <= x <= 5:
		print_(x - 2, y - 1, x + 2, y + 1)
		continue
	if x == 0 and y == 0:
		print_(1, 2)
		continue
	if x == 0 and y == 7:
		print_(1, 5)
		continue
	if x == 7 and y == 0:
		print_(6, 2)
		continue
	if x == 7 and y == 7:
		print_(6, 5)
		continue
	if x == 1 and y == 1:
		print_(0, 3, 4, 0)
		continue
	if x == 1 and y == 6:
		print_(0, 4, 4, 7)
		continue
	if x == 6 and y == 1:
		print_(3, 0, 7, 3)
		continue
	if x == 6 and y == 6:
		print_(3, 7, 7, 4)
		continue
	if x == 0:
		print_(x + 2, y + 1, x + 2, y - 1)
		continue
	if x == 7:
		print_(x - 2, y + 1, x - 2, y - 1)
		continue
	if y == 0:
		print_(x + 1, y + 2, x - 1, y + 2)
	else:
		print_(x + 1, y - 2, x - 1, y - 2)


for _ in range(int(input())):
	(x, y) = map(int, input().split())
	cb = [[0 for i in range(8)] for j in range(8)]
	if (x, y) == (1, 1):
		cb[2][1] = 2
		cb[x - 1][y - 1] = 1
	elif (x, y) == (1, 8):
		cb[1][5] = 2
		cb[x - 1][y - 1] = 1
	elif (x, y) == (8, 1):
		cb[5][1] = 2
		cb[x - 1][y - 1] = 1
	elif (x, y) == (8, 8):
		cb[5][6] = 2
		cb[x - 1][y - 1] = 1
	elif x == 1 or x == 8:
		p = x + 2
		q1 = y + 1
		q2 = y - 1
		if p > 8:
			p = x - 2
		cb[x - 1][y - 1] = 1
		cb[p - 1][q1 - 1] = 2
		cb[p - 1][q2 - 1] = 2
	elif y == 1 or y == 8:
		p = y + 2
		q1 = x + 1
		q2 = x - 1
		if p > 8:
			p = y - 2
		cb[x - 1][y - 1] = 1
		cb[q1 - 1][p - 1] = 2
		cb[q2 - 1][p - 1] = 2
	elif x in range(2, 6) and y in range(2, 7):
		p = x + 3
		p1 = y - 1
		q = y + 2
		q1 = x - 1
		cb[x - 1][y - 1] = 1
		cb[p - 1][p1 - 1] = 2
		cb[q1 - 1][q - 1] = 2
	elif y == 7 and x in range(2, 5):
		p = x + 3
		p1 = y + 1
		q = y - 2
		q1 = x - 1
		cb[x - 1][y - 1] = 1
		cb[p - 1][p1 - 1] = 2
		cb[q1 - 1][q - 1] = 2
	elif x in range(6, 8) and y in range(2, 7):
		p = x - 3
		p1 = y - 1
		q = y + 2
		q1 = x + 1
		cb[x - 1][y - 1] = 1
		cb[p - 1][p1 - 1] = 2
		cb[q1 - 1][q - 1] = 2
	else:
		p = x - 3
		p1 = y + 1
		q = y - 2
		q1 = x + 1
		cb[x - 1][y - 1] = 1
		cb[p - 1][p1 - 1] = 2
		cb[q1 - 1][q - 1] = 2
	for i in cb:
		for j in i:
			print(j, end=' ')
		print()


test = int(input())
for _ in range(test):
	(qx1, qy1) = (0, 0)
	(qx2, qy2) = (-1, -1)
	(y, x) = list(map(int, input().split(' ')))
	y = y - 1
	arr_1_6 = [1, 2, 3, 4, 5, 6]
	x = x - 1
	if x == 0 and y == 0:
		(qx1, qy1) = (2, 1)
	elif y == 7 and x == 0:
		(qy1, qx1) = (6, 2)
	elif y == 7 and x == 7:
		(qy1, qx1) = (6, 5)
	elif y == 0 and x == 7:
		(qy1, qx1) = (1, 5)
	elif x in arr_1_6 and y == 0:
		qy1 = qy2 = y + 2
		qx1 = x - 1
		qx2 = x + 1
	elif x == 0 and y in arr_1_6:
		qx1 = qx2 = x + 2
		qy1 = y - 1
		qy2 = y + 1
	elif x == 7 and y in arr_1_6:
		qx1 = qx2 = x - 2
		qy1 = y - 1
		qy2 = y + 1
	elif y == 7 and x in arr_1_6:
		qy1 = qy2 = y - 2
		qx1 = x - 1
		qx2 = x + 1
	elif x <= 4 and y <= 4:
		qx1 = x + 2
		qy1 = y - 1
		qx2 = x - 1
		qy2 = y + 3
	elif x <= 4 and y >= 4:
		qx1 = x + 2
		qy1 = y + 1
		qx2 = x - 1
		qy2 = y - 3
	elif x >= 4 and y >= 4:
		qx1 = x + 1
		qy1 = y - 2
		qx2 = x - 3
		qy2 = y + 1
	elif x >= 4 and y <= 4:
		qx1 = x + 1
		qy1 = y + 3
		qx2 = x - 2
		qy2 = y - 1
	for y_temp in range(8):
		for x_temp in range(8):
			if y_temp == y and x_temp == x:
				print(1, end=' ')
			elif y_temp == qy1 and x_temp == qx1:
				print(2, end=' ')
			elif y_temp == qy2 and x_temp == qx2:
				print(2, end=' ')
			else:
				print(0, end=' ')
		print()


test = int(input())
while test:
	test -= 1
	(x, y) = map(int, input().split())
	grid = []
	x -= 1
	y -= 1
	for i in range(8):
		row = [0] * 8
		grid.append(row)
	grid[x][y] = 1
	if x == 0 and y == 0:
		grid[x + 2][y + 1] = 2
	elif x == 0 and y == 7:
		grid[x + 1][y - 2] = 2
	elif x == 7 and y == 0:
		grid[x - 1][y + 2] = 2
	elif x == 7 and y == 7:
		grid[x - 2][y - 1] = 2
	elif x == 0:
		grid[x + 2][y + 1] = 2
		grid[x + 2][y - 1] = 2
	elif y == 0:
		grid[x + 1][y + 2] = 2
		grid[x - 1][y + 2] = 2
	elif x == 7:
		grid[x - 2][y + 1] = 2
		grid[x - 2][y - 1] = 2
	elif y == 7:
		grid[x + 1][y - 2] = 2
		grid[x - 1][y - 2] = 2
	elif x == 6 and y == 6:
		grid[4][7] = 2
		grid[7][3] = 2
	elif x == 6 and y == 1:
		grid[3][0] = 2
		grid[7][3] = 2
	elif y == 1 and x == 1:
		grid[3][0] = 2
		grid[0][4] = 2
	elif y == 6 and x == 1:
		grid[0][3] = 2
		grid[3][7] = 2
	elif x == 1 or x == 6:
		grid[x - 1][y + 2] = 2
		grid[x + 1][y - 2] = 2
	else:
		grid[x - 2][y + 1] = 2
		grid[x + 2][y - 1] = 2
	for i in grid:
		for j in i:
			print(j, end=' ')
		print()


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	n -= 1
	m -= 1
	board = [[0 for i in range(8)] for j in range(8)]
	board[n][m] = 1
	if n == 0 and m == 0 or (n == 0 and m == 7) or (n == 7 and m == 0) or (n == 7 and m == 7):
		pos = [[n + 1, m + 2], [n - 1, m + 2], [n + 1, m - 2], [n - 1, m - 2]]
		cnt = 0
		for (x, y) in pos:
			if cnt == 1:
				break
			if 0 <= x <= 7 and 0 <= y <= 7:
				board[x][y] = 2
				cnt += 1
	elif n == 0 or m == 0 or n == 7 or (m == 7):
		pos = [[n + 1, m + 2], [n - 1, m + 2], [n + 1, m - 2], [n - 1, m - 2], [n + 2, m - 1], [n + 2, m + 1], [n - 2, m - 1], [n - 2, m + 1]]
		cnt = 0
		for (x, y) in pos:
			if cnt == 2:
				break
			if 0 <= x <= 7 and 0 <= y <= 7:
				board[x][y] = 2
				cnt += 1
	elif n == 1 and m == 1 or (n == 6 and m == 6) or (n == 1 and m == 6) or (n == 6 and m == 1):
		if n == 1 and m == 1:
			board[n + 3][m - 1] = 2
			board[n - 1][m + 2] = 2
		elif n == 6 and m == 6:
			board[n + 1][m - 3] = 2
			board[n - 2][m + 1] = 2
		elif n == 1 and m == 6:
			board[n + 3][m + 1] = 2
			board[n - 1][m - 2] = 2
		elif n == 6 and m == 1:
			board[n + 1][m + 2] = 2
			board[n - 3][m - 1] = 2
	else:
		if 2 <= m <= 5:
			pos = [[n + 1, m + 2], [n - 1, m - 2]]
		else:
			pos = [[n + 2, m + 1], [n - 2, m - 1]]
		for (x, y) in pos:
			board[x][y] = 2
	for row in board:
		print(*row)


import math
from collections import Counter
ints = lambda : list(map(int, input().split()))
alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
mx = 10 ** 5
inp = lambda : int(input())
for _ in range(inp()):
	(a, b) = ints()
	(org_a, org_b) = (a, b)
	if org_a in (1, 8) and org_b in (1, 8):
		queens = [[2, 3]]
	elif org_b in (1, 8):
		b = 1
		if org_a > 4:
			a = 9 - a
		queens = [[a - 1, b + 2], [a + 1, b + 2]]
	elif org_a in (1, 8):
		a = 1
		if org_b > 4:
			b = 9 - b
		queens = [[a + 2, b - 1], [a + 2, b + 1]]
	else:
		if org_a > 4:
			a = 9 - a
		if org_b > 4:
			b = 9 - b
		if a == 2 and b == 2:
			queens = [[a + 3, b - 1], [a - 1, b + 2]]
		elif b - 2 > 0 and a - 1 > 0:
			queens = [[a - 1, b - 2], [a + 1, b + 2]]
		else:
			queens = [[a - 2, b - 1], [a + 2, b + 1]]
	for queen in queens:
		if org_a > 4:
			queen[0] = 9 - queen[0]
		if org_b > 4:
			queen[1] = 9 - queen[1]
	board = []
	for _ in range(8):
		board.append([0 for _ in range(8)])
	board[org_a - 1][org_b - 1] = 1
	for (queen_r, queen_c) in queens:
		board[queen_r - 1][queen_c - 1] = 2
	for row in board:
		print(*row)


for _ in range(int(input())):
	coor = [int(i) for i in input().split()]
	kx = coor[0]
	ky = coor[1]

	def printoutput(kx, ky, q1x, q1y, q2x=0, q2y=0):
		mat = [[0] * 8 for _ in range(8)]
		mat[kx - 1][ky - 1] = 1
		mat[q1x - 1][q1y - 1] = 2
		if q2x != 0:
			mat[q2x - 1][q2y - 1] = 2
		for row in mat:
			print(*row)
	if kx == 1 and ky == 1:
		q1x = kx + 1
		q1y = ky + 2
		printoutput(kx, ky, q1x, q1y)
		continue
	elif kx == 1 and ky == 8:
		q1x = kx + 1
		q1y = ky - 2
		printoutput(kx, ky, q1x, q1y)
		continue
	elif kx == 8 and ky == 8:
		q1x = kx - 1
		q1y = ky - 2
		printoutput(kx, ky, q1x, q1y)
		continue
	elif kx == 8 and ky == 1:
		q1x = kx - 1
		q1y = ky + 2
		printoutput(kx, ky, q1x, q1y)
		continue
	if kx == 1:
		q1x = kx + 2
		q1y = ky - 1
		q2x = kx + 2
		q2y = ky + 1
		printoutput(kx, ky, q1x, q1y, q2x, q2y)
		continue
	elif kx == 8:
		q1x = kx - 2
		q1y = ky - 1
		q2x = kx - 2
		q2y = ky + 1
		printoutput(kx, ky, q1x, q1y, q2x, q2y)
		continue
	elif ky == 1:
		q1x = kx - 1
		q1y = ky + 2
		q2x = kx + 1
		q2y = ky + 2
		printoutput(kx, ky, q1x, q1y, q2x, q2y)
		continue
	elif ky == 8:
		q1x = kx - 1
		q1y = ky - 2
		q2x = kx + 1
		q2y = ky - 2
		printoutput(kx, ky, q1x, q1y, q2x, q2y)
		continue
	if ky == 2 and kx == 2:
		q1x = kx - 1
		q1y = ky + 2
		q2x = kx + 3
		q2y = ky - 1
		printoutput(kx, ky, q1x, q1y, q2x, q2y)
		continue
	elif ky == 2 and kx == 7:
		q1x = kx + 1
		q1y = ky + 2
		q2x = kx - 3
		q2y = ky - 1
		printoutput(kx, ky, q1x, q1y, q2x, q2y)
		continue
	elif ky == 7 and kx == 2:
		q1x = kx - 1
		q1y = ky - 2
		q2x = kx + 3
		q2y = ky + 1
		printoutput(kx, ky, q1x, q1y, q2x, q2y)
		continue
	elif ky == 7 and kx == 7:
		q1x = kx + 1
		q1y = ky - 2
		q2x = kx - 3
		q2y = ky + 1
		printoutput(kx, ky, q1x, q1y, q2x, q2y)
		continue
	elif ky == 2 or ky == 7:
		q1x = kx - 2
		q1y = ky - 1
		q2x = kx + 2
		q2y = ky + 1
		printoutput(kx, ky, q1x, q1y, q2x, q2y)
		continue
	else:
		q1x = kx - 1
		q1y = ky + 2
		q2x = kx + 1
		q2y = ky - 2
		printoutput(kx, ky, q1x, q1y, q2x, q2y)
		continue


t = int(input())

def wtf(x, y):
	if x == 2 and y == 2:
		return (1, 1)
	if x == 2 and y == 7:
		return (1, 8)
	if x == 7 and y == 2:
		return (8, 1)
	if x == 7 and y == 7:
		return (8, 8)

def shift(s, n):
	if s + n < 9:
		return s + n
	else:
		return s - n

def print_board(Q, X):
	b = [[0 for i in range(8)] for j in range(8)]
	for q in Q:
		b[q[0] - 1][q[1] - 1] = 2
	b[X[0] - 1][X[1] - 1] = 1
	for bb in b:
		print(*bb)
for i in range(t):
	(x, y) = list(map(int, input().split()))
	Q = []
	if x in [1, 8] and y in [1, 8]:
		Q.append((shift(x, 2), shift(y, 1)))
	elif x in [1, 8]:
		Q.append((shift(x, 2), y - 1))
		Q.append((shift(x, 2), y + 1))
	elif y in [1, 8]:
		Q.append((x - 1, shift(y, 2)))
		Q.append((x + 1, shift(y, 2)))
	elif x in [3, 4, 5, 6]:
		Q.append((x + 2, y - 1))
		Q.append((x - 2, y + 1))
	elif y in [3, 4, 5, 6]:
		Q.append((x - 1, y + 2))
		Q.append((x + 1, y - 2))
	else:
		Q.append((shift(x, 2), wtf(x, y)[1]))
		Q.append((wtf(x, y)[0], shift(y, 3)))
	print_board(Q, (x, y))


t = int(input())
for i in range(t):
	(x, y) = map(int, input().split())
	x -= 1
	y -= 1
	mat = []
	for i in range(8):
		a = [0, 0, 0, 0, 0, 0, 0, 0]
		mat.append(a)
	mat[x][y] = 1
	if x + y == 0:
		mat[2][1] = 2
	elif x == 7 and y == 0:
		mat[5][1] = 2
	elif x == 0 and y == 7:
		mat[1][5] = 2
	elif x == 7 and y == 7:
		mat[5][6] = 2
	elif x == 0:
		mat[2][y - 1] = 2
		mat[2][y + 1] = 2
	elif x == 7:
		mat[5][y - 1] = 2
		mat[5][y + 1] = 2
	elif y == 0:
		mat[x - 1][2] = 2
		mat[x + 1][2] = 2
	elif y == 7:
		mat[x - 1][5] = 2
		mat[x + 1][5] = 2
	elif x == 1 and y == 1:
		mat[0][3] = 2
		mat[4][0] = 2
	elif x == 6 and y == 6:
		mat[4][7] = 2
		mat[7][3] = 2
	elif x == 6 and y == 1:
		mat[4][0] = 2
		mat[7][4] = 2
	elif x == 1 and y == 6:
		mat[0][3] = 2
		mat[3][7] = 2
	elif y == 1:
		mat[x + 2][y - 1] = 2
		mat[x - 2][y + 1] = 2
	elif y == 6:
		mat[x - 2][y - 1] = 2
		mat[x + 2][y + 1] = 2
	elif x == 1:
		mat[x - 1][y - 2] = 2
		mat[x + 1][y + 2] = 2
	elif x == 6:
		mat[x + 1][y - 2] = 2
		mat[x - 1][y + 2] = 2
	else:
		mat[x + 1][y + 2] = 2
		mat[x - 1][y - 2] = 2
	for i in range(8):
		for j in range(8):
			print(mat[i][j], end=' ')
		print()


import math
from bisect import bisect, bisect_left, bisect_right
import heapq
from math import gcd, floor, sqrt, log
from collections import defaultdict as ddc
from collections import Counter

def intin():
	return int(input())

def mapin():
	return map(int, input().split())

def strin():
	return input().split()
INF = 10 ** 20
mod = 1000000007

def LIS(arr, n):
	dp = [10 ** 9] * (n + 1)
	for ele in arr:
		dp[bisect_left(dp, ele)] = ele
	return bisect_left(dp, 10 ** 9)

def exponentiation(bas, exp, mod=1000000007):
	t = 1
	while exp > 0:
		if exp % 2 != 0:
			t = t * bas % mod
		bas = bas * bas % mod
		exp //= 2
	return t % mod

def MOD(p, q=1, mod=1000000007):
	expo = 0
	expo = mod - 2
	while expo:
		if expo & 1:
			p = p * q % mod
		q = q * q % mod
		expo >>= 1
	return p
yes = 'YES'
no = 'NO'
even = 'EVEN'
odd = 'ODD'

def check(do):
	(aa, bb) = do
	if -1 < aa < 8 and -1 < bb < 8:
		return True
	return False

def sett(arr, do):
	(aa, bb) = do
	if -1 < aa < 8 and -1 < bb < 8:
		arr[aa][bb] = 2

def process(a, b):
	arr = [[0] * 8 for i in range(8)]
	n = 8
	(x, y) = (a - 1, b - 1)
	arr[x][y] = 1
	if 1 < x < 6 and 0 < y < 7:
		arr[x - 2][y - 1] = 2
		arr[x + 2][y + 1] = 2
	elif x == 0 and y == 0:
		arr[x + 1][y + 2] = 2
	elif x == 0 and y == n - 1:
		arr[x + 1][y - 2] = 2
	elif x == n - 1 and y == 0:
		arr[x - 1][y + 2] = 2
	elif x == n - 1 and y == n - 1:
		arr[x - 1][y - 2] = 2
	elif x == 0:
		if y <= 5:
			arr[x + 1][y + 2] = 2
			arr[x + 2][y - 1] = 2
		else:
			arr[x + 1][y - 2] = 2
			arr[x + 2][y + 1] = 2
	elif x == 7:
		if y <= 5:
			arr[x - 1][y + 2] = 2
			arr[x - 2][y - 1] = 2
		else:
			arr[x - 1][y - 2] = 2
			arr[x - 2][y + 1] = 2
	elif x == 1:
		if y == 0:
			arr[x + 1][y + 2] = 2
			arr[x - 1][y + 2] = 2
		elif y == 7:
			arr[x + 1][y - 2] = 2
			arr[x - 1][y - 2] = 2
		elif y <= 4:
			arr[x + 2][y - 1] = 2
			arr[x - 1][y + 3] = 2
		else:
			arr[x + 2][y + 1] = 2
			arr[x - 1][y - 3] = 2
	elif x == 6:
		if y == 0:
			arr[x + 1][y + 2] = 2
			arr[x - 1][y + 2] = 2
		elif y == 7:
			arr[x + 1][y - 2] = 2
			arr[x - 1][y - 2] = 2
		elif y <= 4:
			arr[x - 2][y - 1] = 2
			arr[x + 1][y + 3] = 2
		else:
			arr[x - 2][y + 1] = 2
			arr[x + 1][y - 3] = 2
	else:
		tleft = [x - 1, y - 2]
		bleft = [x + 1, y - 2]
		tright = [x - 1, y + 2]
		bright = [x + 1, y + 2]
		if check(tleft) and check(bleft):
			sett(arr, tleft)
			sett(arr, bleft)
		elif check(tright) and check(bright):
			sett(arr, tright)
			sett(arr, bright)
		elif check(tleft) and check(tright):
			sett(arr, tleft)
			sett(arr, tright)
		else:
			sett(arr, bleft)
			sett(arr, bright)
	for row in arr:
		print(*row)

def main():
	for _ in range(int(input())):
		(a, b) = list(mapin())
		process(a, b)
main()


for tc in range(int(input())):
	(r, c) = map(int, input().split())
	if c == 1 and r == 1:
		for row in range(1, 9):
			for col in range(1, 9):
				if row == 3 and col == 2:
					if col == 8:
						print(2, end='')
					else:
						print(2, end=' ')
				elif row == r and col == c:
					if col == 8:
						print(1, end='')
					else:
						print(1, end=' ')
				elif col == 8:
					print(0, end='')
				else:
					print(0, end=' ')
			if row != 8:
				print()
	elif c == 8 and r == 8:
		for row in range(1, 9):
			for col in range(1, 9):
				if row == 6 and col == 7:
					if col == 8:
						print(2, end='')
					else:
						print(2, end=' ')
				elif row == r and col == c:
					if col == 8:
						print(1, end='')
					else:
						print(1, end=' ')
				elif col == 8:
					print(0, end='')
				else:
					print(0, end=' ')
			if row != 8:
				print()
	elif c == 1 and r == 8:
		for row in range(1, 9):
			for col in range(1, 9):
				if row == 7 and col == 3:
					if col == 8:
						print(2, end='')
					print(2, end=' ')
				elif row == r and col == c:
					if col == 8:
						print(1, end='')
					else:
						print(1, end=' ')
				elif col == 8:
					print(0, end='')
				else:
					print(0, end=' ')
			if row != 8:
				print()
	elif c == 8 and r == 1:
		for row in range(1, 9):
			for col in range(1, 9):
				if row == 3 and col == 7:
					if col == 8:
						print(2, end='')
					else:
						print(2, end=' ')
				elif row == r and col == c:
					if col == 8:
						print(1, end='')
					else:
						print(1, end=' ')
				elif col == 8:
					print(0, end='')
				else:
					print(0, end=' ')
			if row != 8:
				print()
	elif c == 1:
		for row in range(1, 9):
			for col in range(1, 9):
				if row == r - 1 and col == 3 or (row == r + 1 and col == 3):
					if col == 8:
						print(2, end='')
					else:
						print(2, end=' ')
				elif row == r and col == c:
					if col == 8:
						print(1, end='')
					else:
						print(1, end=' ')
				elif col == 8:
					print(0, end='')
				else:
					print(0, end=' ')
			if row != 8:
				print()
	elif c == 8:
		for row in range(1, 9):
			for col in range(1, 9):
				if row == r - 1 and col == 6 or (row == r + 1 and col == 6):
					if col == 8:
						print(2, end='')
					else:
						print(2, end=' ')
				elif row == r and col == c:
					if col == 8:
						print(1, end='')
					else:
						print(1, end=' ')
				elif col == 8:
					print(0, end='')
				else:
					print(0, end=' ')
			if row != 8:
				print()
	elif r == 1:
		for row in range(1, 9):
			for col in range(1, 9):
				if row == 3 and col == c - 1 or (row == 3 and col == c + 1):
					if col == 8:
						print(2, end='')
					else:
						print(2, end=' ')
				elif row == r and col == c:
					if col == 8:
						print(1, end='')
					else:
						print(1, end=' ')
				elif col == 8:
					print(0, end='')
				else:
					print(0, end=' ')
			if row != 8:
				print()
	elif r == 8:
		for row in range(1, 9):
			for col in range(1, 9):
				if row == 6 and col == c - 1 or (row == 6 and col == c + 1):
					if col == 8:
						print(2, end='')
					else:
						print(2, end=' ')
				elif row == r and col == c:
					if col == 8:
						print(1, end='')
					else:
						print(1, end=' ')
				elif col == 8:
					print(0, end='')
				else:
					print(0, end=' ')
			if row != 8:
				print()
	elif r == 2 and c == 7:
		for row in range(1, 9):
			for col in range(1, 9):
				if row == 1 and col == 4 or (row == 4 and col == 8):
					if col == 8:
						print(2, end='')
					else:
						print(2, end=' ')
				elif row == r and col == c:
					if col == 8:
						print(1, end='')
					else:
						print(1, end=' ')
				elif col == 8:
					print(0, end='')
				else:
					print(0, end=' ')
			if row != 8:
				print()
	elif c == 2 and r == 7:
		for row in range(1, 9):
			for col in range(1, 9):
				if row == 4 and col == 1 or (row == 8 and col == 4):
					if col == 8:
						print(2, end='')
					else:
						print(2, end=' ')
				elif row == r and col == c:
					if col == 8:
						print(1, end='')
					else:
						print(1, end=' ')
				elif col == 8:
					print(0, end='')
				else:
					print(0, end=' ')
			if row != 8:
				print()
	elif c == 7 and r == 7:
		for row in range(1, 9):
			for col in range(1, 9):
				if row == 4 and col == 8 or (row == 8 and col == 5):
					if col == 8:
						print(2, end='')
					else:
						print(2, end=' ')
				elif row == r and col == c:
					if col == 8:
						print(1, end='')
					else:
						print(1, end=' ')
				elif col == 8:
					print(0, end='')
				else:
					print(0, end=' ')
			if row != 8:
				print()
	elif c == 2 and r == 2:
		for row in range(1, 9):
			for col in range(1, 9):
				if row == 1 and col == 5 or (row == 4 and col == 1):
					if col == 8:
						print(2, end='')
					else:
						print(2, end=' ')
				elif row == r and col == c:
					if col == 8:
						print(1, end='')
					else:
						print(1, end=' ')
				elif col == 8:
					print(0, end='')
				else:
					print(0, end=' ')
			if row != 8:
				print()
	elif r == 2 or r == 7:
		for row in range(1, 9):
			for col in range(1, 9):
				if row == r - 1 and col == c - 2 or (row == r + 1 and col == c + 2):
					if col == 8:
						print(2, end='')
					else:
						print(2, end=' ')
				elif row == r and col == c:
					if col == 8:
						print(1, end='')
					else:
						print(1, end=' ')
				elif col == 8:
					print(0, end='')
				else:
					print(0, end=' ')
			if row != 8:
				print()
	elif c == 2 or c == 7:
		for row in range(1, 9):
			for col in range(1, 9):
				if row == r - 2 and col == c - 1 or (row == r + 2 and col == c + 1):
					if col == 8:
						print(2, end='')
					else:
						print(2, end=' ')
				elif row == r and col == c:
					if col == 8:
						print(1, end='')
					else:
						print(1, end=' ')
				elif col == 8:
					print(0, end='')
				else:
					print(0, end=' ')
			if row != 8:
				print()
	else:
		for row in range(1, 9):
			for col in range(1, 9):
				if row == r - 2 and col == c - 1 or (row == r + 2 and col == c + 1):
					if col == 8:
						print(2, end='')
					else:
						print(2, end=' ')
				elif row == r and col == c:
					if col == 8:
						print(1, end='')
					else:
						print(1, end=' ')
				elif col == 8:
					print(0, end='')
				else:
					print(0, end=' ')
			if row != 8:
				print()
	print()
	print()


def gcd(a, b):
	if b == 0:
		return a
	return gcd(a % b, a)

def swap(a, b):
	(a, b) = (b, a)
	return (a, b)

def fact(n):
	if n == 1:
		return 1
	return n * fact(n - 1)

def cbits(n):
	count = 0
	while n:
		n = n & n - 1
		count += 1
	return count

def fill(a, b, mat):
	if a == 1 and b == 1:
		mat[2 - 1][3 - 1] = 2
	elif a == 1 and b == 8:
		mat[2 - 1][6 - 1] = 2
	elif a == 8 and b == 1:
		mat[7 - 1][3 - 1] = 2
	elif a == 8 and b == 8:
		mat[7 - 1][6 - 1] = 2
	elif a == 1:
		mat[a + 2 - 1][b - 1 - 1] = 2
		mat[a + 2 - 1][b + 1 - 1] = 2
	elif a == 8:
		mat[a - 2 - 1][b - 1 - 1] = 2
		mat[a - 2 - 1][b + 1 - 1] = 2
	elif b == 1:
		mat[a - 1 - 1][b + 2 - 1] = 2
		mat[a + 1 - 1][b + 2 - 1] = 2
	elif b == 8:
		mat[a - 1 - 1][b - 2 - 1] = 2
		mat[a + 1 - 1][b - 2 - 1] = 2
	elif a == 2 and b == 2:
		mat[a - 1 - 1][b + 3 - 1] = 2
		mat[a + 2 - 1][b - 1 - 1] = 2
	elif a == 2 and b == 7:
		mat[a - 1 - 1][b - 2 - 1] = 2
		mat[a + 3 - 1][b + 1 - 1] = 2
	elif a == 7 and b == 2:
		mat[a + 1 - 1][b + 3 - 1] = 2
		mat[a - 2 - 1][b - 1 - 1] = 2
	elif a == 7 and b == 7:
		mat[a + 1 - 1][b - 3 - 1] = 2
		mat[a - 2 - 1][b + 1 - 1] = 2
	elif a == 2:
		mat[a - 1 - 1][b - 2 - 1] = 2
		mat[a + 1 - 1][b + 2 - 1] = 2
	elif a == 7:
		mat[a - 1 - 1][b - 2 - 1] = 2
		mat[a + 1 - 1][b + 2 - 1] = 2
	elif b == 2:
		mat[a - 2 - 1][b - 1 - 1] = 2
		mat[a + 2 - 1][b + 1 - 1] = 2
	elif b == 7:
		mat[a - 2 - 1][b + 1 - 1] = 2
		mat[a + 2 - 1][b - 1 - 1] = 2
	else:
		mat[a - 1 - 1][b - 2 - 1] = 2
		mat[a + 1 - 1][b + 2 - 1] = 2

def solve(a, b):
	mat = [[0] * 8 for i in range(8)]
	mat[a - 1][b - 1] = 1
	fill(a, b, mat)
	for i in range(8):
		for j in range(8):
			print(mat[i][j], end=' ')
		print()
t = int(input())
while t > 0:
	(a, b) = map(int, input().split())
	solve(a, b)
	t -= 1


t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	ch = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	a -= 1
	b -= 1
	ch[a][b] = 1
	if a == 0:
		if b == 0:
			ch[2][1] = 2
		elif b == 7:
			ch[2][6] = 2
		else:
			ch[2][b - 1] = 2
			ch[2][b + 1] = 2
	elif a == 7:
		if b == 0:
			ch[5][1] = 2
		elif b == 7:
			ch[5][6] = 2
		else:
			ch[5][b - 1] = 2
			ch[5][b + 1] = 2
	elif a == 1:
		if b == 0:
			ch[a - 1][2] = 2
			ch[a + 1][2] = 2
		elif b == 7:
			ch[a - 1][5] = 2
			ch[a + 1][5] = 2
		elif b == 1:
			ch[0][4] = 2
			ch[3][0] = 2
		elif b == 6:
			ch[0][3] = 2
			ch[3][7] = 2
		else:
			ch[a - 1][b + 2] = 2
			ch[a + 1][b - 2] = 2
	elif a == 6:
		if b == 0:
			ch[a - 1][2] = 2
			ch[a + 1][2] = 2
		elif b == 7:
			ch[a - 1][5] = 2
			ch[a + 1][5] = 2
		elif b == 1:
			ch[7][4] = 2
			ch[4][0] = 2
		elif b == 6:
			ch[7][3] = 2
			ch[4][7] = 2
		else:
			ch[a - 1][b + 2] = 2
			ch[a + 1][b - 2] = 2
	elif b == 0:
		ch[a - 1][2] = 2
		ch[a + 1][2] = 2
	elif b == 7:
		ch[a - 1][5] = 2
		ch[a + 1][5] = 2
	elif b == 1:
		ch[a - 2][2] = 2
		ch[a + 2][0] = 2
	elif b == 6:
		ch[a - 2][7] = 2
		ch[a + 2][5] = 2
	else:
		ch[a - 1][b + 2] = 2
		ch[a + 1][b - 2] = 2
	for j in ch:
		print(*j)


def solve(a, b):
	if a == 1 and b == 1:
		return [2, 3]
	if a == 1 and b == 8:
		return [2, 6]
	if a == 8 and b == 1:
		return [7, 3]
	if a == 8 and b == 8:
		return [7, 6]
	if a == 1:
		return [3, b - 1, 3, b + 1]
	if a == 8:
		return [6, b - 1, 6, b + 1]
	if b == 1:
		return [a - 1, 3, a + 1, 3]
	if b == 8:
		return [a - 1, 6, a + 1, 6]
	if a == 2 and b == 2:
		return [1, 4, 5, 1]
	if a == 2 and b == 7:
		return [1, 5, 5, 8]
	if a == 7 and b == 2:
		return [8, 4, 4, 1]
	if a == 7 and b == 7:
		return [8, 5, 4, 8]
	if a > 2 and a < 7:
		return [a + 2, b - 1, a - 2, b + 1]
	return [a + 1, b + 2, a - 1, b - 2]
for _ in range(int(input())):
	grid = []
	for i in range(8):
		x = []
		for j in range(8):
			x.append(0)
		grid.append(x)
	(a, b) = map(int, input().split())
	alpha = solve(a, b)
	grid[a - 1][b - 1] = 1
	i = 0
	while i < len(alpha):
		grid[alpha[i] - 1][alpha[i + 1] - 1] = 2
		i += 2
	for i in grid:
		print(*i)


for _ in range(int(input())):
	(a, b) = [int(x) for x in input().split()]
	board = [[0 for i in range(8)] for j in range(8)]
	board[a - 1][b - 1] = 1
	if a == 1 and b == 1:
		board[2 - 1][3 - 1] = 2
	elif a == 1 and b == 8:
		board[2 - 1][6 - 1] = 2
	elif a == 8 and b == 1:
		board[7 - 1][3 - 1] = 2
	elif a == 8 and b == 8:
		board[7 - 1][6 - 1] = 2
	elif a == 1:
		board[a + 2 - 1][b - 1 - 1] = 2
		board[a + 2 - 1][b + 1 - 1] = 2
	elif a == 8:
		board[a - 2 - 1][b - 1 - 1] = 2
		board[a - 2 - 1][b + 1 - 1] = 2
	elif b == 1:
		board[a - 1 - 1][b + 2 - 1] = 2
		board[a + 1 - 1][b + 2 - 1] = 2
	elif b == 8:
		board[a - 1 - 1][b - 2 - 1] = 2
		board[a + 1 - 1][b - 2 - 1] = 2
	elif a == 2 and b == 2:
		board[a - 1 - 1][b + 3 - 1] = 2
		board[a + 2 - 1][b - 1 - 1] = 2
	elif a == 2 and b == 7:
		board[a - 1 - 1][b - 2 - 1] = 2
		board[a + 3 - 1][b + 1 - 1] = 2
	elif a == 7 and b == 2:
		board[a + 1 - 1][b + 3 - 1] = 2
		board[a - 2 - 1][b - 1 - 1] = 2
	elif a == 7 and b == 7:
		board[a + 1 - 1][b - 3 - 1] = 2
		board[a - 2 - 1][b + 1 - 1] = 2
	elif a == 2:
		board[a - 1 - 1][b - 2 - 1] = 2
		board[a + 1 - 1][b + 2 - 1] = 2
	elif a == 7:
		board[a - 1 - 1][b - 2 - 1] = 2
		board[a + 1 - 1][b + 2 - 1] = 2
	elif b == 2:
		board[a - 2 - 1][b - 1 - 1] = 2
		board[a + 2 - 1][b + 1 - 1] = 2
	elif b == 7:
		board[a - 2 - 1][b + 1 - 1] = 2
		board[a + 2 - 1][b - 1 - 1] = 2
	else:
		board[a - 1 - 1][b - 2 - 1] = 2
		board[a + 1 - 1][b + 2 - 1] = 2
	for i in range(len(board)):
		print(*board[i])


def func():
	(a, b) = map(int, input().split())
	l = []
	for i in range(8):
		temp = []
		for j in range(8):
			temp.append(0)
		l.append(temp)
	l[a - 1][b - 1] = 1
	if a == 1 and b == 1:
		l[1][2] = 2
	elif a == 1 and b == 8:
		l[1][5] = 2
	elif a == 8 and b == 1:
		l[5][1] = 2
	elif a == 8 and b == 8:
		l[5][6] = 2
	elif a == 1:
		l[2][b - 2] = 2
		l[2][b] = 2
	elif b == 1:
		l[a][2] = 2
		l[a - 2][2] = 2
	elif b == 8:
		l[a][5] = 2
		l[a - 2][5] = 2
	elif a == 8:
		l[5][b - 2] = 2
		l[5][b] = 2
	elif a == 2 and b == 2:
		l[0][4] = 2
		l[3][0] = 2
	elif a == 2 and b == 7:
		l[0][3] = 2
		l[3][7] = 2
	elif a == 7 and b == 2:
		l[4][0] = 2
		l[7][4] = 2
	elif a == 7 and b == 7:
		l[7][3] = 2
		l[4][7] = 2
	elif b == 2 or b == 7:
		l[a - 3][b - 2] = 2
		l[a + 1][b] = 2
	else:
		l[a - 2][b + 1] = 2
		l[a][b - 3] = 2
	for i in range(len(l)):
		print(*l[i])
t = int(input())
while t:
	func()
	t -= 1


for tc in range(int(input())):
	(lailla, glabulous) = map(int, input().split())
	arr = [[0 for j in range(8)] for i in range(8)]
	arr[lailla - 1][glabulous - 1] = 1
	if lailla in [1, 8] and glabulous in [1, 8]:
		if lailla == 1 and glabulous == 1:
			(p, a100, r, s) = (3, 2, 3, 2)
		elif lailla == 1 and glabulous == 8:
			(p, a100, r, s) = (3, 7, 3, 7)
		elif lailla == 8 and glabulous == 1:
			(p, a100, r, s) = (6, 2, 6, 2)
		elif lailla == 8 and glabulous == 8:
			(p, a100, r, s) = (6, 7, 6, 7)
		arr[p - 1][a100 - 1] = 2
	elif lailla in [2, 7] and glabulous in [2, 7]:
		if lailla == 2 and glabulous == 2:
			(p, a100, r, s) = (1, 4, 5, 1)
		elif lailla == 2 and glabulous == 7:
			(p, a100, r, s) = (1, 5, 5, 8)
		elif lailla == 7 and glabulous == 2:
			(p, a100, r, s) = (4, 1, 8, 4)
		elif lailla == 7 and glabulous == 7:
			(p, a100, r, s) = (5, 8, 8, 4)
	else:
		(lailla, glabulous) = (lailla - 1, glabulous - 1)
		chk = lambda lailla, glabulous: 0 <= lailla <= 7 and 0 <= glabulous <= 7
		pts = [(lailla + 1, glabulous - 2), (lailla - 1, glabulous + 2), (lailla + 2, glabulous - 1), (lailla - 2, glabulous + 1), (lailla - 2, glabulous - 1), (lailla + 2, glabulous + 1), (lailla - 1, glabulous - 2), (lailla + 1, glabulous + 2)]
		if lailla == 0 or lailla == 7 or glabulous == 0 or (glabulous == 7):
			tot = 0
			for pt in pts:
				if chk(*pt):
					tot += 1
					(lailla, glabulous) = pt
					if tot == 1:
						(p, a100) = (lailla + 1, glabulous + 1)
					if tot == 2:
						(r, s) = (lailla + 1, glabulous + 1)
						break
		else:
			for i in range(0, len(pts), 2):
				(lailla1, glabulous1) = pts[i]
				(lailla2, glabulous2) = pts[i + 1]
				if chk(lailla1, glabulous1) and chk(lailla2, glabulous2):
					(p, a100, r, s) = (lailla1 + 1, glabulous1 + 1, lailla2 + 1, glabulous2 + 1)
					break
	arr[p - 1][a100 - 1] = arr[r - 1][s - 1] = 2
	for row in arr:
		print(*row)


def factorial(n):
	return 1 if n == 1 or n == 0 else n * factorial(n - 1)
case = int(input())
for z in range(case):
	board = []
	for i in range(8):
		lst = []
		for j in range(8):
			lst.append(0)
		board.append(lst)
	(x, y) = map(int, input().split(' '))
	a = x
	b = y
	board[a - 1][b - 1] = 1
	if a == 1 and b == 1:
		board[2 - 1][3 - 1] = 2
	elif a == 1 and b == 8:
		board[2 - 1][6 - 1] = 2
	elif a == 8 and b == 1:
		board[7 - 1][3 - 1] = 2
	elif a == 8 and b == 8:
		board[7 - 1][6 - 1] = 2
	elif a == 1:
		board[a + 2 - 1][b - 1 - 1] = 2
		board[a + 2 - 1][b + 1 - 1] = 2
	elif a == 8:
		board[a - 2 - 1][b - 1 - 1] = 2
		board[a - 2 - 1][b + 1 - 1] = 2
	elif b == 1:
		board[a - 1 - 1][b + 2 - 1] = 2
		board[a + 1 - 1][b + 2 - 1] = 2
	elif b == 8:
		board[a - 1 - 1][b - 2 - 1] = 2
		board[a + 1 - 1][b - 2 - 1] = 2
	elif a == 2 and b == 2:
		board[a - 1 - 1][b + 3 - 1] = 2
		board[a + 2 - 1][b - 1 - 1] = 2
	elif a == 2 and b == 7:
		board[a - 1 - 1][b - 2 - 1] = 2
		board[a + 3 - 1][b + 1 - 1] = 2
	elif a == 7 and b == 2:
		board[a + 1 - 1][b + 3 - 1] = 2
		board[a - 2 - 1][b - 1 - 1] = 2
	elif a == 7 and b == 7:
		board[a + 1 - 1][b - 3 - 1] = 2
		board[a - 2 - 1][b + 1 - 1] = 2
	elif a == 2:
		board[a - 1 - 1][b - 2 - 1] = 2
		board[a + 1 - 1][b + 2 - 1] = 2
	elif a == 7:
		board[a - 1 - 1][b - 2 - 1] = 2
		board[a + 1 - 1][b + 2 - 1] = 2
	elif b == 2:
		board[a - 2 - 1][b - 1 - 1] = 2
		board[a + 2 - 1][b + 1 - 1] = 2
	elif b == 7:
		board[a - 2 - 1][b + 1 - 1] = 2
		board[a + 2 - 1][b - 1 - 1] = 2
	else:
		board[a - 1 - 1][b - 2 - 1] = 2
		board[a + 1 - 1][b + 2 - 1] = 2
	for i in range(8):
		factorial(i)
	for i in range(8):
		for j in range(8):
			print(board[i][j], end=' ')
		print()


from sys import stdin
input = stdin.readline
t = int(input())
for _ in range(t):
	(x, y) = map(int, input().split())
	x -= 1
	y -= 1
	ans = [[0 for i in range(8)] for j in range(8)]
	ans[x][y] = 1
	a = [[0, 0], [0, 7], [7, 0], [7, 7]]
	b = [[-2, -1], [-2, +1], [-1, -2], [-1, +2], [+1, -2], [+1, +2], [+2, -1], [+2, +1]]
	if [x, y] in a:
		for i in range(len(b)):
			if 0 <= x + b[i][0] <= 7 and 0 <= y + b[i][1] <= 7:
				ans[x + b[i][0]][y + b[i][1]] = 2
				break
	elif x == 0:
		ans[x + 2][y - 1] = 2
		ans[x + 2][y + 1] = 2
	elif x == 7:
		ans[x - 2][y - 1] = 2
		ans[x - 2][y + 1] = 2
	elif y == 0:
		ans[x + 1][y + 2] = 2
		ans[x - 1][y + 2] = 2
	elif y == 7:
		ans[x + 1][y - 2] = 2
		ans[x - 1][y - 2] = 2
	elif x == 1 and y == 1:
		ans[x + 2][y - 1] = 2
		ans[x - 1][y + 3] = 2
	elif x == 1 and y == 6:
		ans[x - 1][y - 3] = 2
		ans[x + 2][y + 1] = 2
	elif x == 6 and y == 1:
		ans[x - 2][y - 1] = 2
		ans[x + 1][y + 3] = 2
	elif x == 6 and y == 6:
		ans[x - 2][y + 1] = 2
		ans[x + 1][y - 3] = 2
	elif y == 1 or y == 6:
		ans[x - 2][y + 1] = 2
		ans[x + 2][y - 1] = 2
	elif x == 1 or x == 6:
		ans[x - 1][y - 2] = 2
		ans[x + 1][y + 2] = 2
	else:
		ans[x - 1][y - 2] = 2
		ans[x + 1][y + 2] = 2
	for i in range(8):
		print(*ans[i])


import numpy as np
for _ in range(int(input())):
	(x, y) = [int(i) for i in input().split()]
	bd = np.zeros((8, 8))
	x -= 1
	y -= 1
	bd[x][y] = 1
	if x <= 0 or x >= 7 or y <= 0 or (y >= 7):
		if x <= 5:
			if y > 0:
				bd[x + 2][y - 1] = 2
			if y < 7:
				bd[x + 2][y + 1] = 2
			if x > 0:
				if y <= 5:
					bd[x - 1][y + 2] = 2
				else:
					bd[x - 1][y - 2] = 2
		else:
			if y > 0:
				bd[x - 2][y - 1] = 2
			if y < 7:
				bd[x - 2][y + 1] = 2
			if x < 7:
				if y <= 5:
					bd[x + 1][y + 2] = 2
				else:
					bd[x + 1][y - 2] = 2
	elif x == 1 or x == 6:
		if x >= 3:
			if y >= 2:
				bd[x + 1][y - 2] = 2
				bd[x - 3][y + 1] = 2
			else:
				bd[x + 1][y + 2] = 2
				bd[x - 3][y - 1] = 2
		elif y >= 2:
			bd[x - 1][y - 2] = 2
			bd[x + 3][y + 1] = 2
		else:
			bd[x - 1][y + 2] = 2
			bd[x + 3][y - 1] = 2
	elif y == 1 or y == 6:
		if y >= 3:
			if x >= 2:
				bd[x - 2][y + 1] = 2
				bd[x + 1][y - 3] = 2
			else:
				bd[x + 2][y + 1] = 2
				bd[x - 1][y - 3] = 2
		elif x >= 2:
			bd[x - 2][y - 1] = 2
			bd[x + 1][y + 3] = 2
		else:
			bd[x + 2][y - 1] = 2
			bd[x - 1][y + 3] = 2
	else:
		bd[x - 1][y - 2] = 2
		bd[x + 1][y + 2] = 2
	for i in range(8):
		for j in range(8):
			print(int(bd[i][j]), end=' ')
		print()


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(x, y) = map(int, input().split())
	ans = []
	for i in range(8):
		ans.append([0] * 8)
	ans[x - 1][y - 1] = 1
	if x == 1:
		if y == 1:
			ans[2][1] = 2
		elif y == 8:
			ans[2][6] = 2
		else:
			ans[2][y] = 2
			ans[2][y - 2] = 2
	elif x == 8:
		if y == 1:
			ans[5][1] = 2
		elif y == 8:
			ans[5][6] = 2
		else:
			ans[5][y - 2] = 2
			ans[5][y] = 2
	elif y == 1:
		ans[x - 2][2] = 2
		ans[x][2] = 2
	elif y == 8:
		ans[x - 2][5] = 2
		ans[x][5] = 2
	elif y == 2:
		if x == 2:
			ans[3][0] = 2
			ans[0][4] = 2
		elif x == 7:
			ans[3][0] = 2
			ans[7][3] = 2
		else:
			ans[x + 1][y - 2] = 2
			ans[x - 3][y] = 2
	elif y == 7:
		if x == 2:
			ans[3][7] = 2
			ans[0][3] = 2
		elif x == 7:
			ans[4][7] = 2
			ans[7][3] = 2
		else:
			ans[x + 1][y - 2] = 2
			ans[x - 3][y] = 2
	else:
		ans[x][y - 3] = 2
		ans[x - 2][y + 1] = 2
	for i in range(8):
		print(*ans[i])


for _ in range(int(input())):
	(a, b) = [int(x) - 1 for x in input().split()]
	cb = []
	c = False
	for i in range(8):
		cb.append([0] * 8)
	cb[a][b] = 1
	if a == 0 and b == 0:
		cb[2][1] = 2
		c = True
	elif a == 0 and b == 7:
		cb[1][5] = 2
		c = True
	elif a == 7 and b == 0:
		cb[5][1] = 2
		c = True
	elif a == 7 and b == 7:
		cb[5][6] = 2
		c = True
	if 'fjruirjufruif' == 3473448:
		print('Tanmay')
	if not c:
		if a == 0 and b not in [0, 7]:
			cb[2][b - 1] = 2
			cb[2][b + 1] = 2
			c = True
		elif a == 7 and b not in [0, 7]:
			cb[5][b - 1] = 2
			cb[5][b + 1] = 2
			c = True
		elif b == 0 and a not in [0, 7]:
			cb[a - 1][2] = 2
			cb[a + 1][2] = 2
			c = True
		elif b == 7 and a not in [0, 7]:
			cb[a - 1][5] = 2
			cb[a + 1][5] = 2
			c = True
	if not c:
		if a in range(1, 6) and b in range(1, 5):
			cb[a + 2][b - 1] = 2
			cb[a - 1][b + 3] = 2
		elif a == 6 and b in range(1, 6):
			cb[7][b + 2] = 2
			cb[3][b - 1] = 2
		elif a in range(1, 6) and b in [5, 6]:
			cb[a + 2][b + 1] = 2
			cb[a - 1][b - 3] = 2
		elif a == b == 6:
			cb[7][4] = 2
			cb[3][7] = 2
	for i in cb:
		for j in i:
			print(j, end=' ')
		print()


for i in range(1, 1000):
	pass
for _ in range(int(input())):
	(x, y) = map(int, input().split())
	(x1, y1) = (0, 0)
	(x2, y2) = (0, 0)
	(x3, y3) = (-1, -1)
	x1 = x - 2
	x2 = x + 2
	y1 = y - 1
	y2 = y + 1
	if x == 2:
		x1 = x - 1
		x2 = x + 1
		y1 = y - 2
		y2 = y + 2
	if y == 2:
		x2 = x - 2
		y2 = y - 1
		x1 = x + 2
		y1 = y + 1
	if x == 7:
		x1 = x - 1
		x2 = x + 1
		y1 = y - 2
		y2 = y + 2
	if x == 2 and y == 2:
		(x1, y1) = (1, 4)
		(x2, y2) = (5, 1)
	if x == 7 and y == 7:
		(x1, y1) = (8, 5)
		(x2, y2) = (4, 8)
	if x == 2 and y == 7:
		(x1, y1) = (1, 5)
		(x2, y2) = (5, 8)
	if x == 7 and y == 2:
		(x1, y1) = (4, 1)
		(x2, y2) = (8, 4)
	if x == 1:
		x1 = x2 = x + 2
	if x == 8:
		x1 = x2 = x - 2
	if y == 1:
		y1 = y2 = y + 2
		x1 = x - 1
		x2 = x + 1
	if y == 8:
		y1 = y2 = y - 2
		x1 = x - 1
		x2 = x + 1
	for i in range(1, 1000):
		pass
	ar = []
	for i in range(1, 9):
		row = []
		for j in range(1, 9):
			if i == x1 and j == y1:
				row.append(2)
			elif i == x2 and j == y2:
				row.append(2)
			elif i == x and j == y:
				row.append(1)
			else:
				row.append(0)
		ar.append(row)
	for i in range(1, 1000):
		pass
	for i in ar:
		print(*i)


import math

def solve():
	(a, b) = map(int, input().split(' '))
	a -= 1
	b -= 1
	lst = []
	for i in range(8):
		lst1 = []
		for j in range(8):
			lst1.append(0)
		lst.append(lst1)
	lst[a][b] = 1
	if a == 0 and b == 0:
		lst[1][2] = 2
	elif a == 7 and b == 7:
		lst[6][5] = 2
	elif a == 0 and b == 7:
		lst[1][5] = 2
	elif a == 7 and b == 0:
		lst[6][2] = 2
	elif a == 0:
		lst[a + 2][b - 1] = 2
		lst[a + 2][b + 1] = 2
	elif a == 7:
		lst[a - 2][b - 1] = 2
		lst[a - 2][b + 1] = 2
	elif b == 0:
		lst[a - 1][b + 2] = 2
		lst[a + 1][b + 2] = 2
	elif b == 7:
		lst[a - 1][b - 2] = 2
		lst[a + 1][b - 2] = 2
	elif a == 6:
		if b == 1:
			lst[a - 3][b - 1] = 2
			lst[a + 1][b + 2] = 2
		else:
			lst[a - 3][b + 1] = 2
			lst[a + 1][b - 2] = 2
	elif a == 1:
		if b == 6:
			lst[a - 1][b - 3] = 2
			lst[a + 2][b + 1] = 2
		else:
			lst[a + 3][b - 1] = 2
			lst[a - 1][b + 2] = 2
	else:
		lst[a - 2][b - 1] = 2
		lst[a + 2][b + 1] = 2
	for i in lst:
		for j in i:
			print(j, end=' ')
		print()
t = int(input())
while t:
	t -= 1
	solve()


for _ in range(int(input())):
	(y, x) = map(int, input().split())
	chess = [[0 for _ in range(8)] for _ in range(8)]
	chess[y - 1][x - 1] = 1
	k = 0
	for d in [[1, 0], [0, 1], [-1, 0], [0, -1]]:
		if 1 <= y + d[0] <= 8 and 1 <= x + d[1] <= 8:
			k += 1
	if k == 2:
		for d in [[1, 2], [-1, 2], [1, -2], [-1, -2]]:
			if 1 <= y + d[0] <= 8 and 1 <= x + d[1] <= 8:
				chess[y + d[0] - 1][x + d[1] - 1] = 2
				break
	elif k == 3:
		for d in [[[-1, 2], [1, 2]], [[-1, -2], [1, -2]], [[2, -1], [2, 1]], [[-2, -1], [-2, 1]]]:
			if 1 <= y + d[0][0] <= 8 and 1 <= x + d[0][1] <= 8 and (1 <= y + d[1][0] <= 8) and (1 <= x + d[1][1] <= 8):
				chess[y + d[0][0] - 1][x + d[0][1] - 1] = 2
				chess[y + d[1][0] - 1][x + d[1][1] - 1] = 2
				break
	else:
		f = False
		for d in [[[-1, 2], [1, -2]], [[-1, -2], [1, 2]], [[2, -1], [-2, 1]], [[-2, -1], [2, 1]]]:
			if 1 <= y + d[0][0] <= 8 and 1 <= x + d[0][1] <= 8 and (1 <= y + d[1][0] <= 8) and (1 <= x + d[1][1] <= 8):
				chess[y + d[0][0] - 1][x + d[0][1] - 1] = 2
				chess[y + d[1][0] - 1][x + d[1][1] - 1] = 2
				f = True
				break
		if not f:
			for d in [[[-1, 2], [3, -1]], [[-1, -2], [3, 1]], [[1, -2], [-3, 1]], [[1, 2], [-3, -1]]]:
				if 1 <= y + d[0][0] <= 8 and 1 <= x + d[0][1] <= 8 and (1 <= y + d[1][0] <= 8) and (1 <= x + d[1][1] <= 8):
					chess[y + d[0][0] - 1][x + d[0][1] - 1] = 2
					chess[y + d[1][0] - 1][x + d[1][1] - 1] = 2
					break
	for i in range(8):
		for j in range(8):
			print(chess[i][j], end=' ')
		print()


t = int(input())
for i in range(t):
	chess = []
	for i in range(8):
		arr = [0, 0, 0, 0, 0, 0, 0, 0]
		chess.append(arr)
	(x, y) = map(int, input().split())
	chess[x - 1][y - 1] = 1
	if x == 1 and y == 1:
		chess[1][2] = 2
	elif x == 8 and y == 1:
		chess[5][1] = 2
	elif x == 8 and y == 8:
		chess[5][6] = 2
	elif x == 1 and y == 8:
		chess[2][6] = 2
	elif x == 1:
		chess[2][y - 2] = 2
		chess[2][y] = 2
	elif y == 1:
		chess[x - 2][2] = 2
		chess[x][2] = 2
	elif x == 8:
		chess[5][y - 2] = 2
		chess[5][y] = 2
	elif y == 8:
		chess[x - 2][5] = 2
		chess[x][5] = 2
	elif x == 2 and y == 2:
		chess[0][3] = 2
		chess[4][0] = 2
	elif x == 2 and y == 7:
		chess[0][4] = 2
		chess[4][7] = 2
	elif x == 7 and y == 2:
		chess[7][3] = 2
		chess[3][0] = 2
	elif x == 7 and y == 7:
		chess[7][4] = 2
		chess[3][7] = 2
	elif x > 2 and x != 7:
		chess[x - 3][y - 2] = 2
		chess[x + 1][y] = 2
	elif y > 2:
		chess[x - 2][y - 3] = 2
		chess[x][y + 1] = 2
	for i in range(8):
		for j in range(8):
			print(chess[i][j], end=' ')
		print()


t = int(input())
for _ in range(t):
	arr = [[0 for i in range(8)] for j in range(8)]
	(A, B) = map(int, input().split())
	x1 = 0
	y1 = 0
	x2 = 0
	y2 = 0
	if A == 1 and B == 1:
		arr[0][0] = 1
		arr[2][1] = 2
	elif A == 1 and B == 8:
		arr[0][7] = 1
		arr[2][6] = 2
	elif A == 8 and B == 8:
		arr[7][7] = 1
		arr[5][6] = 2
	elif A == 8 and B == 1:
		arr[7][0] = 1
		arr[5][1] = 2
	elif B == 1:
		arr[A - 1][0] = 1
		arr[A - 2][B + 1] = 2
		arr[A][B + 1] = 2
	elif B == 8:
		arr[A - 1][7] = 1
		arr[A - 2][5] = 2
		arr[A][5] = 2
	elif A == 8:
		arr[7][B - 1] = 1
		arr[5][B - 2] = 2
		arr[5][B] = 2
	elif A == 1:
		arr[0][B - 1] = 1
		arr[2][B - 2] = 2
		arr[2][B] = 2
	elif B <= 5 and A <= 6:
		arr[A - 1][B - 1] = 1
		arr[A - 2][B + 2] = 2
		arr[A + 1][B - 2] = 2
	elif B > 5 and A <= 6:
		arr[A - 1][B - 1] = 1
		arr[A - 2][B - 4] = 2
		arr[A + 1][B] = 2
	elif B <= 5 and A == 7:
		arr[A - 1][B - 1] = 1
		arr[A][B + 2] = 2
		arr[A - 3][B - 2] = 2
	elif B > 5 and A == 7:
		arr[A - 1][B - 1] = 1
		arr[A][B - 4] = 2
		arr[A - 3][B] = 2
	print(*arr[0])
	print(*arr[1])
	print(*arr[2])
	print(*arr[3])
	print(*arr[4])
	print(*arr[5])
	print(*arr[6])
	print(*arr[7])


for i in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1 and m == 1:
		xx = [[1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 2, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 1 and m == 2:
		xx = [[0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 2, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 1 and m == 3:
		xx = [[0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 2, 0, 2, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 1 and m == 4:
		xx = [[0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 2, 0, 2, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 1 and m == 5:
		xx = [[0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 2, 0, 2, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 1 and m == 6:
		xx = [[0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 2, 0, 2, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 1 and m == 7:
		xx = [[0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 2, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 1 and m == 8:
		xx = [[0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 2, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 2 and m == 1:
		xx = [[0, 0, 2, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 2, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 2 and m == 2:
		xx = [[0, 0, 0, 0, 2, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 2 and m == 3:
		xx = [[2, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 2, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 2 and m == 4:
		xx = [[0, 2, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 2, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 2 and m == 5:
		xx = [[0, 0, 2, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 2, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 2 and m == 6:
		xx = [[0, 0, 0, 2, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 2 and m == 7:
		xx = [[0, 0, 0, 2, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 2 and m == 8:
		xx = [[0, 0, 0, 0, 2, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 2, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 3 and m == 1:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 2, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 2, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 3 and m == 2:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 2, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 3 and m == 3:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 2, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 3 and m == 4:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 2, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 2, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 3 and m == 5:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 2, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 2, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 3 and m == 6:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 2, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 3 and m == 7:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 2, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 3 and m == 8:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 2, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 2, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 4 and m == 1:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 2, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 2, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 4 and m == 2:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 2, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 4 and m == 3:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 2, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 4 and m == 4:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 2, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 2, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 4 and m == 5:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 2, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 2, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 4 and m == 6:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 2, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 4 and m == 7:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 2, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 4 and m == 8:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 2, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 2, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 5 and m == 1:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 2, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 2, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 5 and m == 2:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 2, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 5 and m == 3:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 2, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 5 and m == 4:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 2, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 2, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 5 and m == 5:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 2, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 2, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 5 and m == 6:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 2, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 5 and m == 7:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 2, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 5 and m == 8:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 2, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 2, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 6 and m == 1:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 2, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 2, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 6 and m == 2:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 2, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 6 and m == 3:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 2, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 6 and m == 4:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 2, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 2, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 6 and m == 5:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 2, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 2, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 6 and m == 6:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 2, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0]]
	if n == 6 and m == 7:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 2, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 2]]
	if n == 6 and m == 8:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 2, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 2, 0]]
	if n == 7 and m == 1:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 2, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 2, 0, 0, 0]]
	if n == 7 and m == 2:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 2, 0, 0, 0]]
	if n == 7 and m == 3:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 2, 0, 0, 0]]
	if n == 7 and m == 4:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 2, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 2, 0, 0]]
	if n == 7 and m == 5:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 2, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 2, 0]]
	if n == 7 and m == 6:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 2, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 2]]
	if n == 7 and m == 7:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 2, 0, 0, 0]]
	if n == 7 and m == 8:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 2, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 2, 0, 0]]
	if n == 8 and m == 1:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 2, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0]]
	if n == 8 and m == 2:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 2, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0]]
	if n == 8 and m == 3:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 2, 0, 2, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0]]
	if n == 8 and m == 4:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 2, 0, 2, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0]]
	if n == 8 and m == 5:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 2, 0, 2, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0]]
	if n == 8 and m == 6:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 2, 0, 2, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0]]
	if n == 8 and m == 7:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 2, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0]]
	if n == 8 and m == 8:
		xx = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 2, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1]]
	for j in xx:
		print(*j)


def test():
	N = list(map(int, input().split()))
	x = N[0]
	y = N[1]
	q = []
	if x != 1 and x != 8 and (y != 1) and (y != 8):
		if x <= 4 and y <= 4:
			q.append((x + 2, y - 1))
			q.append((x - 1, y + 3))
		if x <= 4 and y > 4:
			q.append((x + 2, y + 1))
			q.append((x - 1, y - 3))
		if x > 4 and y > 4:
			q.append((x + 1, y - 2))
			q.append((x - 3, y + 1))
		if x > 4 and y <= 4:
			q.append((x - 2, y - 1))
			q.append((x + 1, y + 3))
	elif x == 1 and y == 1:
		q.append((3, 2))
	elif x == 1 and y == 8:
		q.append((2, 6))
	elif x == 8 and y == 1:
		q.append((6, 2))
	elif x == 8 and y == 8:
		q.append((6, 7))
	elif x == 1:
		if y < 7:
			q.append((3, y - 1))
			q.append((2, y + 2))
		else:
			q.append((3, y + 1))
			q.append((2, y - 2))
	elif x == 8:
		if y < 7:
			q.append((6, y - 1))
			q.append((7, y + 2))
		else:
			q.append((7, y - 2))
			q.append((6, y + 1))
	elif y == 1:
		if x < 7:
			q.append((x - 1, 3))
			q.append((x + 2, 2))
		else:
			q.append((x + 1, 3))
			q.append((x - 2, 2))
	elif y == 8:
		if x < 7:
			q.append((x - 1, 6))
			q.append((x + 2, 7))
		else:
			q.append((x - 2, 7))
			q.append((x + 1, 6))
	else:
		print('prob')
	for i in range(1, 9):
		for j in range(1, 9):
			if (i, j) in q:
				print(2, end=' ')
			elif i == x and j == y:
				print(1, end=' ')
			else:
				print(0, end=' ')
		print()
	print()
tests = int(input())
for i in range(tests):
	test()


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	cboard = [[0 for i in range(8)] for j in range(8)]
	cboard[a - 1][b - 1] = 1
	if a == 2 and b == 2:
		cboard[3][0] = 2
		cboard[0][4] = 2
	elif a == 2 and b == 7:
		cboard[3][7] = 2
		cboard[0][3] = 2
	elif a == 7 and b == 2:
		cboard[4][0] = 2
		cboard[7][4] = 2
	elif a == 7 and b == 7:
		cboard[4][7] = 2
		cboard[7][3] = 2
	elif a == 1 and b == 1:
		cboard[2][1] = 2
	elif a == 8 and b == 1:
		cboard[-3][1] = 2
	elif a == 8 and b == 8:
		cboard[-3][-2] = 2
	elif a == 1 and b == 8:
		cboard[2][-2] = 2
	elif a == 1:
		cboard[2][b] = 2
		cboard[2][b - 2] = 2
	elif a == 8:
		cboard[-3][b] = 2
		cboard[-3][b - 2] = 2
	elif b == 1:
		cboard[a][2] = 2
		cboard[a - 2][2] = 2
	elif b == 8:
		cboard[a][-3] = 2
		cboard[a - 2][-3] = 2
	elif a + 1 < 8 and a - 3 >= 0:
		cboard[a + 1][b] = 2
		cboard[a - 3][b - 2] = 2
	else:
		cboard[a][b + 1] = 2
		cboard[a - 2][b - 3] = 2
	for row in cboard:
		print(*row)


I = lambda : list(map(int, input().split()))
(t,) = I()
for i in range(t):
	(a, b) = I()
	op = [[-2, -1], [2, 1], [2, -1], [-2, 1]]
	rop = [[1, 2], [-1, -2], [1, -2], [-1, 2]]
	chess = [[0] * 8 for i in range(8)]
	chess[a - 1][b - 1] = 1
	pos = 0
	ix = 0
	vis = set()
	for (dy, dx) in [[-1, -1], [-1, 1], [1, -1], [1, 1], [0, 1], [0, -1], [1, 0], [-1, 0]]:
		(x, y) = (a + dx, b + dy)
		if x >= 1 and y >= 1 and (x <= 8) and (y <= 8):
			vis.add((x, y))
	noice = set(vis)
	ans = []
	for (dy, dx) in op + rop if 1 not in [x, y] else rop + op:
		(x, y) = (a + dx, b + dy)
		if x >= 1 and y >= 1 and (x <= 8) and (y <= 8):
			cs = len(vis)
			new = set(vis)
			for i in vis:
				(mx, my) = i
				if mx == x or my == y or x + y == mx + my or (x - y == mx - my):
					new.remove(i)
			if len(new) < cs:
				ans.append((x, y))
				vis = set(new)
	if len(ans) == 3:
		na = []
		for i in ans:
			(x, y) = (i[1], i[0])
			if i == (4, 1):
				(x, y) = (1, 5)
			elif i == (1, 5):
				(x, y) = (5, 8)
			if i == (5, 1):
				(x, y) = (8, 5)
			elif i == (8, 5):
				(x, y) = (4, 8)
			if x >= 1 and y >= 1 and (x <= 8) and (y <= 8) and ([x, y] != [i[1], i[0]]):
				na.append((x, y))
				na.append(i)
				break
		ans = na
		if not ans:
			vis = set(noice)
			for (dy, dx) in rop:
				(x, y) = (a + dx, b + dy)
				if x >= 1 and y >= 1 and (x <= 8) and (y <= 8):
					cs = len(vis)
					new = set(vis)
					for i in vis:
						(mx, my) = i
						if mx == x or my == y or x + y == mx + my or (x - y == mx - my):
							new.remove(i)
					if len(new) < cs:
						ans.append((x, y))
						vis = set(new)
	for (x, y) in ans:
		chess[x - 1][y - 1] = 2
	for i in chess:
		print(*i)


import math
import itertools
import heapq
from math import floor, ceil
from collections import deque
import sys
from collections import Counter
I = lambda : list(map(int, input().split()))
M = lambda : map(int, input().split())
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)
MOD = 10 ** 9 + 7

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def isSubsequence(s, t):
	for i in range(0, len(s)):
		try:
			index = t.index(s[i])
		except ValueError:
			return False
		t = t[index + 1:]
	return True

def bisect_right(a, x):
	(lo, hi) = (0, len(a))
	while lo < hi:
		mid = lo + (hi - lo) // 2
		if a[mid] > x:
			hi = mid
		else:
			lo = mid + 1
	return lo

def bisect_left(a, x):
	(lo, hi) = (0, len(a))
	while lo < hi:
		mid = lo + (hi - lo) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def binary_search(nums, target):
	(left, right) = (0, len(nums) - 1)
	while left <= right:
		pivot = left + (right - left) // 2
		if nums[pivot] == target:
			return pivot
		if target < nums[pivot]:
			right = pivot - 1
		else:
			left = pivot + 1
	return -1

def prefixSum(arr):
	dp = [arr[0]]
	arrLen = len(arr)
	for i in range(1, arrLen):
		dp.append(dp[-1] + arr[i])
	return dp

def suffixSum(arr):
	arrLen = len(arr)
	dp = [0] * arrLen
	dp[-1] = arr[-1]
	for i in range(N - 2, -1, -1):
		dp[i] = dp[i + 1] + arr[i]
	return dp

def fun():
	return

def nCr(n, r):
	f = math.factorial
	return f(n) // f(r) // f(n - r)

def solve():
	(i, j) = M()
	mat = []
	for x in range(8):
		tmp = []
		for y in range(8):
			tmp.append(0)
		mat.append(tmp)
	i -= 1
	j -= 1
	coveredrow = set()
	coveredcol = set()
	mat[i][j] = 1
	c = 0
	if i == 0 and j == 0:
		mat[i + 1][j + 2] = 2
	elif i == 0 and j == 7:
		mat[i + 1][j - 2] = 2
	elif i == 7 and j == 0:
		mat[i - 1][j + 2] = 2
	elif i == 7 and j == 7:
		mat[i - 1][j - 2] = 2
	elif i == 0:
		mat[i + 2][j - 1] = 2
		mat[i + 2][j + 1] = 2
	elif i == 7:
		mat[i - 2][j - 1] = 2
		mat[i - 2][j + 1] = 2
	elif j == 0:
		mat[i + 1][j + 2] = 2
		mat[i - 1][j + 2] = 2
	elif j == 7:
		mat[i + 1][j - 2] = 2
		mat[i - 1][j - 2] = 2
	else:
		poss = [[(i + 1, j - 2), (i - 3, j + 1)], [(i + 1, j + 2), (i - 3, j - 1)], [(i - 1, j + 2), (i + 3, j - 1)], [(i - 1, j - 2), (i + 3, j + 1)]]
		for X in poss:
			(r1, c1) = (X[0][0], X[0][1])
			(r2, c2) = (X[1][0], X[1][1])
			if 0 <= r1 <= 7 and 0 <= r2 <= 7 and (0 <= c1 <= 7) and (0 <= c2 <= 7):
				mat[r1][c1] = 2
				mat[r2][c2] = 2
				break
	for x in range(8):
		print(' '.join([str(i) for i in mat[x]]))
TC = 1
TC = int(input())
for testcases in range(TC):
	solve()


test = int(input())
for _ in range(test):
	(n, m) = map(int, input().split())
	if n == 1 and (m == 1 or m == 8):
		if m == 1:
			i = n + 1
			j = m + 2
		if m == 8:
			i = n + 1
			j = m - 2
		for k in range(1, 9):
			for l in range(1, 9):
				if k == n and l == m:
					print('1', end=' ')
				elif k == i and l == j:
					print('2', end=' ')
				else:
					print('0', end=' ')
			print('')
	elif n == 8 and (m == 1 or m == 8):
		if m == 1:
			i = n - 1
			j = m + 2
		if m == 8:
			i = n - 1
			j = m - 2
		for k in range(1, 9):
			for l in range(1, 9):
				if k == n and l == m:
					print('1', end=' ')
				elif k == i and l == j:
					print('2', end=' ')
				else:
					print('0', end=' ')
			print('')
	elif n == 1 and m != 1 and (m != 8):
		i = n + 2
		j = m - 1
		j2 = m + 1
		for k in range(1, 9):
			for l in range(1, 9):
				if k == n and l == m:
					print('1', end=' ')
				elif k == i and l == j:
					print('2', end=' ')
				elif k == i and l == j2:
					print('2', end=' ')
				else:
					print('0', end=' ')
			print('')
	elif n == 8 and m != 1 and (m != 8):
		i = n - 2
		j = m - 1
		j2 = m + 1
		for k in range(1, 9):
			for l in range(1, 9):
				if k == n and l == m:
					print('1', end=' ')
				elif k == i and l == j:
					print('2', end=' ')
				elif k == i and l == j2:
					print('2', end=' ')
				else:
					print('0', end=' ')
			print('')
	elif m == 1 and n != 1 and (n != 8):
		i = n + 1
		i2 = n - 1
		j = m + 2
		for k in range(1, 9):
			for l in range(1, 9):
				if k == n and l == m:
					print('1', end=' ')
				elif k == i and l == j:
					print('2', end=' ')
				elif k == i2 and l == j:
					print('2', end=' ')
				else:
					print('0', end=' ')
			print('')
	elif m == 8 and n != 1 and (n != 8):
		i = n + 1
		i2 = n - 1
		j = m - 2
		for k in range(1, 9):
			for l in range(1, 9):
				if k == n and l == m:
					print('1', end=' ')
				elif k == i and l == j:
					print('2', end=' ')
				elif k == i2 and l == j:
					print('2', end=' ')
				else:
					print('0', end=' ')
			print('')
	elif n == 2 and m == 2 or (n == 2 and m == 7) or (n == 7 and m == 2) or (n == 7 and m == 7):
		if n == 2 and m == 2:
			i = 1
			j = 5
			i2 = 4
			j2 = 1
		if n == 2 and m == 7:
			i = 1
			j = 5
			i2 = 5
			j2 = 8
		if n == 7 and m == 2:
			i = 8
			j = 4
			i2 = 4
			j2 = 1
		if n == 7 and m == 7:
			i = 8
			j = 5
			i2 = 4
			j2 = 8
		for k in range(1, 9):
			for l in range(1, 9):
				if k == n and l == m:
					print('1', end=' ')
				elif k == i and l == j:
					print('2', end=' ')
				elif k == i2 and l == j2:
					print('2', end=' ')
				else:
					print('0', end=' ')
			print('')
	elif n == 2 or n == 7:
		i = n + 1
		i2 = n - 1
		j = m + 2
		j2 = m - 2
		for k in range(1, 9):
			for l in range(1, 9):
				if k == n and l == m:
					print('1', end=' ')
				elif k == i and l == j:
					print('2', end=' ')
				elif k == i2 and l == j2:
					print('2', end=' ')
				else:
					print('0', end=' ')
			print('')
	else:
		i = n + 2
		i2 = n - 2
		j = m + 1
		j2 = m - 1
		for k in range(1, 9):
			for l in range(1, 9):
				if k == n and l == m:
					print('1', end=' ')
				elif k == i and l == j:
					print('2', end=' ')
				elif k == i2 and l == j2:
					print('2', end=' ')
				else:
					print('0', end=' ')
			print('')


def sq(a, b, q):
	global printed
	printed = True
	for i in range(8):
		for j in range(8):
			if (i, j) == (a, b):
				print(1, '', end='')
			elif (i, j) in q:
				print(2, '', end='')
			else:
				print(0, '', end='')
		print()

def corner(a, b):
	if a == 1:
		if b == 1:
			sq(a - 1, b - 1, [(1, 2)])
		elif b == 8:
			sq(a - 1, b - 1, [(1, 5)])
	elif a == 8:
		if b == 1:
			sq(a - 1, b - 1, [(6, 2)])
		elif b == 8:
			sq(a - 1, b - 1, [(6, 5)])

def edge(a, b):
	if a == 1:
		sq(a - 1, b - 1, [(a + 1, b - 2), (a + 1, b)])
	elif a == 8:
		sq(a - 1, b - 1, [(a - 3, b - 2), (a - 3, b)])
	elif b == 1:
		sq(a - 1, b - 1, [(a - 2, b + 1), (a, b + 1)])
	elif b == 8:
		sq(a - 1, b - 1, [(a - 2, b - 3), (a, b - 3)])

def final(a, b):
	if (a, b) == (2, 2):
		q = [(3, 0), (0, 4)]
		sq(a - 1, b - 1, q)
		return
	elif (a, b) == (2, 7):
		q = [(3, 7), (0, 3)]
		sq(a - 1, b - 1, q)
		return
	elif (a, b) == (7, 2):
		q = [(7, 3), (3, 0)]
		sq(a - 1, b - 1, q)
		return
	elif (a, b) == (7, 7):
		q = [(7, 3), (4, 7)]
		sq(a - 1, b - 1, q)
		return

def final2(a, b):
	if a == 2:
		q1 = (0, b + 1)
		q2 = (2, b - 3)
	elif a == 7:
		q1 = (5, b + 1)
		q2 = (7, b - 3)
	elif b == 2:
		q1 = (a + 1, 0)
		q2 = (a - 3, 2)
	elif b == 7:
		q1 = (a + 1, 5)
		q2 = (a - 3, 7)
	else:
		q1 = (a, b + 1)
		q2 = (a - 2, b - 3)
	sq(a - 1, b - 1, [q1, q2])
t = int(input())
for _ in range(t):
	printed = False
	(a, b) = map(int, input().split())
	corner(a, b)
	if not printed:
		edge(a, b)
	if not printed:
		final(a, b)
	if not printed:
		final2(a, b)


t = int(input())
for _ in range(t):

	def retx(ret, x, y):
		if x > 1 and x < 6 and (y > 1) and (y < 6):
			ret[x - 1][y - 2] = 2
			ret[x + 1][y + 2] = 2
		if x == 0:
			if y == 0:
				ret[2][1] = 2
			elif y == 7:
				ret[2][6] = 2
			else:
				ret[2][y - 1] = 2
				ret[2][y + 1] = 2
			return ret
		if x == 7:
			if y == 0:
				ret[5][1] = 2
			elif y == 7:
				ret[5][6] = 2
			else:
				ret[5][y - 1] = 2
				ret[5][y + 1] = 2
			return ret
		if y == 0:
			ret[x - 1][2] = 2
			ret[x + 1][2] = 2
			return ret
		if y == 7:
			ret[x - 1][5] = 2
			ret[x + 1][5] = 2
			return ret
		if x == 1:
			if y == 1:
				ret[3][0] = 2
				ret[0][4] = 2
			elif y == 6:
				ret[3][7] = 2
				ret[0][3] = 2
			else:
				ret[x - 1][y - 2] = 2
				ret[x + 1][y + 2] = 2
			return ret
		if x == 6:
			if y == 1:
				ret[4][0] = 2
				ret[7][4] = 2
			elif y == 6:
				ret[4][7] = 2
				ret[7][3] = 2
			else:
				ret[x - 1][y - 2] = 2
				ret[x + 1][y + 2] = 2
			return ret
		if y == 6:
			ret[x - 2][7] = 2
			ret[x + 2][5] = 2
		elif y == 1:
			ret[x - 2][0] = 2
			ret[x + 2][2] = 2
		return ret
	(x, y) = map(int, input().split())
	(x, y) = (x - 1, y - 1)
	rett = [[0 for i in range(8)] for j in range(8)]
	rett[x][y] = 1
	rett = retx(rett, x, y)
	for i in rett:
		print(' '.join(map(str, i)))


for _ in range(int(input())):
	(x, y) = map(int, input().split())
	ans = [[0 for _ in range(8)] for _ in range(8)]
	x -= 1
	y -= 1
	ans[x][y] = 1

	def solve(i, j, tar):
		c = p = 0
		dir = [(2, 1), (2, -1), (1, -2), (1, 2), (-1, 2), (-1, -2), (-2, -1), (-2, 1)]
		while c < tar:
			(x, y) = dir[p]
			(ni, nj) = (i + x, j + y)
			if 0 <= ni < 8 and 0 <= nj < 8:
				ans[ni][nj] = 2
				c += 1
			p += 1
	if [x, y] in [[0, 0], [7, 7], [0, 7], [7, 0]]:
		solve(x, y, 1)
	elif x == 0 or y == 0 or x == 7 or (y == 7):
		solve(x, y, 2)
	elif x == 1 and y == 1:
		ans[4][0] = 2
		ans[0][3] = 2
	elif x == 6 and y == 6:
		ans[7][4] = 2
		ans[3][7] = 2
	elif x == 1 and y == 6:
		ans[3][7] = 2
		ans[0][3] = 2
	elif x == 6 and y == 1:
		ans[3][0] = 2
		ans[7][3] = 2
	else:
		dir = [(2, 1), (2, -1), (1, -2), (1, 2), (-1, 2), (-1, -2), (-2, -1), (-2, 1)]
		for (i, j) in dir:
			(nx1, ny1) = (x + i, y + j)
			(nx2, ny2) = (x - i, y - j)
			if 0 <= nx1 < 8 and 0 <= nx2 < 8 and (0 <= ny1 < 8) and (0 <= ny2 < 8):
				ans[nx1][ny1] = 2
				ans[nx2][ny2] = 2
				(nx, ny) = (x - i, y - j)
				break
	for row in ans:
		print(*row)