Checksum

아직 제출이 없습니다메모리 제한1024 MB

문제

Grace and Edsger are constructing a N×NN \times N boolean matrix AA. The element in ii-th row and jj-th column is represented by A_i,jA\_{i,j}. They decide to note down the checksum (defined as bitwise XOR of given list of elements) along each row and column. Checksum of ii-th row is represented as R_iR\_i. Checksum of jj-th column is represented as C_jC\_j.

For example, if N=2N = 2, A=[10 11]A = \begin{bmatrix} 1 & 0 \\\ 1 & 1 \end{bmatrix}, then R=[10]R = \begin{bmatrix} 1 & 0 \end{bmatrix} and C=[01]C = \begin{bmatrix} 0 & 1 \end{bmatrix}.

Once they finished the matrix, Edsger stores the matrix in his computer. However, due to a virus, some of the elements in matrix AA are replaced with 1-1 in Edsger's computer. Luckily, Edsger still remembers the checksum values. He would like to restore the matrix, and reaches out to Grace for help. After some investigation, it will take B_i,jB\_{i,j} hours for Grace to recover the original value of A_i,jA\_{i,j} from the disk. Given the final matrix AA, cost matrix BB, and checksums along each row (RR) and column (CC), can you help Grace decide on the minimum total number of hours needed in order to restore the original matrix AA?

입력

The first line of the input gives the number of test cases, TT. TT test cases follow.

The first line of each test case contains a single integer NN.

The next NN lines each contain NN integers representing the matrix AA. jj-th element on the ii-th line represents A_i,jA\_{i,j}.

The next NN lines each contain NN integers representing the matrix BB. jj-th element on the ii-th line represents B_i,jB\_{i,j}.

The next line contains NN integers representing the checksum of the rows. ii-th element represents R_iR\_i.

The next line contains NN integers representing the checksum of the columns. jj-th element represents C_jC\_j.

출력

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is the minimum number of hours to restore matrix AA.

제한

  • 1T1001 \le T \le 100.
  • 1A_i,j1-1 \le A\_{i,j} \le 1, for all ii, jj.
  • 1B_i,j10001 \le B\_{i,j} \le 1000, for ii, jj where A_i,j=1A\_{i,j} = -1, otherwise B_i,j=0B\_{i,j} = 0.
  • 0R_i10 \le R\_i \le 1, for all ii.
  • 0C_j10 \le C\_j \le 1, for all jj.
  • It is guaranteed that there exist at least one way to replace 1-1 in AA with 00 or 11 such that RR and CC as satisfied.

힌트

In Sample Case #1, A_1,2A\_{1,2} can be restored using the checksum of either 1-st row or 2-nd column. Hence, Grace can restore the matrix without spending any time to recover the data.

In Sample Case #2, Grace spends one hour to recover A_1,1A\_{1,1}. After that, she can use checksums of 1-st row and 1-st column to restore A_1,2A\_{1,2} and A_2,1A\_{2,1} respectively. And then she can use checksum of 2-nd row to restore A_2,2A\_{2,2}. Hence, Grace can restore the matrix by spending one hour.

In Sample Case #3, Grace can spend one hour to recover A_1,1A\_{1,1} and another hour to recover A_2,2A\_{2,2}. After that, she can use checksum to restore the rest of the matrix. Hence, Grace can restore the matrix by spending two hours in total.