Under construction

Still being built. Things may change

Good and Lucky Matrices

Time limit2sMemory limit1024 MB

Summary
Given an n x n binary matrix of one type (good or lucky), map it injectively to a matrix of the other type so the mapping is a bijection.
Level

Hard10 of 10

Topics
Combinatorics, Math, Matrix
Solved
No attempts yet

Problem

A square binary matrix is good if and only if its determinant is odd.

A binary matrix of size n×nn \times n is lucky if and only if the greedy matching algorithm below finds a matching of size nn. The algorithm reads the matrix line by line, from top to bottom. On each line, if there is a 11 in a column that was never chosen before, the algorithm chooses the leftmost such column.

You must compare the numbers of good and lucky matrices of size n×nn \times n. To do that, you must build a bijection between the smaller set and an equinumerous subset of the larger set.

Input

The first line contains an integer tt, the number of test cases. The test cases follow.

The first line of each test case contains the string good or lucky, which gives the type of matrix. The next line contains a single integer nn (1≤n≤20001 \leq n \leq 2000), the size of the matrix. Then nn lines follow. The ii-th of them contains a binary string of length exactly nn, the ii-th row of the matrix.

The matrices given on the second run are exactly the same as the matrices your program printed on the first run, in the same order. The sum of nn over all test cases does not exceed 2000.

Output

On each run, print tt answers. Each answer is either a single integer −1-1 on its own line, or a matrix of the opposite type formatted like the input: a line with nn, followed by nn lines that describe the rows.

The mapping must be a bijection. If your answer on the first run for matrix AA of some type was matrix BB, and your answer on the second run for BB of the opposite type was matrix CC, then A=CA = C must hold. For each nn and each matrix type, let UU be the set of all n×nn \times n matrices of this type and VV the set of all n×nn \times n matrices of the other type. You may answer −1-1 for at most max⁡(∣U∣−∣V∣,0)\max(|U| - |V|, 0) different matrices from UU.

Examples2

  1. Example 1

    Input
    3
    lucky
    2
    11
    11
    good
    2
    11
    01
    lucky
    2
    01
    10
    
    Expected output
    2
    11
    10
    2
    11
    01
    2
    01
    10
    
  2. Example 2

    Input
    3
    good
    2
    11
    10
    lucky
    2
    11
    01
    good
    2
    01
    10
    
    Expected output
    2
    11
    11
    2
    11
    01
    2
    01
    10