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

template<class T>
ostream& operator<<(ostream& os,vector<T> const& v){for(auto itr=v.begin();itr!=v.end();++itr){if(itr!=v.begin())os<<' ';os<<*itr;}return os;}

signed main(){

  int N;
  cin>>N;
  vector<int> a(N);
  for(auto& ai:a)cin>>ai;
  
  auto p = a;
  auto n = a;

  if(prev_permutation(p.begin(),p.end()))cout<<p<<endl;
  cout<<a<<endl;
  if(next_permutation(n.begin(),n.end()))cout<<n<<endl;

}
