#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i<(int)n; ++i)
#define forr(i, t, n) for (int i = t; i<n; ++i)
#define rmod(x, y) (((x%y)+y)%y)
#define pb push_back
#define emp emplace_back
#define ppb pop_back
#define len(v) int(v.size())
#define all(v) (v).begin(),(v).end()
#define ri(x) scanf("%d",&(x))
#define rii(x,y) scanf("%d %d",&(x),&(y))
#define riii(x,y,z) scanf("%d %d %d",&(x),&(y),&(z))
#define rl(x) scanf("%lld",&(x))
#define rll(x,y) scanf("%lld %lld",&(x),&(y))
#define rlll(x,y,z) scanf("%lld %lld %lld",&(x),&(y),&(z))
using namespace std;
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;
void fastio(){ios_base::sync_with_stdio(false); cin.tie(0);}

const long double PI = acos(-1);
const long long MOD = 1000000000 +7;
const int maxn = 1024;

int main() {
	int tc;
	ri(tc);
	while (tc--){
		int n;
		ri(n);
		int a[n];
		forn(i, n) ri(a[i]);
		int swaps = n-1;
		for(int i = 0; i<n and swaps>0; i++){
			int p = find(a, a+n, i+1) -a;
			while (swaps>0 and p>i){
				swap(a[p], a[p-1]); swaps--; p--;
			}
		}
		forn(i, n) printf("%d ", a[i]);
		puts("");
	}

	return 0;
}


#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i<(int)n; ++i)
#define forr(i, t, n) for (int i = t; i<n; ++i)
#define rmod(x, y) (((x%y)+y)%y)
#define pb push_back
#define emp emplace_back
#define ppb pop_back
#define len(v) int(v.size())
#define all(v) (v).begin(),(v).end()
#define ri(x) scanf("%d",&(x))
#define rii(x,y) scanf("%d %d",&(x),&(y))
#define riii(x,y,z) scanf("%d %d %d",&(x),&(y),&(z))
#define rl(x) scanf("%lld",&(x))
#define rll(x,y) scanf("%lld %lld",&(x),&(y))
#define rlll(x,y,z) scanf("%lld %lld %lld",&(x),&(y),&(z))
using namespace std;
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;
void fastio(){ios_base::sync_with_stdio(false); cin.tie(0);}

const long double PI = acos(-1);
const long long MOD = 1000000000 +7;
const int maxn = 1024;

int main() {
	int tc;
	ri(tc);
	while (tc--){
		int n;
		ri(n);
		int a[n];
		bool operations[n-1];
		memset(operations, 0, sizeof(operations));
		forn(i, n) ri(a[i]);
		int swaps = n-1;
		int mxp = find(a, a+n, 1)-a;
		int rp = mxp;
		while (mxp>0 and swaps>0){
			swap(a[mxp], a[mxp-1]);
			operations[mxp] = true;
			swaps--; mxp--;
		}
		for(int i = n-1; i>rp and swaps>0; i--){
			if (a[i]<a[i-1]) {
				swap(a[i], a[i-1]);
				swaps--;
			}
		}
		forn(i, n) printf("%d ", a[i]);
		puts("");
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int q;
    cin >> q;
    for (int test = 0; test < q; test++) {
        int n;
        cin >> n;
        vector <int> a(n);
        for (auto & elem : a) {
            cin >> elem;
        }
        int j = n-1;
        int counter = 0;
        while(j) {
            int k = min_element(a.begin() + counter, a.end()) - a.begin();
            if (k == n-1) {
                break;
            }
            if (k != counter) {
                while(j && k >= 1 && a[k-1] > a[k]) {
                    swap(a[k], a[k-1]);
                    j--;
                    k--;
                }
            }
            counter++;
        }
        for (auto elem : a) {
                cout << elem << " ";
        }
        cout << '\n';
    }
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int q;
    cin >> q;
    for (int test = 0; test < q; test++) {
        int n;
        cin >> n;
        vector <int> a(n);
        for (auto & elem : a) {
            cin >> elem;
        }
        int j = n-1;
        for (int counter = 0; counter < n; counter++) {
            int k = min_element(a.begin() + counter, a.end()) - a.begin();
            while (k >= 1 && a[k-1] > a[k] && j) {
                    swap(a[k-1], a[k]);
                    j--;
                    k--;
            }
        }
        for (auto elem : a) {
            cout << elem << " ";
        }
        cout << '\n';
    }
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int q;
    cin >> q;
    for (int test = 0; test < q; test++) {
        int n;
        cin >> n;
        vector <int> a(n);
        for (auto & elem : a) {
            cin >> elem;
        }
        int j = n-1;
        for (int counter = 0; counter < n; counter++) {
            int k = min_element(a.begin() + counter, a.end()) - a.begin();
            while (k >= 1 && a[k-1] > a[k] && j) {
                    swap(a[k-1], a[k]);
                    j--;
                    k--;
            }
            counter = k;
        }
        for (auto elem : a) {
            cout << elem << " ";
        }
        cout << '\n';
    }
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int q;
    cin >> q;
    for (int test = 0; test < q; test++) {
        int n;
        cin >> n;
        vector <int> a(n);
        for (auto & elem : a) {
            cin >> elem;
        }
        int j = n-1;
        int counter = 0;
        while(j) {
            int k = min_element(a.begin() + counter, a.end()) - a.begin();
            if (k == n) {
                break;
            }
            if (k != counter) {
                while(j && k >= 1 && a[k-1] > a[k]) {
                    swap(a[k], a[k-1]);
                    j--;
                    k--;
                }
            }
            counter++;
        }
        for (auto elem : a) {
                cout << elem << " ";
        }
        cout << '\n';
    }
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int q;
    cin >> q;
    for (int test = 0; test < q; test++) {
        int n;
        cin >> n;
        vector <int> a(n);
        for (auto & elem : a) {
            cin >> elem;
        }
        int j = n-1;
        for (int counter = 0; counter < n; counter++) {
            int k = min_element(a.begin() + counter, a.end()) - a.begin();
            int new_counter = k;
            while (k >= 1 && k >= counter && a[k-1] > a[k] && j) {
                    swap(a[k-1], a[k]);
                    j--;
                    k--;
            }
            counter = new_counter;
        }
        for (auto elem : a) {
            cout << elem << " ";
        }
        cout << '\n';
    }
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int q;
    cin >> q;
    for (int test = 0; test < q; test++) {
        int n;
        cin >> n;
        vector <int> a(n);
        for (auto & elem : a) {
            cin >> elem;
        }
        int j = n-1;
        for (int counter = 0; counter < n; counter++) {
            int k = min_element(a.begin() + counter, a.end()) - a.begin();
            counter = k;
            while (k >= 1 && a[k-1] > a[k] && j) {
                    swap(a[k-1], a[k]);
                    j--;
                    k--;
            }
        }
        for (auto elem : a) {
            cout << elem << " ";
        }
        cout << '\n';
    }
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int q;
    cin >> q;
    for (int test = 0; test < q; test++) {
        int n;
        cin >> n;
        vector <int> a(n);
        for (auto & elem : a) {
            cin >> elem;
        }
        int j = n-1;
        for (int counter = 0; counter < n; counter++) {
            int k = min_element(a.begin() + counter, a.end()) - a.begin();
            int new_counter = k;
            while (k > counter && a[k-1] > a[k] && j) {
                    swap(a[k-1], a[k]);
                    j--;
                    k--;
            }
            counter = new_counter;
        }
        for (auto elem : a) {
            cout << elem << " ";
        }
        cout << '\n';
    }
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int q;
    cin >> q;
    for (int test = 0; test < q; test++) {
        int n;
        cin >> n;
        vector <int> a(n);
        for (auto & elem : a) {
            cin >> elem;
        }
        int j = n-1;
        int counter = 0;
        while(j && counter != n-1) {
            int k = min_element(a.begin() + counter, a.end()) - a.begin();
            if (k != counter) {
                while(j && k >= 1 && a[k-1] > a[k]) {
                    swap(a[k], a[k-1]);
                    j--;
                    k--;
                }
            }
            counter++;
        }
        for (auto elem : a) {
                cout << elem << " ";
        }
        cout << '\n';
    }
	return 0;
}


#include<bits/stdc++.h>
#include<iostream>
#define pb push_back
#define ll long long
#define mod 1000000007
using namespace std;

int main() {
	ll int qq;
	cin>>qq;
	while(qq--) {
		ll int n;
		cin>>n;
		ll int arr[n];
		for(ll int i=0; i<n; i++)
			cin>>arr[i];
		ll int index = 1;
		ll int i=0;
		for(i=0; i<n; i++) {
			if(arr[i] == index){
				index++;
			} else {
				break;
			}
		}
		ll int chances = n-1;
		while(chances>0 && i<(n-1)) {
			ll int j;
			int flag = 0;
			for(j = i; j<n; j++) {
				if(arr[j] == index) {
					flag = 1;
					ll int l;
					for(l =j; l>i && chances>0; l--) {
						arr[l] = arr[l-1];
						chances--;
					}
					arr[l] = index;
					index++;
					if(i == j)
						i = j+1;
					else
						i = j;
					break;
				}
			}
			if(flag==0)
				index++;
		}
		for(int i=0; i<n; i++)
			cout<<arr[i]<<" ";
		cout<<endl;
	}
	return 0;
}

// int main() {
// 	ll int qq;
// 	while(qq--) {
// 		ll int n, k;
// 		cin>>n>>k;
// 		string ss
// 		cin>>ss;
// 		map<ll int, ll int> mp;
// 		for(int i=1;i<=n; i++)
// 			mp.insert({1, 0});
// 		for(int i=0; i<n; i++){
// 			if(ss[i])
// 				mp[i+1] = 1;
// 		}
// 		vector<ll int> vc;
// 		for(auto i: mp){
// 			if(i.second == 1){
// 				vc.pb(i.first);
// 			}
// 		}
// 	}
// 	return 0;
// }


#include<bits/stdc++.h>
#include<iostream>
#define pb push_back
#define ll long long
#define mod 1000000007
using namespace std;

int main() {
	ll int qq;
	cin>>qq;
	while(qq--) {
		ll int n;
		cin>>n;
		ll int arr[n];
		for(ll int i=0; i<n; i++)
			cin>>arr[i];
		ll int index = 1;
		ll int i=0;
		for(i=0; i<n; i++) {
			if(arr[i] == index){
				index++;
			} else {
				break;
			}
		}

		ll int chances = n-1;
		while(chances>0 && i!=n && index <=n) {
			ll int j;
			for(j = i; j<n; j++) {
				if(arr[j] == index) {
					ll int l;
					for(l =j; l>i && chances>0; l--) {
						arr[l] = arr[l-1];
						chances--;
					}
					arr[l] = index;
					index++;
					i = l+1;
					break;
				}
			}
		}

		for(int i=0; i<n; i++)
			cout<<arr[i]<<" ";
		cout<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#include<iostream>
#define pb push_back
#define ll long long
#define mod 1000000007
using namespace std;

int main() {
	ll int qq;
	cin>>qq;
	while(qq--) {
		ll int n;
		cin>>n;
		ll int arr[n];
		for(ll int i=0; i<n; i++)
			cin>>arr[i];
		ll int index = 1;
		ll int i=0;
		for(i=0; i<n; i++) {
			if(arr[i] == index){
				index++;
			} else {
				break;
			}
		}

		ll int chances = n-1;
		while(chances>0 && i!=(n-1)) {
			ll int j;
			for(j = i; j<n; j++) {
				if(arr[j] == index) {
					ll int l;
					for(l =j; l>i && chances>0; l--) {
						arr[l] = arr[l-1];
						chances--;
					}
					arr[l] = index;
					index++;
					i = l+1;
					break;
				}
			}
		}

		for(int i=0; i<n; i++)
			cout<<arr[i]<<" ";
		cout<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#include<iostream>
#define pb push_back
#define ll long long
#define mod 1000000007
using namespace std;

int main() {
	ll int qq;
	cin>>qq;
	while(qq--) {

		ll int n;
		cin>>n;
		ll int arr[n];
		for(ll int i=0; i<n; i++)
			cin>>arr[i];
		ll int index = 1;
		ll int i=0;
		for(i=0; i<n; i++) {
			if(arr[i] == index){
				index++;
			} else {
				break;
			}
		}

		ll int chances = n-1;
		while(chances>0 && i<(n-1)) {
			ll int j;
			int flag = 0;
			for(j = i; j<n; j++) {
				if(arr[j] == index) {
					flag = 1;
					ll int l;
					for(l =j; l>i && chances>0; l--) {
						arr[l] = arr[l-1];
						chances--;
					}
					arr[l] = index;
					index++;
					i = j;
					break;
				}
			}
			if(flag==0)
				index++;
		}

		for(int i=0; i<n; i++)
			cout<<arr[i]<<" ";
		cout<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#include<iostream>
#define pb push_back
#define ll long long
#define mod 1000000007
using namespace std;

int main() {
	ll int qq;
	cin>>qq;
	while(qq--) {
		ll int n;
		cin>>n;
		ll int arr[n];
		for(ll int i=0; i<n; i++)
			cin>>arr[i];
		ll int index = 1;
		ll int i=0;
		// for(i=0; i<n; i++) {
		// 	if(arr[i] == index){
		// 		index++;
		// 	} else {
		// 		break;
		// 	}
		// }
		ll int chances = n-1;
		while(chances>0 && i<(n-1)) {
			ll int j;
			int flag = 0;
			for(j = i; j<n; j++) {
				if(arr[j] == index && i==j) {
					i++;
					index++;
				} else if(arr[j] == index) {
					flag = 1;
					ll int l;
					for(l =j; l>i && chances>0; l--) {
						arr[l] = arr[l-1];
						chances--;
					}
					arr[l] = index;
					if(l>0 && (arr[l] < arr[l-1]))
						swap(arr[l], arr[l-1]);
					index++;
					i = j+1;
					break;
				}
			}
			if(flag==0)
				index++;
		}
		for(int i=0; i<n; i++)
			cout<<arr[i]<<" ";
		cout<<endl;
	}
	return 0;
}

// int main() {
// 	ll int qq;
// 	while(qq--) {
// 		ll int n, k;
// 		cin>>n>>k;
// 		string ss
// 		cin>>ss;
// 		map<ll int, ll int> mp;
// 		for(int i=1;i<=n; i++)
// 			mp.insert({1, 0});
// 		for(int i=0; i<n; i++){
// 			if(ss[i])
// 				mp[i+1] = 1;
// 		}
// 		vector<ll int> vc;
// 		for(auto i: mp){
// 			if(i.second == 1){
// 				vc.pb(i.first);
// 			}
// 		}
// 	}
// 	return 0;
// }


#include<bits/stdc++.h>
#include<iostream>
#define pb push_back
#define ll long long
#define mod 1000000007
using namespace std;

int main() {
	ll int qq;
	cin>>qq;
	while(qq--) {
		ll int n;
		cin>>n;
		ll int arr[n];
		for(ll int i=0; i<n; i++)
			cin>>arr[i];
		ll int index = 1;
		ll int i=0;
		for(i=0; i<n; i++) {
			if(arr[i] == index){
				index++;
			} else {
				break;
			}
		}

		ll int chances = n-1;
		while(chances>0 && i!=n && index <=n) {
			ll int j;
			int flag = 0;
			for(j = i; j<n; j++) {
				if(arr[j] == index) {
					flag = 1;
					ll int l;
					for(l =j; l>i && chances>0; l--) {
						arr[l] = arr[l-1];
						chances--;
					}
					arr[l] = index;
					index++;
					i = j+1;
					break;
				}
			}
			if(flag==0)
				index++;
		}

		for(int i=0; i<n; i++)
			cout<<arr[i]<<" ";
		cout<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#include<iostream>
#define pb push_back
#define ll long long
#define mod 1000000007
using namespace std;

int main() {
	ll int qq;
	cin>>qq;
	while(qq--) {
		ll int n;
		cin>>n;
		ll int arr[n];
		for(ll int i=0; i<n; i++)
			cin>>arr[i];
		ll int index = 1;
		ll int i=0;
		for(i=0; i<n; i++) {
			if(arr[i] == index){
				index++;
			} else {
				break;
			}
		}
		ll int chances = n-1;
		while(chances>0 && i<(n-1)) {
			ll int j;
			int flag = 0;
			for(j = i; j<n; j++) {
				if(arr[j] == index) {
					flag = 1;
					ll int l;
					for(l =j; l>i && chances>0; l--) {
						arr[l] = arr[l-1];
						chances--;
					}
					arr[l] = index;
					index++;
					if(arr[j] == index)
						i = j+1;
					else
						i = j;
					break;
				}
			}
			if(flag==0)
				index++;
		}
		for(int i=0; i<n; i++)
			cout<<arr[i]<<" ";
		cout<<endl;
	}
	return 0;
}

// int main() {
// 	ll int qq;
// 	while(qq--) {
// 		ll int n, k;
// 		cin>>n>>k;
// 		string ss
// 		cin>>ss;
// 		map<ll int, ll int> mp;
// 		for(int i=1;i<=n; i++)
// 			mp.insert({1, 0});
// 		for(int i=0; i<n; i++){
// 			if(ss[i])
// 				mp[i+1] = 1;
// 		}
// 		vector<ll int> vc;
// 		for(auto i: mp){
// 			if(i.second == 1){
// 				vc.pb(i.first);
// 			}
// 		}
// 	}
// 	return 0;
// }


#include<bits/stdc++.h>
#include<iostream>
#define pb push_back
#define ll long long
#define mod 1000000007
using namespace std;

int main() {
	ll int qq;
	cin>>qq;
	while(qq--) {
		ll int n;
		cin>>n;
		ll int arr[n];
		for(ll int i=0; i<n; i++)
			cin>>arr[i];
		ll int index = 1;
		ll int i=0;
		for(i=0; i<n; i++) {
			if(arr[i] == index){
				index++;
			} else {
				break;
			}
		}

		ll int chances = n-1;
		while(chances>0 && i!=n && index <=n) {
			ll int j;
			int flag = 0;
			for(j = i; j<n; j++) {
				if(arr[j] == index) {
					flag = 1;
					ll int l;
					for(l =j; l>i && chances>0; l--) {
						arr[l] = arr[l-1];
						chances--;
					}
					arr[l] = index;
					index++;
					i = j;
					break;
				}
			}
			if(flag==0)
				index++;
		}

		for(int i=0; i<n; i++)
			cout<<arr[i]<<" ";
		cout<<endl;
	}
	return 0;
}


#include <stdio.h>
int main() {
    short int q;
    scanf("%hi",&q);
    while(q--){
        short int n,A[100];
        scanf("%hi",&n);
        for(short int i=0;i<n;i++) scanf("%hi",A+i);
        short int l=0,r=0,cur=1;
        while(r<n){
            if(A[r]==cur){
                printf("%hi ",cur);
                A[r]=0;
                for(short int i=l;i<r-1;i++)
                    printf("%hi ",A[i]);
                if(l!=r) A[r]=A[r-1];
                l=r+(l==r);
                cur++;
            }
            r++;
        }
        if(A[n-1]) printf("%hi\n",A[n-1]);
        else printf("\n");
    }
}


#include <stdio.h>
int main() {
    short int q;
    scanf("%hi",&q);
    while(q--){
        short int n,A[100],Ind[100],r=0;
        scanf("%hi",&n);
        for(short int i=1;i<=n;i++){
            scanf("%hi",A+i-1);
            Ind[A[i-1]-1]=i;
        }

        for(int i=0;i<n;i++){
            if(r<Ind[i]){
                printf("%hi ",i+1);
                for(int j=r;j<Ind[i]-2;j++)
                    printf("%hi ",A[j]);

                if(Ind[i]!=r+1){
                    Ind[A[Ind[i]-2]-1]=Ind[i];
                    A[Ind[i]-1]=A[Ind[i]-2];
                    r=Ind[i]-1;
                }
                else {
                    r=Ind[i];
                }
            }
        }
        printf("\n");
    }
}


#include <stdio.h>
int main() {
    short int q;
    scanf("%hi",&q);
    while(q--){
        short int n,A[100],Ind[100],r=0;
        scanf("%hi",&n);
        for(short int i=1;i<=n;i++){
            scanf("%hi",A+i-1);
            Ind[A[i-1]-1]=i;
        }

        bool b=1;

        for(int i=0;i<n;i++){
            if(r<Ind[i]){
                printf("%hi ",i+1);
                for(int j=r;j<Ind[i]-2;j++)
                    printf("%hi ",A[j]);

                if(Ind[i]!=r+1){
                    A[Ind[i]-1]=A[Ind[i]-2];
                    r=Ind[i]-1;
                }
                else {
                    if(i==n-1) b=0;

                    r=Ind[i];
                }
            }
        }
        if(b) printf("%hi",A[n-1]);
        printf("\n");
    }
}


#include <stdio.h>
int main() {
    short int q;
    scanf("%hi",&q);
    while(q--){
        short int n,A[100];
        bool B[100];
        scanf("%hi",&n);
        for(short int i=0;i<n;i++) {
            scanf("%hi",A+i);
            B[i]=0;
        }
        short int l=0,r=0,cur=1;
        while(r<n){
            //printf("%hi (%hi)\n",A[r],cur);
            if(A[r]==cur){
                printf("%hi ",cur);
                B[A[r]-1]=1;
                for(short int i=l;i<r-1;i++){
                    printf("%hi ",A[i]);
                    B[A[i]-1]=1;
                }
                if(l!=r){
                    A[r]=A[r-1];
                    A[r-1]=0;
                }
                l=r+(l==r);
                cur++;
                while(B[cur-1] && cur<=n) cur++;
            }
            r++;
        }
        for(int i=0;i<n;i++)
            if(A[i] && !B[A[i]-1]) printf("%hi ",A[i]);
        printf("\n");
    }
}


#include <stdio.h>
int main() {
    short int q;
    scanf("%hi",&q);
    while(q--){
        short int n,A[100],Ind[100],r=0;
        scanf("%hi",&n);
        for(short int i=1;i<=n;i++){
            scanf("%hi",A+i-1);
            Ind[A[i-1]-1]=i;
        }

        int last=0;

        for(int i=0;i<n;i++){
            if(r<Ind[i]){
                printf("%hi ",i+1);
                for(int j=r;j<Ind[i]-2;j++)
                    printf("%hi ",A[j]);
                if(Ind[i]!=r+1){
                    A[Ind[i]-1]=A[Ind[i]-2];
                    r=Ind[i]-1;
                    last=A[Ind[i]-3];
                }
                else {
                    last=A[Ind[i]-1];
                    r=Ind[i];
                }
            }
        }
        if(last!=A[n-1]) printf("%hi",A[n-1]);
        printf("\n");
    }
}


#include <stdio.h>
int main() {
    short int q;
    scanf("%hi",&q);
    while(q--){
        short int n,A[100],Ind[100],r=0;
        scanf("%hi",&n);
        for(short int i=1;i<=n;i++){
            scanf("%hi",A+i-1);
            Ind[A[i-1]-1]=i;
        }

        bool b=1;

        for(int i=0;i<n;i++){
            if(r<Ind[i]){
                printf("%hi ",i+1);
                for(int j=r;j<Ind[i]-2;j++)
                    printf("%hi ",A[j]);

                if(Ind[i]!=r+1){
                    Ind[A[Ind[i]-2]-1]=Ind[i];
                    A[Ind[i]-1]=A[Ind[i]-2];
                    r=Ind[i]-1;
                }
                else {
                    if(i==n-1) b=0;

                    r=Ind[i];
                }
            }
        }
        if(b) printf("%hi",A[n-1]);
        printf("\n");
    }
}


#include <stdio.h>
int main() {
    short int q;
    scanf("%hi",&q);
    while(q--){
        short int n,A[100];
        scanf("%hi",&n);
        for(short int i=0;i<n;i++) scanf("%hi",A+i);
        short int l=0,r=0,cur=1;
        while(r<n){
            if(A[r]==cur){
                printf("%hi ",cur);
                for(short int i=l;i<r;i++)
                    printf("%hi ",A[i]);
                l=r+1;
                cur++;
            }
            r++;
        }
        printf("\n");
    }
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define f(i, x, n) for(int i = x; i < (int)(n); ++i)
#define all(c) (c).begin(), (c).end()
#define speed ios::sync_with_stdio(0); cin.tie(0);

int const N = 2 * 1e5 + 2;
bool fd[101];

void solve(){
    int n;
    cin >> n;
    f(i, 0, n + 1)fd[i] = false;
    string s = "";
    f(i, 0, n){
        char x;
        cin >> x;
        s += x;
    }
    int fn = n;
    f(i, 1, n + 1){
        int ps;
        f(j, 0, s.size())if (s[j] - '0' == i)ps = j;
        while(ps && fn != 1){
            if ((s[ps - 1] - '0') > (s[ps] - '0') && !fd[ps - 1]){
                fd[ps - 1] = true;
                swap(s[ps - 1], s[ps]);
                ps--;
                fn--;
            } else break;
        }
    }
    f(i, 0, s.size())cout << s[i] << " ";
    cout << endl;
}

int main(){
    speed
    int t;
    cin >> t;
    while(t--)solve();
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define f(i, x, n) for(int i = x; i < (int)(n); ++i)
#define all(c) (c).begin(), (c).end()
#define speed ios::sync_with_stdio(0); cin.tie(0);

int const N = 100 + 2;
bool fd[N];

void solve(){
    int n;
    cin >> n;
    f(i, 0, n + 1)fd[i] = false;
    string s = "";
    f(i, 0, n){
        char x;
        cin >> x;
        s += x;
    }
    int fn = n;
    f(i, 1, n + 1){
        int ps;
        f(j, 0, s.size())if (s[j] - '0' == i)ps = j;
        while(ps && fn != 1){
            if ((s[ps - 1] - '0') > (s[ps] - '0') && !fd[ps - 1]){
                fd[ps - 1] = true;
                swap(s[ps - 1], s[ps]);
                ps--;
                fn--;
            } else break;
        }
    }
    f(i, 0, s.size())cout << s[i] << " ";
    cout << endl;
}

int main(){
    speed
    int t;
    cin >> t;
    while(t--)solve();
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define f(i, x, n) for(int i = x; i < (int)(n); ++i)
#define all(c) (c).begin(), (c).end()
#define speed ios::sync_with_stdio(0); cin.tie(0);

int const N = 100 + 2;
bool fd[N];

void solve(){
    int n;
    cin >> n;
    f(i, 0, n + 1)fd[i] = false;
    string s = "";
    f(i, 0, n){
        char x;
        cin >> x;
        s += x;
    }
    int fn = n;
    f(i, 1, n + 1){
        int ps = 0;
        f(j, 0, s.size())if (s[j] - '0' == i)ps = j;
        while(ps && fn != 1){
            if (s[ps - 1] > s[ps] && !fd[ps - 1]){
                fd[ps - 1] = true;
                swap(s[ps - 1], s[ps]);
                ps--;
                fn--;
            } else break;
        }
    }
    f(i, 0, s.size())cout << s[i] << " ";
    cout << endl;
}

int main(){
    speed
    int t;
    cin >> t;
    while(t--)solve();
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define f(i, x, n) for(int i = x; i < (int)(n); ++i)
#define all(c) (c).begin(), (c).end()
#define speed ios::sync_with_stdio(0); cin.tie(0);

int const N = 100 + 2;
bool fd[N];

void solve(){
    int n;
    cin >> n;
    f(i, 0, 101)fd[i] = false;
    string s = "";
    f(i, 0, n){
        char x;
        cin >> x;
        s += x;
    }
    int fn = n;
    f(i, 1, n + 1){
        int ps = 0;
        f(j, 0, s.size())if (s[j] - '0' == i)ps = j;
        if (ps == 0)continue;
        while(ps && fn != 1){
            if (s[ps - 1] > s[ps] && !fd[ps - 1]){
                fd[ps - 1] = true;
                swap(s[ps - 1], s[ps]);
                ps--;
                fn--;
            } else break;
        }
    }
    f(i, 0, s.size())cout << s[i] << " ";
    cout << endl;
}

int main(){
    speed
    int t;
    cin >> t;
    while(t--)solve();
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define f(i, x, n) for(int i = x; i < (int)(n); ++i)
#define all(c) (c).begin(), (c).end()
#define speed ios::sync_with_stdio(0); cin.tie(0);

int const N = 2 * 1e5 + 2;

void solve(){
    int n;
    cin >> n;
    string s = "";
    f(i, 0, n){
        char x;
        cin >> x;
        s += x;
    }
    int fn = n;
    f(i, 1, n + 1){
        int ps;
        f(j, 0, s.size())if (s[j] - '0' == i)ps = j;
        while(ps && fn != 1)if (s[ps - 1] - '0' > s[ps] - '0')swap(s[ps - 1], s[ps]), ps--, fn--; else break;
    }
    f(i, 0, s.size())cout << s[i] << " ";
    cout << endl;
}

int main(){
    speed
    int t;
    cin >> t;
    while(t--)solve();
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define f(i, x, n) for(int i = x; i < (int)(n); ++i)
#define all(c) (c).begin(), (c).end()
#define speed ios::sync_with_stdio(0); cin.tie(0);

int const N = 100 + 2;
bool fd[N];

void solve(){
    int n;
    cin >> n;
    f(i, 0, 101)fd[i] = false;
    string s = "";
    f(i, 0, n){
        char x;
        cin >> x;
        s += x;
    }
    int fn = n;
    f(i, 1, n + 1){
        int ps = 0;
        f(j, 0, s.size())if (s[j] - '0' == i)ps = j;
        while(ps && fn != 1){
            if (s[ps - 1] > s[ps] && !fd[ps - 1]){
                fd[ps - 1] = true;
                swap(s[ps - 1], s[ps]);
                ps--;
                fn--;
            } else break;
        }
    }
    f(i, 0, s.size())cout << s[i] << " ";
    cout << endl;
}

int main(){
    speed
    int t;
    cin >> t;
    while(t--)solve();
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define f(i, x, n) for(int i = x; i < (int)(n); ++i)
#define all(c) (c).begin(), (c).end()
#define speed ios::sync_with_stdio(0); cin.tie(0);

int const N = 100 + 2;
int a[N];
bool fd[N];

void solve(){
    int n;
    cin >> n;
    f(i, 0, n)fd[i] = false;
    f(i, 0, n)cin >> a[i];
    int fn = n;
    f(i, 1, n + 1){
        int ps = 0;
        f(j, 0, n)if (a[j] == i)ps = j;
        while(ps && fn != 1){
            if (a[ps - 1] > a[ps] && !fd[ps - 1]){
                fd[ps - 1] = true;
                swap(a[ps - 1], a[ps]);
                ps--;
                fn--;
            } else break;
        }
    }
    f(i, 0, n)cout << a[i] << " ";
    cout << endl;
}

int main(){
    speed
    int t;
    cin >> t;
    while(t--)solve();
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define f(i, x, n) for(int i = x; i < (int)(n); ++i)
#define all(c) (c).begin(), (c).end()
#define speed ios::sync_with_stdio(0); cin.tie(0);

int const N = 100 + 2;
bool fd[N];

void solve(){
    int n;
    cin >> n;
    f(i, 0, n + 1)fd[i] = false;
    string s = "";
    f(i, 0, n){
        char x;
        cin >> x;
        s += x;
    }
    int fn = n;
    f(i, 1, n + 1){
        int ps;
        f(j, 0, s.size())if (s[j] - '0' == i)ps = j;
        while(ps && fn != 1){
            if (s[ps - 1] > s[ps] && !fd[ps - 1]){
                fd[ps - 1] = true;
                swap(s[ps - 1], s[ps]);
                ps--;
                fn--;
            } else break;
        }
    }
    f(i, 0, s.size())cout << s[i] << " ";
    cout << endl;
}

int main(){
    speed
    int t;
    cin >> t;
    while(t--)solve();
}


#include <bits/stdc++.h>
using namespace std;

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int q;
	cin >> q;
	while (q--) {
		int n, count = 0, nCnt;
		cin >> n;
		nCnt = n-1;
		vector<int> arr(n + 1);
		vector<bool> brr(n + 1, true);
		for (int i = 1; i <= n; i++)
			cin >> arr[i];

		while (nCnt--) {
			for (int i = n - 1; i >= 1; i--) {
				if (count >= n - 1)
					break;
				if (brr[i] = true && arr[i] > arr[i + 1]) {
					swap(arr[i], arr[i + 1]);
					count++;
					brr[i] = false;
				}
			}
			if (count >= n - 1)
				break;
		}
		for (int i = 1; i <= n; i++) {
			cout << arr[i] << ' ';
		}
		cout << '\n';
	}
}


#include <bits/stdc++.h>
using namespace std;

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int q;
	cin >> q;
	while (q--) {
		int n, arr[101], b[101];
		cin >> n;
		for (int i = 0; i <= n-1; i++)
			cin >> arr[i];
		memset(b, 0, sizeof(b));
		for (int i = n - 2; i >= 0; i--) {
			for (int j = i; j < n - 1; j++) {
				if (arr[j] > arr[j + 1] && b[j] == 0) {
					swap(arr[j], arr[j + 1]);
					b[j] = 1;
				}
			}
		}
		for (int i = 0; i < n; i++) {
			cout << arr[i] << ' ';
		}
		cout << '\n';
	}
}


#include <bits/stdc++.h>
using namespace std;

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int q;
	cin >> q;
	while (q--) {
		int n, count = 0, nCnt;
		cin >> n;
		nCnt = n;
		vector<int> arr(n + 1);
		vector<bool> brr(n);
		for (int i = 1; i <= n; i++)
			cin >> arr[i];

		while (nCnt--) {
			for (int i = n - 1; i >= 1; i--) {
				if (count >= n - 1)
					break;
				if (arr[i] > arr[i + 1]) {
					swap(arr[i], arr[i + 1]);
					count++;
				}
			}
			if (count >= n - 1)
				break;
		}
		for (int i = 1; i <= n; i++) {
			cout << arr[i] << ' ';
		}
		cout << '\n';
	}
}


include <bits/stdc++.h>
using namespace std;

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int q;
	cin >> q;
	while (q--) {
		int n, count = 0, nCnt;
		cin >> n;
		nCnt = n;
		vector<int> arr(n + 1);
		vector<bool> brr(n);
		for (int i = 1; i <= n; i++)
			cin >> arr[i];

		while (nCnt--) {
			for (int i = n - 1; i >= 1; i--) {
				if (count >= n - 1)
					break;
				if (arr[i] > arr[i + 1]) {
					swap(arr[i], arr[i + 1]);
					count++;
				}
			}
			if (count >= n - 1)
				break;
		}
		for (int i = 1; i <= n; i++) {
			cout << arr[i] << ' ';
		}
		cout << '\n';
	}
}


#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int q;
	cin >> q;
	while (q--) {
		int n;
		cin >> n;
		int cnt = n - 1;
		vector<int> arr(n);
		for (int i = 0; i < n; i++)
			cin >> arr[i];
		for (int i = 0; i < cnt; i++) {
			int max = 0, maxIndex = -1;
			for (int j = 0; j < n-1; j++) {
				if (i != cnt-1 && j == 0)
					continue;
				if (arr[j] - arr[j+1] >= max) {
					max = arr[j] - arr[j + 1];
					maxIndex = j;
				}
			}
			if (maxIndex != -1) {
				swap(arr[maxIndex], arr[maxIndex + 1]);
			}
		}
		for (int i = 0; i < n; i++) {
			cout << arr[i] << ' ';
		}
		cout << "\n";
	}
}


#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int q;
	cin >> q;
	while (q--) {
		int n, newI = -1;
		cin >> n;
		int cnt = n - 1;
		vector<int> arr(n);
		for (int i = 0; i < n; i++)
			cin >> arr[i];
		for (int i = 0; i < cnt; i++) {
			int max = 0, maxIndex = -1;
			for (int j = 0; j < n-1; j++) {
				if (arr[j] - arr[j+1] >= max) {
					max = arr[j] - arr[j + 1];
					maxIndex = j;
				}
			}
			if (maxIndex != -1) {
				swap(arr[maxIndex], arr[maxIndex + 1]);
			}
		}
		for (int i = 0; i < n; i++) {
			cout << arr[i] << ' ';
		}
		cout << "\n";
	}
}


#include <bits/stdc++.h>
using namespace std;

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int q;
	cin >> q;
	while (q--) {
		int n, count = 0, nCnt;
		cin >> n;
		nCnt = n;
		vector<int> arr(n + 1);
		vector<bool> brr(n + 1, true);
		for (int i = 1; i <= n; i++)
			cin >> arr[i];

		while (nCnt--) {
			for (int i = n - 1; i >= 1; i--) {
				if (count >= n - 1)
					break;
				if (brr[i] = true && arr[i] > arr[i + 1]) {
					swap(arr[i], arr[i + 1]);
					count++;
					brr[i] = false;
				}
			}
			if (count >= n - 1)
				break;
		}
		for (int i = 1; i <= n; i++) {
			cout << arr[i] << ' ';
		}
		cout << '\n';
	}
}


#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int q;
	cin >> q;
	while (q--) {
		int n, newI = -1;
		cin >> n;
		int cnt = n - 1;
		vector<int> arr(n);
		vector<bool>brr(n, true);
		for (int i = 0; i < n; i++)
			cin >> arr[i];
		for (int i = 0; i < cnt; i++) {
			int max = 0, maxIndex = -1;
			for (int j = 0; j < n-1; j++) {
				if (arr[j] - arr[j+1] >= max && brr[j] == true) {
					max = arr[j] - arr[j + 1];
					maxIndex = j;
				}
			}
			if (maxIndex != -1) {
				swap(arr[maxIndex], arr[maxIndex + 1]);
				brr[maxIndex] = false;
			}
		}
		for (int i = 0; i < n; i++) {
			cout << arr[i] << ' ';
		}
		cout << "\n";
	}
}


#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int q;
	cin >> q;
	while (q--) {
		int n, newI = -1;
		cin >> n;
		int cnt = n - 1;
		vector<int> arr(n);
		for (int i = 0; i < n; i++)
			cin >> arr[i];
		for (int i = 0; i < cnt; i++) {
			int max = 0, maxIndex = -1;
			for (int j = 0; j < n-1; j++) {
				if (i != cnt - 1 && j == 0) {
					continue;
				}
				if (arr[j] - arr[j+1] >= max) {
					max = arr[j] - arr[j + 1];
					maxIndex = j;
				}
			}
			if (maxIndex != -1) {
				swap(arr[maxIndex], arr[maxIndex + 1]);
			}
			else {
				if (i != cnt - 1) {
					newI = cnt - (i + 1);
					break;
				}
			}
		}
		if (newI != -1) {
			for (int i = 0; i < newI; i++) {
				if (arr[i] > arr[i + 1]) {
					swap(arr[i], arr[i + 1]);
				}
			}
		}
		for (int i = 0; i < n; i++) {
			cout << arr[i] << ' ';
		}
		cout << "\n";
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define ll long long
#define ld long double
#define forr(i,k,n) for(ll i=k;i<n;i++)
#define rev(i,k) for(ll i=k;i>=0;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pll pair<ll,ll>
#define MX LLONG_MAX
#define MN LLONG_MIN
#define MOD 1000000007
ll powermodm(ll x,ll n,ll M){ll result=1;while(n>0){if(n % 2 ==1)result=(result * x)%M;x=(x*x)%M;n=n/2;}return result;}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
    ll t;
    cin>>t;
    while(t--){
    	ll n;
    	cin>>n;
    	ll arr[n];
    	unordered_map<ll,ll> ind;
    	forr(i,0,n){
    		cin>>arr[i];
    		ind[arr[i]] = i;
    	}
    	ll start = 0;
    	ll ans[n];
    	forr(i,0,n){
    		ans[i] = -1;
    	}
    	ll st = 0;

    	forr(i,1,n+1){
    		if(start==n-1){
    			if(ind[i]==start){
    				ans[start++] = i;
    				st++;
    			}
    			break;
    		}
    		if(ind[i]>start){
    			ans[start] = i;
    			start = ind[i];
    		}else if(ind[i]==start){
    			ans[start++] =i;
    		}
    		st++;
    	}

    	ll ind1 =0;
    	forr(i,0,n){
    		if(ans[i]!=-1){
    			continue;
    		}
    		while(arr[ind1]<st){
    			ind1++;
    		}
    		ans[i] = arr[ind1++];

    	}
    	forr(i,0,n){
    		cout<<ans[i]<<" ";
    	}
    	cout<<'\n';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define ll long long
#define ld long double
#define forr(i,k,n) for(ll i=k;i<n;i++)
#define rev(i,k) for(ll i=k;i>=0;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pll pair<ll,ll>
#define MX LLONG_MAX
#define MN LLONG_MIN
#define MOD 1000000007
ll powermodm(ll x,ll n,ll M){ll result=1;while(n>0){if(n % 2 ==1)result=(result * x)%M;x=(x*x)%M;n=n/2;}return result;}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
    ll t;
    cin>>t;
    while(t--){
    	ll n;
    	cin>>n;
    	ll arr[n];
    	unordered_map<ll,ll> ind;
    	forr(i,0,n){
    		cin>>arr[i];
    		ind[arr[i]] = i;
    	}
    	ll start = 0;
    	forr(i,1,n+1){
    		ll j=ind[i];
    		ll t1 = j;
    		if(j==start){
    			start++;
    			continue;
    		}
    		if(j<start)
    			continue;
    		while(j>start){
    			ind[arr[j]]--;
    			ind[arr[j-1]]++;
    			swap(arr[j],arr[j-1]);
    			j--;
    		}
    		start = t1;
    	}
    	forr(i,0,n){
    		cout<<arr[i]<<" ";
    	}
    	cout<<'\n';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define ll long long
#define ld long double
#define forr(i,k,n) for(ll i=k;i<n;i++)
#define rev(i,k) for(ll i=k;i>=0;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pll pair<ll,ll>
#define MX LLONG_MAX
#define MN LLONG_MIN
#define MOD 1000000007
ll powermodm(ll x,ll n,ll M){ll result=1;while(n>0){if(n % 2 ==1)result=(result * x)%M;x=(x*x)%M;n=n/2;}return result;}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
    ll t;
    cin>>t;
    while(t--){
    	ll n;
    	cin>>n;
    	ll arr[n];
    	unordered_map<ll,ll> ind;
    	forr(i,0,n){
    		cin>>arr[i];
    		ind[arr[i]] = i;
    	}
    	ll start = 0;
    	ll ans[n];
    	forr(i,0,n){
    		ans[i] = -1;
    	}
    	ll st = 0;

    	forr(i,1,n+1){
    		if(start==n-1){
    			if(ind[i]==start){
    				ans[start++] = i;
    				st++;
    			}
    			break;
    		}
    		if(ind[i]>start){
    			ans[start] = i;
    			start = ind[i];
    		}else if(ind[i]==start){
    			ans[start++] =i;
    		}
    		st++;
    	}

    	ll ind1 =0;
    	forr(i,0,n){
    		if(ans[i]!=-1){
    			continue;
    		}
    		while(ind1<n && arr[ind1]<st){
    			ind1++;
    		}
    		if(ind<n)
    		ans[i] = arr[ind1++];

    	}
    	forr(i,0,n){
    		cout<<ans[i]<<" ";
    	}
    	cout<<'\n';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define ll long long
#define ld long double
#define forr(i,k,n) for(ll i=k;i<n;i++)
#define rev(i,k) for(ll i=k;i>=0;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pll pair<ll,ll>
#define MX LLONG_MAX
#define MN LLONG_MIN
#define MOD 1000000007
ll powermodm(ll x,ll n,ll M){ll result=1;while(n>0){if(n % 2 ==1)result=(result * x)%M;x=(x*x)%M;n=n/2;}return result;}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
    ll t;
    cin>>t;
    while(t--){
    	ll n;
    	cin>>n;
    	ll arr[n];
    	unordered_map<ll,ll> ind;
    	forr(i,0,n){
    		cin>>arr[i];
    		ind[arr[i]] = i;
    	}
    	ll start = 0;
    	ll ans[n];
    	forr(i,0,n){
    		ans[i] = -1;
    	}
    	ll st = 0;

    	forr(i,1,n+1){
    		if(start==n-1){
    			if(ind[i]==start){
    				ans[start++] = i;
    				st++;
    			}
    			break;
    		}
    		if(ind[i]>start){
    			ans[start] = i;
    			start = ind[i];
    		}else if(ind[i]==start){
    			ans[start++] =i;
    		}
    		st++;
    	}

    	ll ind1 =0;
    	forr(i,0,n){
    		if(ans[i]!=-1){
    			continue;
    		}
    		while(ind1<n && arr[ind1]<st){
    			ind1++;
    		}
    		if(ind1<n)
    		ans[i] = arr[ind1++];

    	}
    	forr(i,0,n){
    		cout<<ans[i]<<" ";
    	}
    	cout<<'\n';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define ll long long
#define ld long double
#define forr(i,k,n) for(ll i=k;i<n;i++)
#define rev(i,k) for(ll i=k;i>=0;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pll pair<ll,ll>
#define MX LLONG_MAX
#define MN LLONG_MIN
#define MOD 1000000007
ll powermodm(ll x,ll n,ll M){ll result=1;while(n>0){if(n % 2 ==1)result=(result * x)%M;x=(x*x)%M;n=n/2;}return result;}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
    ll t;
    cin>>t;
    while(t--){
    	ll n;
    	cin>>n;
    	ll arr[n];
    	unordered_map<ll,ll> ind;
    	forr(i,0,n){
    		cin>>arr[i];
    		ind[arr[i]] = i;
    	}
    	ll start = 0;
    	ll ans[n];
    	forr(i,0,n){
    		ans[i] = -1;
    	}
    	ll st = 0;

    	forr(i,1,n+1){
    		if(start>=n-1)
    			break;
    		if(ind[i]>start){
    			ans[start] = i;
    			start = ind[i];
    		}else if(ind[i]==start){
    			ans[start++] =i;
    		}
    		st++;
    	}

    	ll ind1 =0;
    	forr(i,0,n){
    		if(ans[i]!=-1){
    			continue;
    		}
    		while(arr[ind1]<st){
    			ind1++;
    		}
    		ans[i] = arr[ind1++];

    	}
    	forr(i,0,n){
    		cout<<ans[i]<<" ";
    	}
    	cout<<'\n';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define ll long long
#define ld long double
#define forr(i,k,n) for(ll i=k;i<n;i++)
#define rev(i,k) for(ll i=k;i>=0;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pll pair<ll,ll>
#define MX LLONG_MAX
#define MN LLONG_MIN
#define MOD 1000000007
ll powermodm(ll x,ll n,ll M){ll result=1;while(n>0){if(n % 2 ==1)result=(result * x)%M;x=(x*x)%M;n=n/2;}return result;}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
    ll t;
    cin>>t;
    while(t--){
    	ll n;
    	cin>>n;
    	ll arr[n];
    	unordered_map<ll,ll> ind;
    	forr(i,0,n){
    		cin>>arr[i];
    		ind[arr[i]] = i;
    	}
    	ll start = 0;
    	forr(i,1,n+1){
    		ll j=ind[i];
    		if(j==start){
    			start++;
    			continue;
    		}
    		while(j>start){
    			swap(arr[j],arr[j-1]);
    			j--;
    		}
    		start = ind[i];
    	}
    	forr(i,0,n){
    		cout<<arr[i]<<" ";
    	}
    	cout<<'\n';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define ll long long
#define ld long double
#define forr(i,k,n) for(ll i=k;i<n;i++)
#define rev(i,k) for(ll i=k;i>=0;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pll pair<ll,ll>
#define MX LLONG_MAX
#define MN LLONG_MIN
#define MOD 1000000007
ll powermodm(ll x,ll n,ll M){ll result=1;while(n>0){if(n % 2 ==1)result=(result * x)%M;x=(x*x)%M;n=n/2;}return result;}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
    ll t;
    cin>>t;
    while(t--){
    	ll n;
    	cin>>n;
    	ll arr[n];
    	unordered_map<ll,ll> ind;
    	forr(i,0,n){
    		cin>>arr[i];
    		ind[arr[i]] = i;
    	}
    	ll start = 0;
    	forr(i,1,n+1){
    		ll j=ind[i];
    		if(j==start){
    			start++;
    			continue;
    		}
    		if(j<start)
    			continue;
    		while(j>start){
    			swap(arr[j],arr[j-1]);
    			j--;
    		}
    		start = ind[i];
    	}
    	forr(i,0,n){
    		cout<<arr[i]<<" ";
    	}
    	cout<<'\n';
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
   int t;
   cin>>t;
   while(t--)
   {

 int n;
 cin>>n;
 int arr[n];
 for(int i=0;i<n;i++)
 {
    cin>>arr[i];
 }
 int count=n-1;
 for(int i=1;i<=n;i++)
 {
    int j=0;
    for(int l=0;l<n;l++)
    {
       if(arr[l]==i)
       {
          j=l;
          break;

       }
    }
    for(;j>=1;j--)
    {
       if(count==0)
       {
          break;
       }
       if(arr[j-1]>arr[j])
       {
          swap(arr[j-1],arr[j]);
          count--;
       }
    }
    if(count==0)
    break;

 }

 for(int i=0;i<n;i++)
 {
    cout<<arr[i]<<" ";
 }
 cout<<endl;

   }

   return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
   int t;
   cin>>t;
   while(t--)
   {

 int n;
 cin>>n;
 int arr[n];
 for(int i=0;i<n;i++)
 {
    cin>>arr[i];
 }
 int count=n-1;
 int k=1;
 for(int i=n-1;i>=1;i--)
 {
    int j=0;
    for(int l=0;l<n;l++)
    {
       if(arr[l]==k)
       {
          j=l;
          k++;
          break;
       }
    }
    for(;j>=1;j--)
    {
       if(count==0)
       {
          break;
       }
       if(arr[j-1]>arr[j])
       {
          swap(arr[j-1],arr[j]);
          count--;
       }
    }
    if(count==0)
    break;
 }

 for(int i=0;i<n;i++)
 {
    cout<<arr[i]<<" ";
 }
 cout<<endl;

   }

   return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
   int t;
   cin>>t;
   while(t--)
   {

 int n;
 cin>>n;
 int arr[n];
 for(int i=0;i<n;i++)
 {
    cin>>arr[i];
 }
 int count=n-1;
 for(int i=n-1;i>=1;i--)
 {
    for(int j=i;j>=1;j--)
    {
       if(count==0)
       {
          break;
       }
       if(arr[j-1]>arr[j])
       {
          swap(arr[j-1],arr[j]);
          count--;
       }
    }
    if(count==0)
    break;
 }

 for(int i=0;i<n;i++)
 {
    cout<<arr[i]<<" ";
 }
 cout<<endl;

   }

   return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
   int t;
   cin>>t;
   while(t--)
   {

 int n;
 cin>>n;
 int arr[n];
 for(int i=0;i<n;i++)
 {
    cin>>arr[i];
 }
 int count=n-1;
 int k=1;
 for(int i=n-1;i>=1;i--)
 {
    int j=0;
    for(int l=0;l<n;l++)
    {
       if(arr[l]==k)
       {
          j=l;
          break;

       }
    }
    for(;j>=1;j--)
    {
       if(count==0)
       {
          break;
       }
       if(arr[j-1]>arr[j])
       {
          swap(arr[j-1],arr[j]);
          count--;
       }
    }
    if(count==0)
    break;
    k++;
 }

 for(int i=0;i<n;i++)
 {
    cout<<arr[i]<<" ";
 }
 cout<<endl;

   }

   return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
   int t;
   cin>>t;
   while(t--)
   {

 int n;
 cin>>n;
 int arr[n];
 for(int i=0;i<n;i++)
 {
    cin>>arr[i];
 }
 int count=n-1;
 int k=1;
 for(int i=n-1;i>=1;i--)
 {
    int j=0;
    for(int l=0;l<n;l++)
    {
       if(arr[i]==k)
       {
          j=k;
          k++;
          break;
       }
    }
    for(;j>=1;j--)
    {
       if(count==0)
       {
          break;
       }
       if(arr[j-1]>arr[j])
       {
          swap(arr[j-1],arr[j]);
          count--;
       }
    }
    if(count==0)
    break;
 }

 for(int i=0;i<n;i++)
 {
    cout<<arr[i]<<" ";
 }
 cout<<endl;

   }

   return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
   int t;
   cin>>t;
   while(t--)
   {

 int n;
 cin>>n;
 int arr[n];
 for(int i=0;i<n;i++)
 {
    cin>>arr[i];
 }

 vector<bool> visited(n,false);

 for(int i=n-2;i>=0;i--)
 {

    for(int j=i;j<n-1;j++)
    {
       if(visited[j]==false && arr[j]>arr[j+1])
       {
          swap(arr[j],arr[j+1]);
          visited[j]=true;
       }
    }
 }

 for(int i=0;i<n;i++)
 {
    cout<<arr[i]<<" ";
 }
 cout<<endl;

   }

   return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
   int t;
   cin>>t;
   while(t--)
   {

 int n;
 cin>>n;
 int arr[n];
 for(int i=0;i<n;i++)
 {
    cin>>arr[i];
 }
 int count=n-1;
 int k=1;
 for(int i=n-1;i>=1;i--)
 {
    int j=0;
    for(int l=0;l<n;l++)
    {
       if(arr[l]==k)
       {
          j=l;
          break;

       }
    }
    for(;j>=1;j--)
    {
       if(count==0)
       {
          break;
       }
       if(arr[j-1]>arr[j])
       {
          swap(arr[j-1],arr[j]);
          count--;
       }
    }
    if(count==0)
    break
    k++;
 }

 for(int i=0;i<n;i++)
 {
    cout<<arr[i]<<" ";
 }
 cout<<endl;

   }

   return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
   int t;
   cin>>t;
   while(t--)
   {

 int n;
 cin>>n;
 int arr[n];
 for(int i=0;i<n;i++)
 {
    cin>>arr[i];
 }
 int count=n-1;
 for(int i=n-1;i>=1;i--)
 {
    for(int j=n-1;j>=1;j--)
    {
       if(count==0)
       {
          break;
       }
       if(arr[j-1]>arr[j])
       {
          swap(arr[j-1],arr[j]);
          count--;
       }
    }
    if(count==0)
    break;
 }

 for(int i=0;i<n;i++)
 {
    cout<<arr[i]<<" ";
 }
 cout<<endl;

   }

   return 0;
}


#include <bits/stdc++.h>

#ifndef ONLINE_JUDGE
	#define W(x, y) cerr << "\033[31m" << #x << " = " << x << "\033[0m" << y;
#else
	#define W(x, y)
#endif

#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vii vector<pii>
#define vl vector<ll>
#define vll vector<pll>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi acos(-1)
#define ld long double
#define prime (1000000000 + 7)

using namespace std;

int main(){
	ios::sync_with_stdio(false);
 	cin.tie(0); cout.tie(0);

	ll q;
	cin >> q;

	while(q-->0){
		ll n;
		cin >> n;

		vl vet(n);
		vl res(n);
		for(int i = 0; i < n; ++i){
			cin >> vet[i];
		}

		ll aux = 0;
		// set<pll> sete;

		for(int i = 0; i < n && aux != n-1; ++i){
			pll menor{0x3f3f3f3f, {}};
			for(int j = i; j < n; ++j){
				menor = min(menor, {vet[j], j});
			}
			for(int j = menor.ss; j > i && aux != n-1 && j - i < n - aux; --j){
				aux++;
				swap(vet[j], vet[j-1]);
			}
			i = max(menor.ss - 1, (long long)i);
		}

		for(int i = 0; i < n; ++i){
			cout << vet[i] << " \n"[i + 1 == n];
		}

	}

	return 0;
}


#include <bits/stdc++.h>

#ifndef ONLINE_JUDGE
	#define W(x, y) cerr << "\033[31m" << #x << " = " << x << "\033[0m" << y;
#else
	#define W(x, y)
#endif

#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vii vector<pii>
#define vl vector<ll>
#define vll vector<pll>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi acos(-1)
#define ld long double
#define prime (1000000000 + 7)

using namespace std;

int main(){
	ios::sync_with_stdio(false);
 	cin.tie(0); cout.tie(0);

	ll q;
	cin >> q;

	while(q-->0){
		ll n;
		cin >> n;

		vl vet(n);
		vl res(n);
		for(int i = 0; i < n; ++i){
			cin >> vet[i];
		}

		ll aux = 0;

		for(int i = 0; i < n && aux != n-1; ++i){
			pll menor{0x3f3f3f3f, {}};
			for(int j = i; j < n; ++j){
				menor = min(menor, {vet[j], j});
			}
			for(int j = menor.ss; j > i && aux != n-1; --j){
				aux++;
				swap(vet[j], vet[j-1]);
			}
		}

		for(int i = 0; i < n; ++i){
			cout << vet[i] << " \n"[i + 1 == n];
		}

	}

	return 0;
}


#include <bits/stdc++.h>

#ifndef ONLINE_JUDGE
	#define W(x, y) cerr << "\033[31m" << #x << " = " << x << "\033[0m" << y;
#else
	#define W(x, y)
#endif

#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vii vector<pii>
#define vl vector<ll>
#define vll vector<pll>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi acos(-1)
#define ld long double
#define prime (1000000000 + 7)

using namespace std;

int main(){
	ios::sync_with_stdio(false);
 	cin.tie(0); cout.tie(0);

	ll q;
	cin >> q;

	while(q-->0){
		ll n;
		cin >> n;

		vl vet(n);
		vl res(n);
		for(int i = 0; i < n; ++i){
			cin >> vet[i];
		}

		ll aux = 0;

		for(int i = 0; i < n && aux != n-1; ++i){
			pll menor{0x3f3f3f3f, {}};
			for(int j = i+1; j < n && aux != n-1; ++j){
				menor = min(menor, {vet[j], j});
			}
			for(int j = menor.ss; j > 0 && aux != n-1; --j){
				aux++;
				swap(vet[j], vet[j-1]);
			}
		}

		for(int i = 0; i < n; ++i){
			cout << vet[i] << " \n"[i + 1 == n];
		}

	}

	return 0;
}


#include <bits/stdc++.h>

#ifndef ONLINE_JUDGE
	#define W(x, y) cerr << "\033[31m" << #x << " = " << x << "\033[0m" << y;
#else
	#define W(x, y)
#endif

#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vii vector<pii>
#define vl vector<ll>
#define vll vector<pll>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi acos(-1)
#define ld long double
#define prime (1000000000 + 7)

using namespace std;

int main(){
	ios::sync_with_stdio(false);
 	cin.tie(0); cout.tie(0);

	ll q;
	cin >> q;

	while(q-->0){
		ll n;
		cin >> n;

		vl vet(n);
		vl res(n);
		for(int i = 0; i < n; ++i){
			cin >> vet[i];
		}

		ll aux = 0;

		for(int i = 0; i < n && aux != n-1; ++i){
			pll menor{0x3f3f3f3f, {}};
			for(int j = i; j < n; ++j){
				menor = min(menor, {vet[j], j});
			}
			for(int j = menor.ss; j > i && aux != n-1; --j){
				aux++;
				swap(vet[j], vet[j-1]);
			}
		}

		if(aux != n-1 && (n-1-aux)%2 == 1){
				swap(vet[n-2], vet[n-1]);
		}

		for(int i = 0; i < n; ++i){
			cout << vet[i] << " \n"[i + 1 == n];
		}

	}

	return 0;
}


#include <bits/stdc++.h>

#ifndef ONLINE_JUDGE
	#define W(x, y) cerr << "\033[31m" << #x << " = " << x << "\033[0m" << y;
#else
	#define W(x, y)
#endif

#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vii vector<pii>
#define vl vector<ll>
#define vll vector<pll>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi acos(-1)
#define ld long double
#define prime (1000000000 + 7)

using namespace std;

int main(){
	ios::sync_with_stdio(false);
 	cin.tie(0); cout.tie(0);

	ll q;
	cin >> q;

	while(q-->0){
		ll n;
		cin >> n;

		vl vet(n);
		vl res(n);
		for(int i = 0; i < n; ++i){
			cin >> vet[i];
		}

		ll aux = 0;

		for(int i = 0; i < n && aux != n-1; ++i){
			pll menor{0x3f3f3f3f, {}};
			for(int j = i; j < n; ++j){
				menor = min(menor, {vet[j], j});
			}
			for(int j = menor.ss; j > i && aux != n-1; --j){
				aux++;
				swap(vet[j], vet[j-1]);
			}
		}
		if(aux != n-1 && n %2 == 1){
			swap(vet[n-1], vet[n-2]);
		}

		for(int i = 0; i < n; ++i){
			cout << vet[i] << " \n"[i + 1 == n];
		}

	}

	return 0;
}


#include <bits/stdc++.h>

#ifndef ONLINE_JUDGE
	#define W(x, y) cerr << "\033[31m" << #x << " = " << x << "\033[0m" << y;
#else
	#define W(x, y)
#endif

#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vii vector<pii>
#define vl vector<ll>
#define vll vector<pll>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi acos(-1)
#define ld long double
#define prime (1000000000 + 7)

using namespace std;

int main(){
	ios::sync_with_stdio(false);
 	cin.tie(0); cout.tie(0);

	ll q;
	cin >> q;

	while(q-->0){
		ll n;
		cin >> n;

		vl vet(n);
		vl res(n);
		for(int i = 0; i < n; ++i){
			cin >> vet[i];
		}

		ll aux = 0;

		for(int i = 0; i < n && aux != n-1; ++i){
			pll menor{0x3f3f3f3f, {}};
			for(int j = i; j-i < n-aux; ++j){
				menor = min(menor, {vet[j], j});
			}
			for(int j = menor.ss; j > i && aux != n-1; --j){
				aux++;
				swap(vet[j], vet[j-1]);
			}
		}

		if(aux != n-1 && (n-1-aux)%2 == 1){
				swap(vet[n-2], vet[n-1]);
		}

		for(int i = 0; i < n; ++i){
			cout << vet[i] << " \n"[i + 1 == n];
		}

	}

	return 0;
}


#include <bits/stdc++.h>

#ifndef ONLINE_JUDGE
	#define W(x, y) cerr << "\033[31m" << #x << " = " << x << "\033[0m" << y;
#else
	#define W(x, y)
#endif

#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vii vector<pii>
#define vl vector<ll>
#define vll vector<pll>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi acos(-1)
#define ld long double
#define prime (1000000000 + 7)

using namespace std;

int main(){
	ios::sync_with_stdio(false);
 	cin.tie(0); cout.tie(0);

	ll q;
	cin >> q;

	while(q-->0){
		ll n;
		cin >> n;

		vl vet(n);
		vl res(n);
		for(int i = 0; i < n; ++i){
			cin >> vet[i];
		}

		ll aux = 0;

		for(int i = 0; i < n && aux != n-1; ++i){
			pll menor{0x3f3f3f3f, {}};
			for(int j = i; j < n && j - i < n - aux + 1; ++j){
				menor = min(menor, {vet[j], j});
			}
			for(int j = menor.ss; j > i && aux != n-1; --j){
				aux++;
				swap(vet[j], vet[j-1]);
			}
		}

		for(int i = 0; i < n; ++i){
			cout << vet[i] << " \n"[i + 1 == n];
		}

	}

	return 0;
}


#include <bits/stdc++.h>

#ifndef ONLINE_JUDGE
	#define W(x, y) cerr << "\033[31m" << #x << " = " << x << "\033[0m" << y;
#else
	#define W(x, y)
#endif

#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vii vector<pii>
#define vl vector<ll>
#define vll vector<pll>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi acos(-1)
#define ld long double
#define prime (1000000000 + 7)

using namespace std;

int main(){
	ios::sync_with_stdio(false);
 	cin.tie(0); cout.tie(0);

	ll q;
	cin >> q;

	while(q-->0){
		ll n;
		cin >> n;

		vl vet(n);
		vl res(n);
		for(int i = 0; i < n; ++i){
			cin >> vet[i];
		}

		ll aux = 0;
		// set<pll> sete;

		for(int i = 0; i < n && aux != n-1; ++i){
			pll menor{0x3f3f3f3f, {}};
			for(int j = i; j < n; ++j){
				menor = min(menor, {vet[j], j});
			}
			for(int j = menor.ss; j > i && aux != n-1 && j - i < n - aux; --j){
				aux++;
				swap(vet[j], vet[j-1]);
			}
			i = max(menor.ss - 1, (long long)i);
		}

		if(aux != n-1 && (n-1-aux)%2 == 1){
				swap(vet[n-2], vet[n-1]);
		}

		for(int i = 0; i < n; ++i){
			cout << vet[i] << " \n"[i + 1 == n];
		}

	}

	return 0;
}


#include <bits/stdc++.h>

#ifndef ONLINE_JUDGE
	#define W(x, y) cerr << "\033[31m" << #x << " = " << x << "\033[0m" << y;
#else
	#define W(x, y)
#endif

#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vii vector<pii>
#define vl vector<ll>
#define vll vector<pll>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi acos(-1)
#define ld long double
#define prime (1000000000 + 7)

using namespace std;

int main(){
	ios::sync_with_stdio(false);
 	cin.tie(0); cout.tie(0);

	ll q;
	cin >> q;

	while(q-->0){
		ll n;
		cin >> n;

		vl vet(n);
		vl res(n);
		for(int i = 0; i < n; ++i){
			cin >> vet[i];
		}

		ll aux = 0;

		for(int i = 0; i < n && aux != n-1; ++i){
			pll menor{0x3f3f3f3f, {}};
			for(int j = i; j < n; ++j){
				menor = min(menor, {vet[j], j});
			}
			for(int j = menor.ss; j > i && aux != n-1; --j){
				aux++;
				swap(vet[j], vet[j-1]);
			}
			i = max(menor.ss - 1, (long long)i);
		}

		if(aux != n-1 && (n-1-aux)%2 == 1){
				swap(vet[n-2], vet[n-1]);
		}

		for(int i = 0; i < n; ++i){
			cout << vet[i] << " \n"[i + 1 == n];
		}

	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n,i,temp,tempm,j,c1=0;
        cin >> n;
        int arr[n];
        map<int,int> m;
        int arr1[n]={0};
        for(i=0;i<n;i++)
        {
            cin >> arr[i];
            m[arr[i]]=i;
        }
        for(i=1;i<=n;i++)
        {
            for(j=m[i];j>0;j--)
            {
                if(arr[i-1]==i) if(i<n) {arr1[i]++; break;}
                if(arr1[j]!=0) break;
                else
                {
                    arr1[j]++;
                    tempm = m[arr[j]];m[arr[j]] = m[arr[j-1]];m[arr[j-1]] = tempm;
                    temp = arr[j];
                    arr[j] = arr[j-1];
                    arr[j-1] = temp;
                }
            }
        }
        for(i=0;i<n;i++)
        {
            cout << arr[i] << " ";
        }
        cout << endl;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n,i,temp,tempm,j,c1=0;
        cin >> n;
        int arr[n];
        map<int,int> m;
        int arr1[n]={0};
        for(i=0;i<n;i++)
        {
            cin >> arr[i];
            m[arr[i]]=i;
        }
        for(i=1;i<=n;i++)
        {
            for(j=m[i];j>0;j--)
            {
                if(arr[i-1]==i) if(i<n) {arr1[i]++; break;}
                if(arr1[j]!=0) break;
                if(arr[j]>arr[j-1]) break;
                else
                {
                    arr1[j]++;
                    tempm = m[arr[j]];m[arr[j]] = m[arr[j-1]];m[arr[j-1]] = tempm;
                    temp = arr[j];
                    arr[j] = arr[j-1];
                    arr[j-1] = temp;
                }
            }
        }
        for(i=0;i<n;i++)
        {
            cout << arr[i] << " ";
        }
        cout << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        long long a[n+1],b[n+1];
        int i;
        long long vis[n+2],pos[n+2];
        memset(pos,0,sizeof(pos));
        memset(vis,0,sizeof(vis));
        for(i=1;i<=n;i++)
        {
            cin>>a[i];
            pos[a[i]]=i;
        }
        int c=1;
        while(c<=n)
        {
            int h=pos[c];
            while(!vis[h]&&h>c)
            {
                pos[a[h]]=h-1;
                pos[a[h-1]]=h;
                swap(a[h],a[h-1]);
                vis[h]=1;
                h--;
            }
            c++;
        }
        for(i=1;i<=n;i++)
        {
            cout<<a[i]<<" ";
        }
        cout<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        long long a[n+1];
        int i;
        long long vis[n+2],pos[n+2];
        memset(vis,0,sizeof(vis));
        for(i=1;i<=n;i++)
        {
            cin>>a[i];
            pos[a[i]]=i;
        }
        int c=1;
        while(c<=n)
        {
            int h=pos[c];
            while(!vis[h]&&h>c)
            {
                swap(a[h],a[h-1]);
                pos[a[h]]=h-1;
                pos[a[h-1]]=h;
                vis[h]=1;
                h--;
            }
            c++;
        }
        for(i=1;i<=n;i++)
        {
            cout<<a[i]<<" ";
        }
        cout<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        long long a[n+1];
        int i;
        long long vis[n+2],pos[n+2];
        memset(vis,0,sizeof(vis));
        for(i=1;i<=n;i++)
        {
            cin>>a[i];
            pos[a[i]]=i;
        }
        int c=1;
        while(c<=n)
        {
            int h=pos[c];
            while(!vis[h]&&h>c)
            {
                pos[a[h]]=h-1;
                pos[a[h-1]]=h;
                swap(a[h],a[h-1]);
                vis[h]=1;
                h--;
            }
            c++;
        }
        for(i=1;i<=n;i++)
        {
            cout<<a[i]<<" ";
        }
        cout<<endl;
    }
}


#include <iostream>
#include <vector>
#include <cmath>
#include<map>
#include<unordered_map>

using namespace std;

template <typename egesz> class SieveFactor{
    public:
        //INIT
        SieveFactor(egesz n):limit(static_cast<egesz>(n)),sieve(limit,true){
            //cout<<"SieveFactor: Generating sieve of a size of "<<limit<<endl;
            sieve[0]=sieve[1]=false;
            for(egesz i=2; i < n;++i){
                int j=i*i;
                while(j<limit){
                    sieve[j]=false;
                    j+=i;
                }
            }
            //cout<<"SieveFactor: Sieve generated."<<endl;
        }

        map<egesz,egesz> factorize(egesz n){
            //cout<<"SieveFactor: Beginning to factorize "<<n<<endl;
            map<egesz,egesz> res;
            egesz i=2;
            while (n!=1){
                if(sieve[i]){
                    if(n%i==0){
                        res.emplace(i,0);
                    }
                    while(n%i==0){
                        n/=i;
                        res[i]++;
                    }
                }
                i++;
            }
            return res;
            //cout<<"SieveFactor: Factorization complete."<<endl;
        }
    private:
        egesz limit;
        vector<bool> sieve;
};
typedef unsigned long long egesz;

egesz mypow(egesz nmbr, int exponent){
    if(exponent==0){
        return 1;
    }
    if(exponent==1){
        return nmbr;
    }
    return nmbr*mypow(nmbr, exponent-1);
}

int main(){
    int num;
    cin >> num;
    egesz Test[num];
    for(int i=0; i<num; ++i){
        cin >> Test[i];
    }
    for(int i=0; i<num; ++i){
        SieveFactor<egesz> factorizator(Test[i]);
        map<egesz,egesz> fact=factorizator.factorize(Test[i]);
        int distinct_factors=0;
        for(auto itr=fact.begin();itr!=fact.end();itr++){
            distinct_factors++;
            //cout<<itr->first<<"^"<<itr->second<<", ";
        }
        //cout<<endl;
        if(distinct_factors>2){
            cout<<"YES"<<endl;
            auto itr=fact.begin();
            cout<<mypow(itr->first, itr->second)<<" ";
            itr++;
            cout<<mypow(itr->first, itr->second)<<" ";
            itr++;
            egesz last=mypow(itr->first, itr->second);
            itr++;
            while (itr!=fact.end()){
                last*=mypow(itr->first, itr->second);
                itr++;
            }
            cout<<last<<endl;
        }else if(distinct_factors==2){
            auto itr=fact.begin();
            egesz p_1=itr->first;
            egesz a_1=itr->second;
            itr++;
            egesz p_2=itr->first;
            egesz a_2=itr->second;
            if((a_1==1 && a_2<=2) || (a_1<=2 && a_2==1)) {
                cout<<"NO"<<endl;
            }else{
                cout<<"YES"<<endl;
                cout<<p_1<<" "<<p_2<<" "<<mypow(p_1, a_1-1)*mypow(p_2, a_2-1)<<endl;
            }
        }else{
            auto itr=fact.begin();
            egesz p=itr->first;
            egesz a=itr->second;
            if(a<6) {
                cout<<"NO"<<endl;
            }else{
                cout<<"YES"<<endl;
                cout<<p<<" "<<p*p<<" "<<mypow(p, a-3)<<endl;
            }
        }

    }
    return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include<map>
#include<unordered_map>

using namespace std;

template <typename egesz> class SieveFactor{
    public:
        //INIT
        SieveFactor(egesz n):limit(static_cast<egesz>(sqrt(n)+1)),sieve(limit,true){
            //cout<<"SieveFactor: Generating sieve of a size of "<<limit<<endl;
            sieve[0]=sieve[1]=false;
            for(egesz i=2; i < sqrt(n);++i){
                int j=i*i;
                while(j<limit){
                    sieve[j]=false;
                    j+=i;
                }
            }
            //cout<<"SieveFactor: Sieve generated."<<endl;
        }

        map<egesz,egesz> factorize(egesz n){
            //cout<<"SieveFactor: Beginning to factorize "<<n<<endl;
            map<egesz,egesz> res;
            egesz i=2;
            while (n!=1 && i < sqrt(n)){
                if(sieve[i]){
                    if(n%i==0){
                        res.emplace(i,0);
                    }
                    while(n%i==0){
                        n/=i;
                        res[i]++;
                    }
                }
                i++;
            }
            if(n!=1){
                res.emplace(n,1);
            }
            return res;
            //cout<<"SieveFactor: Factorization complete."<<endl;
        }
    private:
        egesz limit;
        vector<bool> sieve;
};
typedef unsigned long long egesz;

egesz mypow(egesz nmbr, int exponent){
    if(exponent==0){
        return 1;
    }
    if(exponent==1){
        return nmbr;
    }
    return nmbr*mypow(nmbr, exponent-1);
}

int main(){
    int num;
    cin >> num;
    egesz Test[num];
    for(int i=0; i<num; ++i){
        cin >> Test[i];
    }
    for(int i=0; i<num; ++i){
        SieveFactor<egesz> factorizator(Test[i]);
        map<egesz,egesz> fact=factorizator.factorize(Test[i]);
        int distinct_factors=0;
        for(auto itr=fact.begin();itr!=fact.end();itr++){
            distinct_factors++;
            //cout<<itr->first<<"^"<<itr->second<<", ";
        }
        //cout<<endl;
        if(distinct_factors>2){
            cout<<"YES"<<endl;
            auto itr=fact.begin();
            cout<<mypow(itr->first, itr->second)<<" ";
            itr++;
            cout<<mypow(itr->first, itr->second)<<" ";
            itr++;
            egesz last=mypow(itr->first, itr->second);
            itr++;
            while (itr!=fact.end()){
                last*=mypow(itr->first, itr->second);
                itr++;
            }
            cout<<last<<endl;
        }else if(distinct_factors==2){
            auto itr=fact.begin();
            egesz p_1=itr->first;
            egesz a_1=itr->second;
            itr++;
            egesz p_2=itr->first;
            egesz a_2=itr->second;
            if((a_1==1 && a_2<=2) || (a_1<=2 && a_2==1)) {
                cout<<"NO"<<endl;
            }else{
                cout<<"YES"<<endl;
                cout<<p_1<<" "<<p_2<<" "<<mypow(p_1, a_1-1)*mypow(p_2, a_2-1)<<endl;
            }
        }else{
            auto itr=fact.begin();
            egesz p=itr->first;
            egesz a=itr->second;
            if(a<6) {
                cout<<"NO"<<endl;
            }else{
                cout<<"YES"<<endl;
                cout<<p<<" "<<p*p<<" "<<mypow(p, a-3)<<endl;
            }
        }

    }
    return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include<map>
#include<unordered_map>

using namespace std;

template <typename egesz> class SieveFactor{
    public:
        //INIT
        SieveFactor(egesz n):limit(static_cast<egesz>(n)),sieve(limit,true){
            //cout<<"SieveFactor: Generating sieve of a size of "<<limit<<endl;
            sieve[0]=sieve[1]=false;
            for(egesz i=2; i < n;++i){
                int j=i*i;
                while(j<limit){
                    sieve[j]=false;
                    j+=i;
                }
            }
            //cout<<"SieveFactor: Sieve generated."<<endl;
        }

        map<egesz,egesz> factorize(egesz n){
            //cout<<"SieveFactor: Beginning to factorize "<<n<<endl;
            map<egesz,egesz> res;
            egesz i=2;
            while (n!=1){
                if(sieve[i]){
                    if(n%i==0){
                        res.emplace(i,0);
                    }
                    while(n%i==0){
                        n/=i;
                        res[i]++;
                    }
                }
                i++;
            }
            return res;
            //cout<<"SieveFactor: Factorization complete."<<endl;
        }
    private:
        egesz limit;
        vector<bool> sieve;
};
typedef unsigned long long egesz;

int main(){
    int num;
    cin >> num;
    egesz Test[num];
    for(int i=0; i<num; ++i){
        cin >> Test[i];
    }
    for(int i=0; i<num; ++i){
        SieveFactor<egesz> factorizator(Test[i]);
        map<egesz,egesz> fact=factorizator.factorize(Test[i]);
        int distinct_factors=0;
        for(auto itr=fact.begin();itr!=fact.end();itr++){
            distinct_factors++;
            //cout<<to_string(itr->first)+"^"+to_string(itr->second)+", ";
        }
        //cout<<endl;
        if(distinct_factors>2){
            cout<<"YES"<<endl;
            auto itr=fact.begin();
            cout<<static_cast<egesz>(pow(itr->first, itr->second))<<" ";
            itr++;
            cout<<static_cast<egesz>(pow(itr->first, itr->second))<<" ";
            itr++;
            egesz last=pow(itr->first, itr->second);
            itr++;
            while (itr!=fact.end()){
                last*=pow(itr->first, itr->second);
                itr++;
            }
            cout<<to_string(last);
        }else if(distinct_factors==2){
            auto itr=fact.begin();
            egesz p_1=itr->first;
            egesz a_1=itr->second;
            itr++;
            egesz p_2=itr->first;
            egesz a_2=itr->second;
            if((a_1==1 && a_2<=2) || (a_1<=2 && a_2==1)) {
                cout<<"NO"<<endl;
            }else{
                cout<<"YES"<<endl;
                cout<<p_1<<" "<<p_2<<" "<<static_cast<egesz>(pow(p_1, a_1-1)*pow(p_2, a_2-1))<<endl;
            }
        }else{
            auto itr=fact.begin();
            egesz p=itr->first;
            egesz a=itr->second;
            if(a<6) {
                cout<<"NO"<<endl;
            }else{
                cout<<"YES"<<endl;
                cout<<p<<" "<<p*p<<" "<<static_cast<egesz>(pow(p, a-3))<<endl;
            }
        }

    }
    return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include<map>
#include<unordered_map>

using namespace std;

template <typename egesz> class SieveFactor{
    public:
        //INIT
        SieveFactor(egesz n):limit(static_cast<egesz>(n)),sieve(limit,true){
            //cout<<"SieveFactor: Generating sieve of a size of "<<limit<<endl;
            sieve[0]=sieve[1]=false;
            for(egesz i=2; i < n;++i){
                int j=i*i;
                while(j<limit){
                    sieve[j]=false;
                    j+=i;
                }
            }
            //cout<<"SieveFactor: Sieve generated."<<endl;
        }

        map<egesz,egesz> factorize(egesz n){
            //cout<<"SieveFactor: Beginning to factorize "<<n<<endl;
            map<egesz,egesz> res;
            egesz i=2;
            while (n!=1){
                if(sieve[i]){
                    if(n%i==0){
                        res.emplace(i,0);
                    }
                    while(n%i==0){
                        n/=i;
                        res[i]++;
                    }
                }
                i++;
            }
            return res;
            //cout<<"SieveFactor: Factorization complete."<<endl;
        }
    private:
        egesz limit;
        vector<bool> sieve;
};
typedef unsigned long long egesz;

int main(){
    int num;
    cin >> num;
    egesz Test[num];
    for(int i=0; i<num; ++i){
        cin >> Test[i];
    }
    for(int i=0; i<num; ++i){
        SieveFactor<egesz> factorizator(Test[i]);
        map<egesz,egesz> fact=factorizator.factorize(Test[i]);
        int distinct_factors=0;
        for(auto itr=fact.begin();itr!=fact.end();itr++){
            distinct_factors++;
            //cout<<to_string(itr->first)+"^"+to_string(itr->second)+", ";
        }
        //cout<<endl;
        if(distinct_factors>2){
            cout<<"YES"<<endl;
            auto itr=fact.begin();
            cout<<static_cast<egesz>(pow(itr->first, itr->second))<<" ";
            itr++;
            cout<<static_cast<egesz>(pow(itr->first, itr->second))<<" ";
            itr++;
            egesz last=pow(itr->first, itr->second);
            while (itr!=fact.end()){
                last*=pow(itr->first, itr->second);
                itr++;
            }
            cout<<to_string(last);
        }else if(distinct_factors==2){
            auto itr=fact.begin();
            egesz p_1=itr->first;
            egesz a_1=itr->second;
            itr++;
            egesz p_2=itr->first;
            egesz a_2=itr->second;
            if((a_1==1 && a_2<=2) || (a_1<=2 && a_2==1)) {
                cout<<"NO"<<endl;
            }else{
                cout<<"YES"<<endl;
                cout<<p_1<<" "<<p_2<<" "<<static_cast<egesz>(pow(p_1, a_1-1)*pow(p_2, a_2-1))<<endl;
            }
        }else{
            auto itr=fact.begin();
            egesz p=itr->first;
            egesz a=itr->second;
            if(a<6) {
                cout<<"NO"<<endl;
            }else{
                cout<<"YES"<<endl;
                cout<<p<<" "<<p*p<<" "<<static_cast<egesz>(pow(p, a-3))<<endl;
            }
        }

    }
    return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include<map>
#include<unordered_map>
#include <algorithm>

using namespace std;

template <typename egesz> class SieveFactor{
    public:
        //INIT
        SieveFactor(egesz n):limit(static_cast<egesz>(sqrt(n)+1)),sieve(limit,true){
            //cout<<"SieveFactor: Generating sieve of a size of "<<limit<<endl;
            sieve[0]=sieve[1]=false;
            for(egesz i=2; i < static_cast<egesz>(sqrt(n)+1);++i){
                int j=i*i;
                while(j<limit){
                    sieve[j]=false;
                    j+=i;
                }
            }
            //cout<<"SieveFactor: Sieve generated."<<endl;
        }

        map<egesz,egesz> factorize(egesz n){
            //cout<<"SieveFactor: Beginning to factorize "<<n<<endl;
            map<egesz,egesz> res;
            egesz i=2;
            while (n!=1 && i < static_cast<egesz>(sqrt(n)+1)){
                if(sieve[i]){
                    if(n%i==0){
                        res.emplace(i,0);
                    }
                    while(n%i==0){
                        n/=i;
                        res[i]++;
                    }
                }
                i++;
            }
            if(n!=1){
                res.emplace(n,1);
            }
            return res;
            //cout<<"SieveFactor: Factorization complete."<<endl;
        }
    private:
        egesz limit;
        vector<bool> sieve;
};
typedef unsigned long long egesz;

egesz mypow(egesz nmbr, int exponent){
    if(exponent==0){
        return 1;
    }
    if(exponent==1){
        return nmbr;
    }
    return nmbr*mypow(nmbr, exponent-1);
}

int main(){
    int num;
    cin >> num;
    egesz Test[num];
    for(int i=0; i<num; ++i){
        cin >> Test[i];
    }
    SieveFactor<egesz> factorizator(*max_element(Test, Test+num));

    for(int i=0; i<num; ++i){
        map<egesz,egesz> fact=factorizator.factorize(Test[i]);
        int distinct_factors=0;
        for(auto itr=fact.begin();itr!=fact.end();itr++){
            distinct_factors++;
            //cout<<itr->first<<"^"<<itr->second<<", ";
        }
        //cout<<endl;
        if(distinct_factors>2){
            cout<<"YES"<<endl;
            auto itr=fact.begin();
            cout<<mypow(itr->first, itr->second)<<" ";
            itr++;
            cout<<mypow(itr->first, itr->second)<<" ";
            itr++;
            egesz last=mypow(itr->first, itr->second);
            itr++;
            while (itr!=fact.end()){
                last*=mypow(itr->first, itr->second);
                itr++;
            }
            cout<<last<<endl;
        }else if(distinct_factors==2){
            auto itr=fact.begin();
            egesz p_1=itr->first;
            egesz a_1=itr->second;
            itr++;
            egesz p_2=itr->first;
            egesz a_2=itr->second;
            if((a_1==1 && a_2<=2) || (a_1<=2 && a_2==1)) {
                cout<<"NO"<<endl;
            }else{
                cout<<"YES"<<endl;
                cout<<p_1<<" "<<p_2<<" "<<mypow(p_1, a_1-1)*mypow(p_2, a_2-1)<<endl;
            }
        }else{
            auto itr=fact.begin();
            egesz p=itr->first;
            egesz a=itr->second;
            if(a<6) {
                cout<<"NO"<<endl;
            }else{
                cout<<"YES"<<endl;
                cout<<p<<" "<<p*p<<" "<<mypow(p, a-3)<<endl;
            }
        }

    }
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int t;
    cin >> t;
    int Test[t];
    for(int i=0; i<t; i++){
        cin >> Test[i];
    }
    for(int i=0; i<t; i++){
        int j = 2;
        while(j<Test[i] && Test[i]%j!=0){
            j++;
        }
        if(j>=Test[i]){
            cout << "NO" << endl;
        }
        else{
            int hanyad = Test[i]/j;
            int k = j+1;
            while(k<hanyad && hanyad%k!=0){
                k++;
            }
            if(k>=hanyad){
                cout << "NO" << endl;
            }
            else{
                int c = hanyad/k;
                if(c!=j && c!=k){
                    cout << "YES" << endl;
                    int a = j;
                    int b = k;
                    cout << a << " " << b << " " << c << endl;
                }
                else{
                    cout << "NO" << endl;
                }
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#include <iostream>
#include <functional>
#include <vector>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <math.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define rep1(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
#define per1(i,a,n) for (int i=a;i>n;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define endl "\n"
#define pi 3.14159265358979323846264338327950288419716939937510
#define IOS cin.sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll mod=1000000007;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
ll qpow(ll a,ll b){ll s=1;while(b>0){if(b%2==1){s=s*a;}a=a*a;b=b>>1;}return s;}
ll qpowmod(ll a, ll b, ll c){ll res, t;res=1;t=a%c;while(b){if(b & 1){res=res*t%c;}t=t*t%c;b>>=1;}return res;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline long long read(){long long k=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){k=k*10+ch-'0';ch=getchar();}return k*f;}
inline void write(long long x){if(x<0)x=-x,putchar('-');if(x>9)write(x/10);putchar(x%10+'0');}
// head
ll t;
int main()
{
    //IOS;
    cin>>t;
    while(t--)
    {
        ll n,a,b;
        cin>>n;
        for(int i=2;i*i<=n;i++)
        {
            if(n%i==0)
            {
                a=i;
            }
        }
        if(n%a!=0)
        {
            cout<<"NO"<<endl;
            continue;
        }
        n/=a;
        for(int i=a+1;i*i<=n;i++)
        {
            if(n%i==0)
            {
                b=i;
            }
        }
        if(n%b!=0)
        {
            cout<<"NO"<<endl;
            continue;
        }
        n/=a;
        if(a!=n&&b!=n&&n!=1)
        {
            cout<<a<<' '<<b<<' '<<n<<endl;
        }
        else
        {
            cout<<"NO"<<endl;
            continue;
        }
    }
    return 0;
}
/*
5
64
32
97
2
12345

*/


#include <bits/stdc++.h>
#include <iostream>
#include <functional>
#include <vector>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <math.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define rep1(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
#define per1(i,a,n) for (int i=a;i>n;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define endl "\n"
#define pi 3.14159265358979323846264338327950288419716939937510
#define IOS cin.sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll mod=1000000007;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
ll qpow(ll a,ll b){ll s=1;while(b>0){if(b%2==1){s=s*a;}a=a*a;b=b>>1;}return s;}
ll qpowmod(ll a, ll b, ll c){ll res, t;res=1;t=a%c;while(b){if(b & 1){res=res*t%c;}t=t*t%c;b>>=1;}return res;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline long long read(){long long k=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){k=k*10+ch-'0';ch=getchar();}return k*f;}
inline void write(long long x){if(x<0)x=-x,putchar('-');if(x>9)write(x/10);putchar(x%10+'0');}
// head
ll t,n,a,b;
int isPrime(int n)
{
    //返回1表示判断为质数，0为非质数，在此没有进行输入异常检测
    float n_sqrt;
    if(n==2 || n==3)
        return 1;
    if(n%6!=1 && n%6!=5)
        return 0;
    n_sqrt=floor(sqrt((float)n));
    for(int i=5; i<=n_sqrt; i+=6)
    {
        if(n%(i)==0 | n%(i+2)==0)
            return 0;
    }
    return 1;
}
int main()
{
    //IOS;
    cin>>t;
    while(t--)
    {
        cin>>n;
        if(isPrime(n))
        {
            cout<<"NO"<<endl;
        }
        else
        {
            ll cnt=2;
            while(1)
            {
                if(n%cnt==0)
                {
                    n/=cnt;
                    a=cnt;
                    cnt++;
                    break;
                }
                cnt++;
            }
            if(isPrime(n))
            {
                cout<<"NO"<<endl;
                continue;
            }
            cnt=2;
            while(1)
            {
                if(n%cnt==0&&cnt!=a)
                {
                    n/=cnt;
                    b=cnt;
                    break;
                }
                cnt++;
            }
            if(a!=b&&a!=n&&b!=n)
            {
                cout<<"YES"<<endl;
                cout<<a<<' '<<b<<' '<<n<<endl;
            }
            else
                cout<<"NO"<<endl;
        }
    }
    return 0;
}
/*
5
64
32
97
2
12345

*/


#include <bits/stdc++.h>
#include <iostream>
#include <functional>
#include <vector>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <math.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define rep1(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
#define per1(i,a,n) for (int i=a;i>n;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define endl "\n"
#define pi 3.14159265358979323846264338327950288419716939937510
#define IOS cin.sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll mod=1000000007;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
ll qpow(ll a,ll b){ll s=1;while(b>0){if(b%2==1){s=s*a;}a=a*a;b=b>>1;}return s;}
ll qpowmod(ll a, ll b, ll c){ll res, t;res=1;t=a%c;while(b){if(b & 1){res=res*t%c;}t=t*t%c;b>>=1;}return res;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline long long read(){long long k=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){k=k*10+ch-'0';ch=getchar();}return k*f;}
inline void write(long long x){if(x<0)x=-x,putchar('-');if(x>9)write(x/10);putchar(x%10+'0');}
// head
ll t,n,a,b;
int isPrime(int n)
{
    //返回1表示判断为质数，0为非质数，在此没有进行输入异常检测
    float n_sqrt;
    if(n==2 || n==3)
        return 1;
    if(n%6!=1 && n%6!=5)
        return 0;
    n_sqrt=floor(sqrt((float)n));
    for(int i=5; i<=n_sqrt; i+=6)
    {
        if(n%(i)==0 | n%(i+2)==0)
            return 0;
    }
    return 1;
}
int main()
{
    //IOS;
    cin>>t;
    while(t--)
    {
        cin>>n;
        if(isPrime(n))
        {
            cout<<"NO"<<endl;
        }
        else
        {
            ll cnt=2;
            while(1)
            {
                if(n%cnt==0)
                {
                    n/=cnt;
                    a=cnt;
                    cnt++;
                    break;
                }
                cnt++;
            }
            if(isPrime(n))
            {
                cout<<"NO"<<endl;
                continue;
            }
            cnt=2;
            while(1)
            {
                if(n%cnt==0)
                {
                    n/=cnt;
                    b=cnt;
                    break;
                }
                cnt++;
            }
            if(n>b)
            {
                cout<<"YES"<<endl;
                cout<<a<<' '<<b<<' '<<n<<endl;
            }
            else
                cout<<"NO"<<endl;
        }
    }
    return 0;
}
/*
5
64
32
97
2
12345

*/


#include <bits/stdc++.h>
#include <iostream>
#include <functional>
#include <vector>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <math.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define rep1(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
#define per1(i,a,n) for (int i=a;i>n;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define endl "\n"
#define pi 3.14159265358979323846264338327950288419716939937510
#define IOS cin.sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll mod=1000000007;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
ll qpow(ll a,ll b){ll s=1;while(b>0){if(b%2==1){s=s*a;}a=a*a;b=b>>1;}return s;}
ll qpowmod(ll a, ll b, ll c){ll res, t;res=1;t=a%c;while(b){if(b & 1){res=res*t%c;}t=t*t%c;b>>=1;}return res;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline long long read(){long long k=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){k=k*10+ch-'0';ch=getchar();}return k*f;}
inline void write(long long x){if(x<0)x=-x,putchar('-');if(x>9)write(x/10);putchar(x%10+'0');}
// head
ll t,n,a,b;
int isPrime(int n)
{
    //返回1表示判断为质数，0为非质数，在此没有进行输入异常检测
    float n_sqrt;
    if(n==2 || n==3)
        return 1;
    if(n%6!=1 && n%6!=5)
        return 0;
    n_sqrt=floor(sqrt((float)n));
    for(int i=5; i<=n_sqrt; i+=6)
    {
        if(n%(i)==0 | n%(i+2)==0)
            return 0;
    }
    return 1;
}
int main()
{
    //IOS;
    cin>>t;
    while(t--)
    {
        cin>>n;
        if(isPrime(n))
        {
            cout<<"NO"<<endl;
        }
        else
        {
            ll cnt=2;
            while(1)
            {
                if(n%cnt==0)
                {
                    n/=cnt;
                    a=cnt;
                    cnt++;
                    break;
                }
                cnt++;
            }
            if(isPrime(n))
            {
                cout<<"NO"<<endl;
                continue;
            }
            cnt=2;
            while(1)
            {
                if(n%cnt==0&&cnt!=a)
                {
                    n/=cnt;
                    b=cnt;
                    break;
                }
                cnt++;
            }
            if(a!=b&&a!=n&&b!=n&&n!=1)
            {
                cout<<"YES"<<endl;
                cout<<a<<' '<<b<<' '<<n<<endl;
            }
            else
                cout<<"NO"<<endl;
        }
    }
    return 0;
}
/*
5
64
32
97
2
12345

*/


#include <bits/stdc++.h>
#include <iostream>
#include <functional>
#include <vector>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <math.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define rep1(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
#define per1(i,a,n) for (int i=a;i>n;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define endl "\n"
#define pi 3.14159265358979323846264338327950288419716939937510
#define IOS cin.sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll mod=1000000007;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
ll qpow(ll a,ll b){ll s=1;while(b>0){if(b%2==1){s=s*a;}a=a*a;b=b>>1;}return s;}
ll qpowmod(ll a, ll b, ll c){ll res, t;res=1;t=a%c;while(b){if(b & 1){res=res*t%c;}t=t*t%c;b>>=1;}return res;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline long long read(){long long k=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){k=k*10+ch-'0';ch=getchar();}return k*f;}
inline void write(long long x){if(x<0)x=-x,putchar('-');if(x>9)write(x/10);putchar(x%10+'0');}
// head
ll q,x,a[400005],k;
int main()
{
    //IOS;
    cin>>q>>x;
    while(q--)
    {
        cin>>t;
        a[t%x]++;
        while(1)
        {
            if(a[k%x]==0)
            {
                break;
            }
            a[k%x]--;
            k++;
        }
        cout<<k<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#include <iostream>
#include <functional>
#include <vector>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <math.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define rep1(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
#define per1(i,a,n) for (int i=a;i>n;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define endl "\n"
#define pi 3.14159265358979323846264338327950288419716939937510
#define IOS cin.sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll mod=1000000007;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
ll qpow(ll a,ll b){ll s=1;while(b>0){if(b%2==1){s=s*a;}a=a*a;b=b>>1;}return s;}
ll qpowmod(ll a, ll b, ll c){ll res, t;res=1;t=a%c;while(b){if(b & 1){res=res*t%c;}t=t*t%c;b>>=1;}return res;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline long long read(){long long k=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){k=k*10+ch-'0';ch=getchar();}return k*f;}
inline void write(long long x){if(x<0)x=-x,putchar('-');if(x>9)write(x/10);putchar(x%10+'0');}
// head
ll t;
ll n,a,b;
int main()
{
    //IOS;
    cin>>t;
    while(t--)
    {
        cin>>n;
        a=0;
        b=0;
        for(int i=2;i*i<=n;i++)
        {
            if(n%i==0)
            {
                a=i;
                break;
            }
        }
        if(a==0)
        {
            cout<<"NO"<<endl;
            continue;
        }
        n/=a;
        for(int i=a+1;i*i<=n;i++)
        {
            if(n%i==0)
            {
                b=i;
                break;
            }
        }
        if(b==0)
        {
            cout<<"NO"<<endl;
            continue;
        }
        n/=b;
        if(a!=n&&b!=n&&n!=1)
        {
            cout<<"YES"<<endl;
            cout<<a<<' '<<b<<' '<<n<<endl;
        }
        else
        {
            cout<<"NO"<<endl;
            continue;
        }
    }
    return 0;
}
/*
5
64
32
97
2
12345

*/


#include <bits/stdc++.h>
#include <iostream>
#include <functional>
#include <vector>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <math.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define rep1(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
#define per1(i,a,n) for (int i=a;i>n;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define endl "\n"
#define pi 3.14159265358979323846264338327950288419716939937510
#define IOS cin.sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll mod=1000000007;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
ll qpow(ll a,ll b){ll s=1;while(b>0){if(b%2==1){s=s*a;}a=a*a;b=b>>1;}return s;}
ll qpowmod(ll a, ll b, ll c){ll res, t;res=1;t=a%c;while(b){if(b & 1){res=res*t%c;}t=t*t%c;b>>=1;}return res;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline long long read(){long long k=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){k=k*10+ch-'0';ch=getchar();}return k*f;}
inline void write(long long x){if(x<0)x=-x,putchar('-');if(x>9)write(x/10);putchar(x%10+'0');}
// head
ll t,n,a,b,c,flag,cnt;
int isPrime(int n)
{
    //返回1表示判断为质数，0为非质数，在此没有进行输入异常检测
    float n_sqrt;
    if(n==2 || n==3)
        return 1;
    if(n%6!=1 && n%6!=5)
        return 0;
    n_sqrt=floor(sqrt((float)n));
    for(int i=5; i<=n_sqrt; i+=6)
    {
        if(n%(i)==0 | n%(i+2)==0)
            return 0;
    }
    return 1;
}
int main()
{
    //IOS;
    cin>>t;
    while(t--)
    {
        cin>>n;
        flag=0;
        cnt=2;
        if(isPrime(n)||n<24)
        {
            cout<<"NO"<<endl;
        }
        else
        {
            while(1)
            {
                if(cnt>=n)
                {
                    break;
                }
                if(n%cnt==0)
                {
                    if(flag==0&&n%cnt==0)
                    {
                        a=cnt;
                        n/=cnt;
                        flag=1;
                        cnt++;
                        if(isPrime(n))
                        {
                            cout<<"NO"<<endl;
                            goto s1;
                        }
                    }
                    if(flag==1&&n%cnt==0)
                    {
                        b=cnt;
                        n/=cnt;
                        break;
                    }
                }
                else
                {
                    cnt++;
                }
            }
            if(n>b&&b!=0)
            {
                cout<<"YES"<<endl;
                cout<<min(a,b)<<' '<<max(a,b)<<' '<<n<<endl;
            }
            else
                cout<<"NO"<<endl;
            s1:;
        }
    }
    return 0;
}
/*
5
64
32
97
2
12345

*/