Connect the Cells

Connect each color pair with disjoint grid paths that cover every cell and print the lexicographically smallest direction map.

Hard8BacktrackingGraphDFSNo attempts yetTime limit3sMemory limit256 MB

Problem

Connect the Cells is a well known puzzle game, and a version of it exists on most mobile devices.

The board has NN rows with NN cells in each row. Every cell is either empty or colored. An empty cell is written as 0, and a colored cell is written as a digit from 1 to 9. Every color that appears on the board appears in exactly two cells. Your task is to connect the two cells of each color while leaving no empty cell behind.

Two cells are adjacent when they share an edge, vertically or horizontally. In every input board, two cells of the same color are never adjacent.

Here is how two cells of the same color are connected. Each color has its own pen, and a cell takes that color as soon as the pen touches it. Put the pen on one of the two cells of the color, keep moving it to an adjacent cell through empty cells, and finally move it onto the second cell of the same color. The moment the pen enters that second cell the two cells count as connected, so you stop using that pen and start connecting another color if any color is left. The pen cannot leave the board and cannot color the same cell twice.

The pen may stand on an already colored cell only when the cell is the start cell or the end cell of its own color, and it may stand on each of those two cells only once.

Input

The first line has the number of test cases TT (1T1001 \le T \le 100). The TT test cases follow. The first line of a test case has the board size NN (3N83 \le N \le 8). The next NN lines each hold NN digits, and one digit describes one cell. A 0 means an empty cell.

If the board holds XX distinct colors, those colors are named with the digits from 1 to XX (1X91 \le X \le 9). Every test case has at least one solution, and every input board satisfies all of the conditions above.

Output

For each test case first print Case n: on its own line, where nn is the test case number starting from 1. Then print NN lines of NN characters each. The jj-th character of the ii-th line is the direction the pen used to leave the cell in row ii and column jj: U for up, R for right, D for down, L for left, and X if that cell is the last cell the pen entered after connecting the cells of its color.

Several solutions can exist. Print the lexicographically smallest one. Read the NN lines you print from top to bottom as one string of N2N^2 characters, and compare characters so that they grow in the order D, L, R, U, X.