#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n, x[1001];
int final = 0;
void init(){
for(int i = 1; i <= n; i++){
x[i] = i;
}
}
void gen(){
int i = n - 1;
while(i >= 1 && x[i] >= x[i + 1]){
i--;
}
if(i == 0){
init();
}
else{
for(int j = n; j > i; j--){
if(x[j] >= x[i]){
swap(x[i], x[j]);
break;
}
}
sort(x + i + 1, x + n + 1);
}
}
int main(){
int t; cin >> t;
while(t--){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> x[i];
}
gen();
for(int i = 1; i <= n; i++){
cout << x[i] << ' ';
}
cout << endl;
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgbGwgbG9uZyBsb25nCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG4sIHhbMTAwMV07CmludCBmaW5hbCA9IDA7Cgp2b2lkIGluaXQoKXsKICAgZm9yKGludCBpID0gMTsgaSA8PSBuOyBpKyspewogICAgICB4W2ldID0gaTsKICAgfQp9Cgp2b2lkIGdlbigpewogICBpbnQgaSA9IG4gLSAxOwogICB3aGlsZShpID49IDEgJiYgeFtpXSA+PSB4W2kgKyAxXSl7CiAgICAgIGktLTsKICAgfQogICBpZihpID09IDApewogICAgICBpbml0KCk7CiAgIH0KICAgZWxzZXsKICAgICAgZm9yKGludCBqID0gbjsgaiA+IGk7IGotLSl7CiAgICAgICAgIGlmKHhbal0gPj0geFtpXSl7CiAgICAgICAgICAgIHN3YXAoeFtpXSwgeFtqXSk7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgICB9CiAgICAgIH0KICAgICAgc29ydCh4ICsgaSArIDEsIHggKyBuICsgMSk7CiAgIH0KfQoKCgppbnQgbWFpbigpewogICBpbnQgdDsgY2luID4+IHQ7CiAgIHdoaWxlKHQtLSl7CiAgICAgIGNpbiA+PiBuOwogICAgICBmb3IoaW50IGkgPSAxOyBpIDw9IG47IGkrKyl7CiAgICAgICAgIGNpbiA+PiB4W2ldOwogICAgICB9CiAgICAgIGdlbigpOwogICAgICBmb3IoaW50IGkgPSAxOyBpIDw9IG47IGkrKyl7CiAgICAgICAgIGNvdXQgPDwgeFtpXSA8PCAnICc7CiAgICAgIH0KICAgICAgY291dCA8PCBlbmRsOwogICB9Cn0=