Under construction

Still being built. Things may change

Sudoku Transformation

Time limit1sMemory limit128 MB

Summary
Determine whether a completed sudoku board can be turned into another via rotations, band/stack swaps, row/column swaps, and digit relabeling.
Level

Medium6 of 10

Topics
Brute force, Simulation, Combinatorics
Solved
No attempts yet

Problem

Sang-geun starts every morning by solving the Sudoku puzzle printed in the newspaper. One day, while solving one, he noticed something strange: the puzzle he was working on was yesterday's puzzle rotated by 90 degrees. He felt deeply betrayed. Of course, when he begins a puzzle he cannot tell whether it is yesterday's, but as he fills in the numbers he eventually realizes it.

Furious, Sang-geun spent every night drinking, and decided he could no longer put up with the newspaper's tyranny. So he wants to check whether today's puzzle was made from yesterday's puzzle through a few simple operations.

A Sudoku board consists of 9×99 \times 9 cells. Moreover, every 3×33 \times 3 block of cells is grouped together into one of nine regions. Initially only some cells are filled with numbers between 1 and 9, and all the remaining cells are empty. The goal of the puzzle is to fill the empty cells with numbers from 1 to 9 so that each of the numbers 1 through 9 appears exactly once in every row, every column, and every region. A valid Sudoku puzzle always has exactly one way to fill in the empty cells.

The allowed simple operations are as follows.

  1. Rotate the whole puzzle clockwise or counterclockwise.
  2. Swap two 3×93 \times 9 column segments.
  3. Swap two 9×39 \times 3 row segments.
  4. Swap two entire rows or two entire columns.
  5. Apply a permutation ff of the numbers 1 through 9 to every cell; that is, replace the value xx in every cell with f(x)f(x).

All of the operations above are applied to a Sudoku's solution (a completed board), and any Sudoku that was solvable before a transformation is still solvable after it.

Input

The first line contains the number of test cases NN (0≤N≤500 \le N \le 50).

For each test case, the first 9 lines are yesterday's puzzle's answer, and the next 9 lines are today's puzzle. Empty cells are given as 0.

Consecutive test cases are separated by a single blank line. Yesterday's puzzle is always a valid Sudoku, and today's puzzle also has exactly one answer.

Output

For each test case, print Yes if today's puzzle is a transformation of yesterday's puzzle, and No otherwise.

Examples1

  1. Example 1

    Input
    2
    963174258
    178325649
    254689731
    821437596
    496852317
    735961824
    589713462
    317246985
    642598173
    060104050
    200000001
    008305600
    800407006
    006000300
    700901004
    500000002
    040508070
    007206900
    
    534678912
    672195348
    198342567
    859761423
    426853791
    713924856
    961537284
    287419635
    345286179
    010900605
    025060070
    870000902
    702050043
    000204000
    490010508
    107000056
    040080210
    208001090
    
    Expected output
    Yes
    No