#P1868A. Fill in the Matrix
Fill in the Matrix
Description
There is an empty matrix of size .
Zhongkao examination is over, and Daniel would like to do some puzzle games. He is going to fill in the matrix using permutations of length . That is, each row of must be a permutation of length .
Define the value of the -th column in as . Since Daniel likes diversity, the beauty of is .
You have to help Daniel fill in the matrix and maximize its beauty.
A permutation of length is an array consisting of distinct integers from to in arbitrary order. For example, is a permutation, but is not a permutation ( appears twice in the array), and is also not a permutation ( but there is in the array).
The of an array is the smallest non-negative integer that does not belong to the array. For example, because does not belong to the array, and because , , and appear in the array, but does not.
The first line of input contains a single integer () — the number of test cases. The description of test cases follows.
The only line of each test case contains two integers and () — the size of the matrix.
It is guaranteed that the sum of over all test cases does not exceed .
For each test case, in the first line output a single integer — the maximum beauty of .
Then output the matrix of size — the matrix you find.
If there are multiple solutions, you may output any of them.
Input
The first line of input contains a single integer () — the number of test cases. The description of test cases follows.
The only line of each test case contains two integers and () — the size of the matrix.
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, in the first line output a single integer — the maximum beauty of .
Then output the matrix of size — the matrix you find.
If there are multiple solutions, you may output any of them.
Note
In the first test case:
- ;
- ;
- .
Therefore, .
It can be shown that is the maximum possible beauty of .
In the second test case, any permutation will make .
In the third test case:
- ;
- ;
- ;
- ;
- ;
- .
Therefore, .