#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
const int inf = 0x3f3f3f3f;
const LL infll = 0x3f3f3f3f3f3f3f3f;
const LL mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int maxn = 2e6 + 5;
using namespace std;
bool cmp(int x, int y)
{
return x > y;
}
int a[10005], ans[10005][105];
int main()
{
int T, N;
scanf("%d", &T);
for(int ca = 1; ca <= T;ca++)
{
scanf("%d", &N);
for (int i = 1;i <= N;i++)
{
scanf("%d", &a[i]);
}
sort(a + 1, a + 1 + N, cmp);
int n, m;
for (int i = 1; i*i <=N ;i++)
{
if (N % i == 0)
{
n = i;
m = N / n;
}
}
int x = 1;
int r = m, c = n;
for (int i = 1;i <= (n + 1) / 2;i++)
{
for (int j = i, k = i;k <= c && x<=N;k++)
{
ans[j][k] = a[x++];
}
if (x == N + 1)break;
for (int j = i+1, k = c;j <= r && x <= N;j++)
{
ans[j][k] = a[x++];
}
if (x == N + 1)break;
for (int j = r, k = c - 1;k >= i && x <= N;k--)
{
ans[j][k] = a[x++];
}
if (x == N + 1)break;
for (int j = r - 1, k = i;j >= i + 1 && x <= N;j--)
{
ans[j][k] = a[x++];
}
if (x == N + 1)break;
c--;
r--;
if (r <= 0)break;
}
for (int i = 1;i <= m;i++)
{
for (int j = 1;j <= n;j++)
{
printf("%d", ans[i][j]);
if (j == n)printf("\n");
else printf(" ");
}
}
if (ca != T) printf("\n");
}
return 0;
}