#include <iostream>
using namespace std;
typedef long long ll;
int main(){
int t, n, m;
cin >> t;
while (t--){
cin >> n >> m;
if (m >= 2 * n) cout << "No\n";
else{
cout << "Yes\n";
for (int i = 0; i < 2 * n; ++i){
for (int j = 0; j < m; ++j) cout << (j + i) / 2 % n + 1 << " ";
cout << "\n";
}
}
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKaW50IG1haW4oKXsKICAgIGludCB0LCBuLCBtOwogICAgY2luID4+IHQ7CiAgICB3aGlsZSAodC0tKXsKICAgICAgICBjaW4gPj4gbiA+PiBtOwogICAgICAgIGlmIChtID49IDIgKiBuKSBjb3V0IDw8ICJOb1xuIjsKICAgICAgICBlbHNlewogICAgICAgICAgICBjb3V0IDw8ICJZZXNcbiI7CiAgICAgICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgMiAqIG47ICsraSl7CiAgICAgICAgICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IG07ICsraikgY291dCA8PCAoaiArIGkpIC8gMiAgJSBuICsgMSA8PCAiICI7CiAgICAgICAgICAgICAgICBjb3V0IDw8ICJcbiI7CiAgICAgICAgICAgIH0KICAgICAgICB9Cgl9Cn0=