/************************
 *
 *  Stephen Fulwider
 *	BHCSI - 2007
 *	Grid Traveling - A Program using recursion to traverse a grid given a set of
 *	rules that must be followed for each movement
 *
 ***********************/

import java.io.*;
import java.util.*;
import java.math.*;

public class GridTraveling
{
	public static void main(String[] args) throws Exception
	{
		new GridTraveling().go();
	}
	
	char[][] grid;
	int n;
	
	void go() throws Exception
	{
		Scanner sc = new Scanner(new File("grid.in"));
		int k = Integer.parseInt(sc.nextLine());
		
		for (int i=1; i<=k; i++)
		{
			n = Integer.parseInt(sc.nextLine());
			grid = new char[n][n];
			
			/* read in the grid */
			for (int j=0; j<n; j++)
			{
				String s = sc.nextLine();
				for (int l=0; l<n; l++)
					grid[j][l] = s.charAt(l);
			}
			
			/* find the start state */
			int j=0,l=0;
			outer: for (j=0; j<n; j++)
							for (l=0; l<n; l++)
								if (grid[j][l] == 'X')
									break outer;
			
			/* move each dir. from the start state */
			solve(j+1,l);
			solve(j-1,l);
			solve(j,l+1);
			solve(j,l-1);
			
			/* output solution */
			System.out.println("Reachable Map #" + i);
			for (j=0; j<n; j++)
			{
				for (l=0; l<n; l++)
					System.out.print(grid[j][l] + " ");
				System.out.println();
			}
			System.out.println();
			
		}
	}
	
	void solve(int r, int c)
	{
		/* if grid spot out of bounds or can't be move from, just return */
		if (r<0 || r>=n || c <0 || c>=n || grid[r][c] == 'R' || grid[r][c] == 'X' || grid[r][c] == 'W')
			return;
		
		/* remember current char, since it will be overwritten */
		char thisGridChar = grid[r][c];
		grid[r][c] = 'R';
		
		/* if characer is a digit, you can move to other digits around w/ greater value or punctuation */
		if (Character.isDigit(thisGridChar))
		{
			
			/* N */
			if (r > 0)
				if (grid[r-1][c] >= thisGridChar || isPunct(grid[r-1][c]))
					solve(r-1,c);
			
			/* E */
			if (c < n-1)
				if (grid[r][c+1] >= thisGridChar || isPunct(grid[r][c+1]))
					solve(r,c+1);
			
			/* S */
			if (r < n-1)
				if (grid[r+1][c] >= thisGridChar || isPunct(grid[r+1][c]))
					solve(r+1,c);
			
			/* W */
			if (c > 0)
				if (grid[r][c-1] >= thisGridChar || isPunct(grid[r][c-1]))
					solve(r,c-1);
		}
		
		/* otherwise, the spot is punctuation and can move NESW, or to any other
		 * punctuation mark of the same value on the grid */
		else
		{
			/* try NESW */
			solve(r+1,c);
			solve(r-1,c);
			solve(r,c+1);
			solve(r,c-1);
			
			/* find any value on the grid that is the same, and move there as well */
			for (int i=0; i<n; i++)
				for (int j=0; j<n; j++)
					if (grid[i][j] == thisGridChar)
						solve(i,j);
		}
	}
	
	/* determines whether a given character is a punctuation mark */
	boolean isPunct(char x)
	{
		return (x == '?' || x == '!' || x == '.');
	}
}