Sudoku Solution Checker

Check whether each completed N-squared by N-squared grid has every row, column, and N by N block holding 1 to N-squared exactly once.

Easy3MatrixHash mapImplementationInterviewNo 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-matrices holds each digit from 1 to 9 exactly once. The grid starts out partially filled and usually has one solution.

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

  • Every row holds each number from 1 to N2N^2 exactly once.
  • Every 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-matrices. Every sub-matrix holds each number from 1 to N2N^2 exactly once.

Uniqueness of the puzzle 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 line holds exactly N2N^2 integers. Every integer in the input is positive and smaller than 1000.

Limits

  • 1T1001 \le T \le 100
  • 3N63 \le N \le 6

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.