// Mohib Manva.
#include <bits/stdc++.h>
#include <random>
using namespace std;
#define ll long long
#define all(vc) vc.begin(), vc.end()
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
int main() {
int t = 1;
scanf("%d", &t);
while (t--) {
int n, m;
scanf("%d %d", &n, &m);
if (m > 2 * n) {
puts("NO");
continue;
}
puts("YES");
if (m <= n) {
for (int i = 1; i <= 2 * n; i++) {
for (int j = 1; j <= m; j++) {
printf("%d ", j);
}
puts("");
}
continue;
}
for (int i = 1; i <= n; i++) {
int st1 = i;
int st2 = 1;
for (int j = 1; j <= m; j += 2) {
printf("%d ", st1);
if (j + 1 <= m) {
printf("%d ", st2);
}
if (st1 == n) {
st1 = 1;
} else {
st1++;
}
if (st2 == n) {
st2 = 1;
} else {
st2++;
}
}
puts("");
st1 = i;
st2 = 2;
for (int j = 1; j <= m; j += 2) {
printf("%d ", st1);
if (j + 1 <= m) {
printf("%d ", st2);
}
if (st1 == n) {
st1 = 1;
} else {
st1++;
}
if (st2 == n) {
st2 = 1;
} else {
st2++;
}
}
puts("");
}
}
return 0;
}
Ly8gTW9oaWIgTWFudmEuCiNpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgojaW5jbHVkZSA8cmFuZG9tPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwogCiNkZWZpbmUgbGwgbG9uZyBsb25nCiNkZWZpbmUgYWxsKHZjKSB2Yy5iZWdpbigpLCB2Yy5lbmQoKQogCm10MTk5MzdfNjQgUk5HKGNocm9ubzo6c3RlYWR5X2Nsb2NrOjpub3coKS50aW1lX3NpbmNlX2Vwb2NoKCkuY291bnQoKSk7CiAKaW50IG1haW4oKSB7CglpbnQgdCA9IDE7CglzY2FuZigiJWQiLCAmdCk7Cgl3aGlsZSAodC0tKSB7CgkJaW50IG4sIG07CgkJc2NhbmYoIiVkICVkIiwgJm4sICZtKTsKIAoJCWlmIChtID4gMiAqIG4pIHsKCQkJcHV0cygiTk8iKTsKCQkJY29udGludWU7CgkJfQ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