#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MOD = 1e9 + 7;
void solve(){
int n, m;
cin >> n >> m;
if(m >= 2 * n){
cout << "NO\n";
return;
}
cout << "YES\n";
vector<vector<int>> ans(2 * n, vector<int>(2 * n - 1));
for(int i = 0; i < 2 * n - 1; i++){
int start = i;
int col = 1;
for(int j = 0; j < 2 * n - 1; j++){
ans[i][start] = col;
start++;
start %= (2 * n - 1);
if(j % 2){
col++;
}
}
}
int col = 1;
for(int i = 0; i < 2 * n - 1; i++){
ans[2 * n - 1][(i + 1) % (2 * n - 1)] = col;
if(i < n - 1 || !(i % 2))col++;
col = min(col, n);
}
if(n == 2){
ans[2][0] = 2;
ans[2][1] = 2;
ans[2][2] = 1;
ans[3][0] = 1;
ans[3][1] = 2;
ans[3][2] = 1;
}
for(int i = 0; i < 2 * n; i++){
for(int j = 0; j < m; j++){
cout << ans[i][j] << " ";
}
cout << "\n";
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
cin >> t;
for(int i = 1; i <= t; i++){
solve();
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgbGwgbG9uZyBsb25nCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY29uc3QgaW50IE1PRCA9IDFlOSArIDc7Cgp2b2lkIHNvbHZlKCl7CglpbnQgbiwgbTsKCWNpbiA+PiBuID4+IG07CglpZihtID49IDIgKiBuKXsKCQljb3V0IDw8ICJOT1xuIjsKCQlyZXR1cm47Cgl9Cgljb3V0IDw8ICJZRVNcbiI7Cgl2ZWN0b3I8dmVjdG9yPGludD4+IGFucygyICogbiwgdmVjdG9yPGludD4oMiAqIG4gLSAxKSk7CgkKCWZvcihpbnQgaSA9IDA7IGkgPCAyICogbiAtIDE7ICBpKyspewoJCWludCBzdGFydCA9IGk7CgkJaW50IGNvbCA9IDE7CgkJZm9yKGludCBqID0gMDsgaiA8IDIgKiBuIC0gMTsgaisrKXsKCQkJ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