#include <stdio.h>
#include <algorithm>
using namespace std;
int main(){
int n;
while(scanf("%d",&n)&&n){
int a[1000];
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
sort(a,a+n);
do{
for(int i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}while(next_permutation(a,a+n));
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main(){
for(string s;cin>>s;cout<<endl){
sort(s.begin(),s.end());
for(cout<<s<<endl;next_permutation(s.begin(),s.end());cout<<s<<endl);
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main(){
string a;cin>>a;
do{
cout<<a<<endl;
}while(next_permutation(a.begin(),a.end()));
while(prev_permutation(a.begin(),a.end()));
return 0;
}