#include <bits/stdc++.h>
using namespace std;
#define int long long
void read (int &x) {
    char ch = getchar(); x = 0; while (!isdigit(ch)) ch = getchar();
    while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
} const int N = 2e5 + 5, M = 2000, NN = N * 5;
int n, s, mx, a[N], res[M][M], sr, sb, sy, id[N];
pair<int, int> r[NN], b[NN], y[NN];
#define xx first
#define yy second
int get () {
    int l = 0, r = 1000, mid, res = 0;
    while (l <= r) {
        mid = l + r >> 1;
        if (s <= mid * mid - (mid / 2) * (mid / 2) && a[1] <= mid * ((mid + 1) / 2))
            res = mid, r = mid - 1;
        else l = mid + 1;
    }
    return res;
}
signed main() {
    int T; read (T);
    while (T--) {
        read (s), read (n); mx = 0;
        for (int i = 1; i <= n; ++i) {
            read (a[i]), id[i] = i;
            if (a[i] > a[mx]) mx = i;
        }
        id[1] = mx, id[mx] = 1; swap (a[1], a[mx]);
        int len = get (); sr = sb = sy = 0;
        for (int i = 2; i <= len; i += 2)
            for (int j = 1; j <= len; j += 2) r[++sr] = {i, j};
        for (int i = 1; i <= len; i += 2)
            for (int j = 1; j <= len; j += 2) b[++sb] = {i, j};
        for (int i = 1; i <= len; i += 2)
            for (int j = 2; j <= len; j += 2) y[++sy] = {i, j};
        for (int i = 1; i <= len; ++i)
            for (int j = 1; j <= len; ++j) res[i][j] = 0;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= a[i]; ++j) {
                if (sr) res[r[sr].xx][r[sr].yy] = id[i], --sr;
                else if (sb) res[b[sb].xx][b[sb].yy] = id[i], --sb;
                else res[y[sy].xx][y[sy].yy] = id[i], --sy;
            }
        }
        printf ("%d\n", len);
        for (int i = 1; i <= len; ++i) {
            for (int j = 1; j <= len; ++j) printf ("%d ", res[i][j]); puts ("");
        }
    }
    return 0;
}
