#include
using namespace std;
#include
int main() {
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int n;
cin>>n;
int*arr=new int(n);
for(int j=0;j<n;j++)
cin>>arr[j];
next_permutation(arr,arr+n);
for(int j=0;j<n;j++)
cout<<arr[j]<<" ";
cout<<endl;
delete []arr;
}
return 0;
}