#include <cstdio>
#include <algorithm>
using namespace std;
#define MAXN 100005
int t, n, m, k;
int a[MAXN];
int order[MAXN];
int tot[MAXN], single[MAXN];
int maxn;

bool cmp(int x, int y)
{
	return a[x] > a[y];
}

void init()
{
	for(int i = 1; i < MAXN; i ++)
	{
		if(i & 1)
		{
			tot[i] = tot[i - 1] + i + i - 1;
			single[i] = single[i - 1] + i + i / 2;
		}else{
			tot[i] = tot[i - 1] + i;
			single[i] = single[i - 1] + i / 2;
		}
		if(single[i] > MAXN)
		{
			maxn = i;
			return;
		}
	}

}

int ans[5005][5005];
int main()
{
	init();
	scanf("%d", &t);
	while(t --)
	{
		scanf("%d%d", &m, &k);
		for(int i = 1; i <= k; i ++) {scanf("%d", a + i); order[i] = i;}
		sort(order + 1, order + k + 1, cmp);
		if(m <= 1)
		{
			printf("1\n%d\n", order[1]);
			continue;
		}	
		int l = 0, r = maxn, mid;
		while(l + 1 < r)
		{
			mid = (l + r) / 2;
			if(tot[mid] >= m && single[mid] >= a[order[1]]) r = mid;
		       	else l = mid;
		}
		int cur, curx = 2, cury = 1, startx = 1, starty = 1;
		for(int i = 1; i <= r; i ++)
			for(int j = 1; j <= r; j ++)
				ans[i][j] = 0;
		for(int i = 1; i <= k; i ++)
		{
			cur = order[i];
			while(a[cur])
			{
				ans[curx][cury] = cur;
				curx += 2;
				if(curx > r)
				{
					curx = starty ^ 3;
					cury += 2;
					if(cury > r)
					{
						curx = startx;
						cury = starty;
						startx = 1;
						starty = 2;
					}
				}
				a[cur] --;
			}
		}
		printf("%d\n", r);
		for(int i = 1; i <= r; i ++)
		{
			for(int j = 1; j <= r; j ++)
			{
				printf("%d ", ans[i][j]);
			}
			putchar('\n');
		}


	}
	return 0;
}

