#include <bits/stdc++.h>
using namespace std;
// #Sorting #Theory
int partition(vector<int> &a,int l,int r){
// taking last element as the pivot
// intialize pi partition index as starting element l
// if we come accross an element less than pivot,
// (while traversing from i=l to i=pivot-1) swap it with pi and move pi to right
// else don't change pi
// so in the end pi is the place of pivot so we can swap it with pivot
// return pivot index after arranging elements around it
int pi=l;
int pivot=a[r];
for(int i=l;i<r;i++){ // not including pivot, i<r
if(a[i]<pivot){
swap(a[pi],a[i]);
pi++;
}
}
swap(a[pi],a[r]); // a[r] is the pivot
return pi; // returning pivot after elements are arranged around it
}
void QuickSort(vector<int> &a,int l,int r){ // passing vector by reference
if(l<r){ // Base case: size>1 or l<r
int pi=partition(a,l,r);
/*
Target of partitions is, given an array and an element x of array as pivot,
put x at its correct position in sorted array and put all smaller elements
(smaller than x) before x, and put all greater elements (greater than x) after x.
All this should be done in linear time.
pi is the index after the partition operation
*/
QuickSort(a,l,pi-1); // Quick Sorting left of pivot
QuickSort(a,pi+1,r); // Quick Sorting right of pivot
}
}
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
vector<int> a(n);
for(int i=0;i<n;i++){
cin>>a[i];
}
cout<<"Original: ";
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
QuickSort(a,0,n-1);
cout<<"Sorted: ";
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
return 0;
}