using namespace std;

#include <iostream>
#include <algorithm>
#include <vector>

#define MAXN 100005

int M,K,ans[1005][1005];

void solve() {
	cin >> M >> K;
	int mx = 0;
	vector<pair<int,int>> cnt;
	for(int i = 1;i <= K;++i) {
		int c;
		cin >> c;
		mx = max(mx,c);
		cnt.push_back({c,i});
	}
	sort(cnt.begin(),cnt.end(),[&](pair<int,int> p,pair<int,int> p2) {return p.first > p2.first;});
	for(int i = 1;i <= M;++i) {
		if(mx > i * ((i + 1) / 2)) continue;
		if(M > i * i - (i / 2) * (i / 2)) continue;
		vector<pair<int,int>> blue,yellow,red;
		for(int j = 0;j < i;++j) {
			for(int k = 0;k < i;++k) {
				ans[j][k] = 0;
				if((j + k) % 2 == 1) {
					if(j % 2 == 0) {
						yellow.push_back({j,k});
					}else{
						red.push_back({j,k});
					}
				}else{
					if(j % 2 == 0) {
						blue.push_back({j,k});
					}
				}
			}
		}


		for(int j = 0;j < K;++j) {
			vector<pair<int,int>> &cur = (yellow.empty() ? red : yellow);
			while(cnt[j].first && !cur.empty()) {
				pair<int,int> pos = cur.back();
				ans[pos.first][pos.second] = cnt[j].second;
				cur.pop_back();
				// cout << "Hello" << pos.first << " " << pos.second << " " << cnt[j].second << endl;
				cnt[j].first--;
			}
			while(cnt[j].first--) {
				pair<int,int> pos = blue.back();
				ans[pos.first][pos.second] = cnt[j].second;
				blue.pop_back();
			}
		}
		cout << i << endl;
		for(int j = 0;j < i;++j) {
			for(int k = 0;k < i;++k) {
				cout << ans[j][k] << " ";
			}
			cout << endl;
		}
		break;
	}
	return;
}

int main() {
	int T;
	cin >> T;
	while(T--) solve();
}