#include <bits/stdc++.h>
using namespace std;

int m, k;
vector<int> q;
vector<int> ids;
vector<vector<int>> b;

int tryBuild(int n) {
    int n2 = n / 2, n2_ = (n + 1) / 2;
    int zeros = n2 * n2;
    int any = n2_ * n2_;
    int imp = n2 * n2_;
    if (m > 2 * imp + any) return 0;
    if (q[ids[0]] > any + imp) return 0;
    cerr << n << " :n\n";
    b.assign(n, vector<int>(n, 0));
    int i = 0;
    auto nxt = [&]() -> int {
        while (i < k && q[ids[i]] == 0) i++;
        if (i == k) return 0;
        q[ids[i]]--;
        return ids[i] + 1;
    };

    for (int i = 0; i < n; i += 2) for (int j = 1; j < n; j += 2) b[i][j] = nxt();
    for (int i = 0; i < n; i += 2) for (int j = 0; j < n; j += 2) b[i][j] = nxt();
    for (int i = 1; i < n; i += 2) for (int j = 0; j < n; j += 2) b[i][j] = nxt();

    return 1;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int t;
    cin >> t;
    while (t--) {
        cin >> m >> k;
        q.assign(k, 0);
        for (int i = 0; i < k; i++) cin >> q[i];
        
        ids.clear();
        for (int i = 0; i < k; i++) ids.push_back(i);
        sort(ids.begin(), ids.end(), [&](int i, int j) { return q[i] > q[j]; });

        int n = 1;
        while (!tryBuild(n)) n++;

        cout << n << "\n";
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) cout << b[i][j] << " ";
            cout << "\n";
        }
    }
}
