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

const int N = 1e5 + 10;

int main() {
	int t;
	cin >> t;
	while (t--) {
		int m, k;
		cin >> m >> k;
		pair <int, int> a[N];
		for (int i = 1; i <= k; ++i) {
			cin >> a[i].first;
			a[i].second = i;
		}
		sort(a + 1, a + k + 1, greater <pair<int, int>> ());
		int mx = a[1].first;
		for (int n = 1; n <= m; ++n) {
			// cout << n << endl;
			if (mx > 1ll* n * ((n + 1) / 2)) continue;
			if (m > 1ll * n * n - 1ll * (n / 2) * (n / 2)) continue;
			vector <pair<int, int>> x, y, z;
			for(int i = 1; i <= n; ++i) 
				for(int j = 1; j <= n; ++j) {
					if((i + j) % 2 == 0) {
						if(i % 2 == 1) z.push_back({i, j});
					} else {
						if(j % 2 == 0) x.push_back({i, j});
						else y.push_back({i, j});
					}
				}
			int ans[n + 1][n + 1];
			memset(ans, 0, sizeof(ans));
			cout << n << endl;
			for(int i = 1; i <= k; ++i) {
				vector<pair<int, int>> *cur = &(x.empty() ? y : x);
				while(a[i].first && !cur->empty()) {
					pair <int, int> now = cur->back();
					cur->pop_back();
					ans[now.first][now.second] = a[i].second;
					a[i].first --;
				}
				while(a[i].first) {
					pair <int, int> now = z.back();
					z.pop_back();
					ans[now.first][now.second] = a[i].second;
					--a[i].first;
				}
				
			}
			for(int i = 1; i <= n; ++i) {
				for(int j = 1; j <= n; ++j) {
					cout << ans[i][j] << " ";
				}
				cout << endl;
			}
			break;
		}
	}
}