// Source: https://usaco.guide/general/io
#include <bits/stdc++.h>
using namespace std;
#define int long long
int mex(vector<int> &arr, int N)
{
// sort the array
sort(arr.begin(), arr.end());
int mex = 0;
for (int idx = 0; idx < N; idx++)
{
if (arr[idx] == mex)
{
// Increment mex
mex += 1;
}
}
// Return mex as answer
return mex;
}
int calcbeaut(vector<vector<int>> arr, int coloms, int rows){
vector<int> mexv;
for (int i=0; i < coloms; i++){
vector<int> v;
for (int j = 0; j < rows; j++){
v.push_back(arr[j][i]);
}
mexv.push_back(mex(v, v.size()));
}
for (int i = 0; i < mexv.size(); i++){
//cout << mexv[i] << "\n";
}
return mex(mexv, mexv.size());
}
signed main() {
int t;
cin >> t;
while (t--){
int n,m;
cin >> n >> m;
int dupn = n;
n = min(n,m);
vector<vector<int>> ans;
ans.push_back({});
for (int i = 0; i< dupn; i++){
ans.push_back({});
for (int j = 0; j < m; j++){
ans[i].push_back(0);
}
}
m-=1;
// for (int i =0; i < n; i++){
// ans[i][0] = m-1;
// }
int awf, bawf;
vector<vector<int>> aww;
awf = -1;
bawf = -1;
for (int i = 0; i < n; i++){
for (int j = i; j < m; j++){
ans[i][j] = j-i;
}
}
if (m>4){
if (true){
for (int i = 1; i < n; i++){
for (int j = i-1; j >=0; j--){
ans[i][j] = m-(m-(m-(j)-1)-1)-1;
if (ans[i][j] == j+1){
ans[i][j] += 1;
//ans[i][j-1] = 10;
awf = i;
bawf = j-1;
aww.push_back({awf,bawf});
}
}
}}
else{
for (int i = 1; i < n; i++){
for (int j = i-1; j >=0; j--){
ans[i][j] = (m-(m-(j)-1)-1);
}
//
}
}
for (int i =0; i < n; i++){
ans[i][ans[i].size()-1] = ans[i].size()-1;
}
for (int i = 1; i < n; i++){
//ans[i][0] = m-1;
}
int ababb = 0;
if (n > 2){
//ans[n-1][m-1] = 0;
}
}
else if (m == 4){
vector<vector<int>> answer = {
{0, 1, 2, 3, 4},
{3, 0, 1, 2, 4},
{2, 3, 0, 1, 4},
{2, 3, 1, 0, 4},
{2, 3, 1, 0, 4},
};
n = 5;
for (int i = 0; i < min(dupn,n); i++){
ans[i] = answer[i];
}
}
else if (m == 3){
vector<vector<int>> answer = {
{0, 1, 2, 3},
{2, 0, 1, 3},
{2, 1, 0, 3},
{2, 1, 0, 3},
};
n = 4;
for (int i = 0; i < min(dupn,n); i++){
ans[i] = answer[i];
}
}
else if (m == 2){
vector<vector<int>> answer = {
{1,0,2},
{0,2,1}
};
n = 2;
ans[0] = answer[0];
ans[1] = answer[1];
for (int i =0; i < min(dupn,n); i++){
ans[i] = answer[i];
}
}
else if (m == 1){
vector<vector<int>> answer = {
{0, 1}
};
n = 1;
ans[0] = answer[0];
}
else{
vector<vector<int>> answer = {
{0}
};
n = 1;
ans[0] = answer[0];
}
vector<int> model = ans[0];
for (int i = n; i < dupn; i++){
ans[i] = model;
}
for (int i = 0; i < aww.size(); i++){
ans[aww[i][0]][aww[i][1]] -= 1;
}
//cout << n+1 << "\n";
//cout << "\n";
cout << calcbeaut(ans, ans[0].size(), ans.size()-1) << "\n";
for (int i = 0; i < dupn; i++){
for (int j = 0; j < ans[i].size(); j++){
cout << ans[i][j] << " ";
}
cout << "\n";
}
}
}
242. Valid Anagram | 141. Linked List Cycle |
21. Merge Two Sorted Lists | 203. Remove Linked List Elements |
733. Flood Fill | 206. Reverse Linked List |
83. Remove Duplicates from Sorted List | 116. Populating Next Right Pointers in Each Node |
145. Binary Tree Postorder Traversal | 94. Binary Tree Inorder Traversal |
101. Symmetric Tree | 77. Combinations |
46. Permutations | 226. Invert Binary Tree |
112. Path Sum | 1556A - A Variety of Operations |
136. Single Number | 169. Majority Element |
119. Pascal's Triangle II | 409. Longest Palindrome |
1574A - Regular Bracket Sequences | 1574B - Combinatorics Homework |
1567A - Domino Disaster | 1593A - Elections |
1607A - Linear Keyboard | EQUALCOIN Equal Coins |
XOREQN Xor Equation | MAKEPAL Weird Palindrome Making |
HILLSEQ Hill Sequence | MAXBRIDGE Maximise the bridges |