#include <bits/stdc++.h>
using namespace std;
// #Sorting #Theory
void swap(int &x,int &y){
int t=x;
x=y;
y=t;
}
void BubbleSort(vector<int> &a,int n){ // passing vector by reference
if(n==1){
return; // Base case: if one elements array is already sorted
}
int swap_flag=0;
for(int j=0;j<n-1;j++){ //x=0 to x=n-2
if(a[j]>a[j+1]){
swap(a[j],a[j+1]);
swap_flag=1;
}
}
if(swap_flag==0){ // if no swap occured then the array is already sorted
return;
}
BubbleSort(a,n-1); // as the largest elements is moved toward right
// sort the remaining first n-1 elements
}
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;
BubbleSort(a,n); //Sort first n elements
cout<<"Sorted: ";
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
return 0;
}