Sudoku Solution Checker

Check whether each completed 9x9 grid has digits 1 to 9 exactly once in every row, column, and 3x3 box.

Easy3MatrixHash mapInterviewNo attempts yetTime limit5sMemory limit512 MB

Problem

Sudoku is a single player number puzzle. You fill a 9x9 grid with digits so that every row, every column, and each of the nine non-overlapping 3x3 sub-grids holds each digit from 1 to 9 exactly once. A puzzle starts partly filled and usually has one solution.

You are given a completed N2×N2N^2 \times N^2 Sudoku grid. Decide whether it is a valid solution. A valid solution meets all three conditions:

  • Each row holds each number from 1 to N2N^2 exactly once.
  • Each column holds each number from 1 to N2N^2 exactly once.
  • Split the N2×N2N^2 \times N^2 grid into N2N^2 non-overlapping N×NN \times N sub-grids. Each sub-grid holds each number from 1 to N2N^2 exactly once.

Uniqueness does not matter here. Only check whether the given grid is a valid solution.

Input

The first line holds the number of test cases, TT. TT test cases follow. Each test case starts with an integer NN. The next N2N^2 lines describe a completed Sudoku grid, and each of those lines holds exactly N2N^2 integers. Every integer in the input is positive and less than 1000.

Limits

  • 1T1001 \le T \le 100
  • N=3N = 3

Output

For each test case, print one line in the form "Case #x: y", where x is the test case number starting from 1, and y is Yes if the grid is a valid solution or No if it is not. The judge is case sensitive, so yes and no are not accepted.