#include<bits/stdc++.h>
using namespace std;
using Int = long long;
//INSERT ABOVE HERE
signed main(){
  Int n;
  cin>>n;
  vector<Int> a(n);
  for(Int i=0;i<n;i++) cin>>a[i];
  if(!is_sorted(a.begin(),a.end())){
    prev_permutation(a.begin(),a.end());
    for(Int i=0;i<n;i++){
      if(i) cout<<" ";
      cout<<a[i];
    }
    cout<<endl;
    next_permutation(a.begin(),a.end());
  }
  
  for(Int i=0;i<n;i++){
    if(i) cout<<" ";
    cout<<a[i];
  }
  cout<<endl;
   
  if(next_permutation(a.begin(),a.end())){
    for(Int i=0;i<n;i++){
      if(i) cout<<" ";
      cout<<a[i];
    }
    cout<<endl;
    prev_permutation(a.begin(),a.end());
  }
  return 0;
}

