#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
#define CRP(t, x) const t &x
#define OPL(t, x) bool operator<(CRP(t, x)) const
#define FIL(x, v) memset(x, v, sizeof(x))
#define CLR(x) FIL(x, 0)
#define NE1(x) FIL(x, -1)
#define INF(x) FIL(x, 0x3f)
typedef long long ll, i64;
const int N = 1e5 + 50;
vector<int> slots[N];
int main()
{
    int Tt, n, k;
    scanf("%d", &Tt);
    while (Tt--)
    {
        scanf("%d%d", &n, &k);
        if ((n + 1ll) * n / 2 % k)
            puts("no");
        else
        {
            puts("yes");
            for (int i = 0; i < k; i++) slots[i].clear();
            int st = 1;
            if (n / k & 1)
            {
                for (int i = 0; i < k; i++) slots[i].push_back(i + 1);
                for (int i = 0; i < k; i++)
                    slots[i].push_back(k + (k / 2 + i) % k + 1);
                for (int i = 0; i < k; i++)
                    slots[i].push_back(3ll * (1 + 3 * k) / 2 - slots[i][0] - slots[i][1]);
                st += 2;
            }
            for (int i = (st - 1) * 3 / 2 * k + 1; i <= n; i++) slots[(i - 1) % k].push_back(i);
            for (int i = st; i < n / k; i += 2)
                for (int p = 0, q = k; p < q;)
                    swap(slots[p++][i], slots[--q][i]);
            ll sum = (n + 1ll) * n / 2 / k;
            for (int i = 0; i < k; i++)
                for (int j = 0; j < n / k; j++)
                    printf("%d%c", slots[i][j], " \n"[j + 1 == n / k]);
        }
    }
    return 0;
}