#include <bits/stdc++.h>
using namespace std;
int main() {
	cin.tie(nullptr)->sync_with_stdio(false);
	int tt;
	cin >> tt;
	while (tt--) {
		int m, k;
		cin >> m >> k;
		vector<int> a(k);
		for (int i = 0; i < k; i++) {
			cin >> a[i];
		}
		vector<int> p(k);
		iota(p.begin(), p.end(), 0);
		sort(p.begin(), p.end(), [&](const int &x, const int &y) {
			return a[x] > a[y];
		});
		int n = 0;
		auto check = [&](int n) {
			if (n * n - (n / 2) * (n / 2) < m) {
				return false;
			} else if (n * ((n + 1) / 2) < a[p[0]]) {
				return false;
			} else {
				return true;
			}
		};
		while (!check(++n));
		vector<vector<int>> ans(n, vector<int>(n, -1));
		vector<tuple<int, int>> position;
		for (int i = 0; i < n; i += 2) {
			for (int j = 1; j < n; j += 2) {
				position.emplace_back(i, j);
			}
		}
		for (int i = 0; i < n; i += 2) {
			for (int j = 0; j < n; j += 2) {
				position.emplace_back(i, j);
			}
		}
		for (int i = 1; i < n; i += 2) {
			for (int j = 0; j < n; j += 2) {
				position.emplace_back(i, j);
			}
		}
		int pos = 0;
		for (auto &x : p) {
			while (a[x]--) {
				int x0, y0;
				tie(x0, y0) = position[pos++];
				ans[x0][y0] = x;
			}
		}
		cout << n << "\n";
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				cout << ans[i][j] + 1 << " \n"[j + 1 == n];
			}
		}
	}
}
