Checkerboard Matrix (Large)

Find the fewest row and column swaps that turn the given binary matrix into a checkerboard, or report IMPOSSIBLE.

Medium6MatrixGreedyMathNo attempts yetTime limit5sMemory limit512 MB

Problem

When she is bored, Mija plays with matrices. She tries to turn one matrix into another using as few moves as possible. For Mija, one move is swapping two rows of the matrix or swapping two columns of the matrix.

Today Mija has a very special matrix MM. MM is a 2N×2N2N \times 2N matrix whose entries are all 0 or 1. Mija wants to turn MM into a checkerboard matrix, a matrix whose entries alternate between 0 and 1 along every row and along every column. Find the minimum number of moves that turns MM into a checkerboard matrix.

Input

The first line contains the number of test cases TT. TT test cases follow. Each test case starts with a line containing a single integer NN. The next 2N2N lines each contain 2N2N characters, which are the rows of MM. Each character is 0 or 1.

Limits

  • 1T1001 \le T \le 100
  • 1N1031 \le N \le 10^3

Output

For each test case, print one line in the form Case #x: y, where xx is the test case number starting from 1 and yy is the minimum number of row swaps and column swaps that turns MM into a checkerboard matrix. If MM cannot be turned into a checkerboard matrix, print IMPOSSIBLE in place of yy.

Hint

In the first sample case, MM is already a checkerboard matrix.

In the second sample case, Mija swaps columns 1 and 2 and then swaps rows 1 and 2 to get a checkerboard matrix.

In the third sample case, no sequence of moves works because the matrix does not have enough ones.