for _ in range(int(input())):
n, m = map(int, input().split())
seq_in = [] seq_out = [] ss = [] for i in range(n):
s = list(map(int, input().split()))
seq_in.append(s)
seq_out.append([0] * len(s)) ss.extend(s)
ss.sort()
for i in range(m):
p = ss.pop(0) for j in range(n):
if p in seq_in[j]:
seq_in[j].remove(p)
seq_out[j][i] = p
break
for j in range(n):
for i in range(m):
if seq_out[j][i] == 0:
seq_out[j][i] = seq_in[j].pop()
for line in seq_out:
print(' '.join(map(str, line)))
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//#define ll long long
#define yugant ios_base::sync_with_stdio(false);
#define manoj cin.tie(NULL);
#define shewale cout.tie(NULL);
#define vi std::vector<int>
#define vvi std::vector<vi>
#define pii pair<int,int>
#define vii std::vector<pii>
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define yes cout<<"YES\n";
#define TxtIO freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
#define no cout<<"NO\n";
#define ml '\n'
#define one cout<<"1\n";
#define negone cout<<"-1\n";
#define zero cout<<"0\n";
#define out(x) cout<<x<<'\n';
#define act cout<<"acitvated\n";
#define rep(i,a,b) for (int i = a; i < b; i++)
#define copyarray(a,n) for (int i = 0; i < n; ++i){ cin>>a[i];}
#define printarray(a,n) for (int i = 0; i < n; ++i){cout<<a[i]<<' ';}
#define line cout<<'\n';
#define setBits(x) builtin_popcount(x)
#define rfor(i,a,b) for(int i = a; i >= b;i--)
#define yrn(jack) cout<<(jack ? "YES\n" : "NO\n");
#define pri(x,y) cout<<(x)<<' '<<(y)<<"\n";
#define sz(x) x.size()
#define cr7 cout<<"------------------\n";
const int MOD = 1e9+7;
int N = 1000000;
bool sortbysecdesc(const pair<int,int> &a,const pair<int,int> &b){
return a.second>b.second;
}
bool sortbysec(const pair<int,int> &a,
const pair<int,int> &b)
{
return (a.second < b.second);
}
bool isPrime(ll n){
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
bool isPerfectSquare(long double x){
if (x >= 0) {
long long sr = sqrt(x);
return (sr * sr == x);
}
return false;
}
template<typename T, size_t N>
int binary_search(T (&a)[N], const T& x)
{
int l = 0;
int r = N - 1;
while (l <= r) {
int mid = l + (r-l) / 2;
if (a[mid] == x)
return mid;
else if (a[mid] < x)
l = mid + 1;
else
r = mid - 1;
}
return -1;
}
ll nCr(ll n, ll r){
long long p = 1, k = 1;
if (n - r < r)
r = n - r;
if (r != 0) {
while (r) {
p *= n;
k *= r;
long long m = __gcd(p, k);
p /= m;
k /= m;
n--;
r--;
}
}
else
p = 1;
return p;
}
int binpow(int x, int y)
{
int temp;
if( y == 0)
return 1;
temp = pow(x, y / 2);
if (y % 2 == 0)
return temp * temp;
else
return x * temp * temp;
}
int kadanes(int a[], int size)
{
int ans = INT_MIN, currsum = 0;
for (int i = 0; i < size; i++) {
currsum = currsum + a[i];
if (ans < currsum)
ans = currsum;
if (currsum < 0)
currsum = 0;
}
return ans;
}
bool PrimeSieve[1000005]; // 1e6+5
/*void buildSieve(){
for(int i=2;i<=sz;i++) PrimeSieve[i]=1;
PrimeSieve[0]=0;//
PrimeSieve[1]=0; // 1 is neither prime nor composite
for(int i=2;i<sz;i++){
if(PrimeSieve[i]==0) continue; // the current number itself is composite
for(int j=2;j*i<sz;j++){
PrimeSieve[i*j]=0;
}
}
}*/
bool ispal(string s){
string g = s; reverse(all(g)); return g == s;
}
ll computeXOR(ll n)
{
if (n % 4 == 0)
return n;
if (n % 4 == 1)
return 1;
if (n % 4 == 2)
return n + 1;
return 0;
}
//////////////////template by yogomate/////////////////////////////
ll n,m,x,k;
ll ans = 0,cnt = 0;
string s,s1,s2,s3,s4;
void solve(int _) {
int n,m;
cin>>n>>m;
int a[n][m], ans[n][m];
memset(ans,-1,sizeof(ans));
vector<pair<int,pair<int,int>>> nums;
rep(i,0,n){
rep(j,0,m){
cin>>a[i][j];
nums.pb({a[i][j],{i,j}});
}
}
sort(all(nums));
for (int i = 0; i < m; ++i)
{
int row = nums[i].ss.ff;
int col = nums[i].ss.ss;
int number = nums[i].ff;
ans[row][i] = number;
a[row][col] = -1;
}
for (int i = 0; i < n; ++i)
{
int idx = 0;
rep(j,0,m){
while(ans[i][idx] != -1){
idx++;
}
if(a[i][j] == -1)
continue;
ans[i][idx++] = a[i][j];
}
}
rep(i,0,n){
rep(j,0,m)
cout<<ans[i][j]<<' ';
line
}
}
signed main() {
yugant manoj shewale
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
int _; cin>>_;
for(int i = 1;i<=_;i++)
solve(_);
return 0;
}
/*
*/
1553C - Penalty | 1474E - What Is It |
1335B - Construct the String | 1004B - Sonya and Exhibition |
1397A - Juggling Letters | 985C - Liebig's Barrels |
115A - Party | 746B - Decoding |
1424G - Years | 1663A - Who Tested |
1073B - Vasya and Books | 195B - After Training |
455A - Boredom | 1099A - Snowball |
1651D - Nearest Excluded Points | 599A - Patrick and Shopping |
237A - Free Cash | 1615B - And It's Non-Zero |
1619E - MEX and Increments | 34B - Sale |
1436A - Reorder | 1363C - Game On Leaves |
1373C - Pluses and Minuses | 1173B - Nauuo and Chess |
318B - Strings of Power | 1625A - Ancient Civilization |
864A - Fair Game | 1663B - Mike's Sequence |
448A - Rewards | 1622A - Construct a Rectangle |