#include<iostream> #include<cstdlib> #include<cstdio> #include<algorithm> #include<cstring> usingnamespacestd; inlineintread() { int f = 1, x = 0; char ch;
do{ ch = getchar(); if (ch == '-') f = -1; }while(ch < '0' || ch > '9'); do{ x = x * 10 + ch - '0'; ch = getchar(); }while(ch >= '0' && ch <= '9'); return f * x; } constint N = 500; constint M = 1e5;
int tt; int n, m, k; structNode { int num, cnt; inlinebooloperator <(Node x) const { return cnt > x.cnt; } } node[M + 1]; int w[M + 1]; int ans[N + 1][N + 1];
intmain() { tt = read();
while (tt--) { m = read(); k = read(); for (int i = 1; i <= k; i++) node[i] = { i, read() }; sort(node + 1, node + k + 1); for (int i = 1, j = 0; i <= k; i++) for (int k = 1; k <= node[i].cnt; k++) w[++j] = node[i].num; if (m == 1) { printf("1\n"); printf("%d\n", node[1].num); continue; } n = 1; while (n * n - (n / 2) * (n / 2) < m || n * ((n + 1) / 2) < node[1].cnt) n++; printf("%d\n", n); memset(ans, 0, sizeof(int) * (N + 1) * (N + 1)); int i = 1; for (int x = 2, y = 1; i <= m && x <= n; i++) { ans[x][y] = w[i]; y += 2; if (y > n) { x += 2; y = 1; } } for (int x = 1, y = 1; i <= m && x <= n; i++) { ans[x][y] = w[i]; y += 2; if (y > n) { x += 2; y = 1; } } for (int x = 1, y = 2; i <= m && x <= n; i++) { ans[x][y] = w[i]; y += 2; if (y > n) { x += 2; y = 2; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) printf("%d ", ans[i][j]); printf("\n"); } }