#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main() {
    int n;
    cin >> n;

    vector<int> a(n);
    for (int i = 0; i < n; i++){
      cin >> a[i];
    }
    auto itr = a.begin();
    if (prev_permutation(a.begin(), a.end())){
      for (itr = a.begin(); itr!=a.end(); ++itr) {
        if (itr!=--a.end()){
          printf("%d ",*itr);
        }else{
          printf("%d\n",*itr);
        }
      }
    }else{
      printf("");
    }

    next_permutation(a.begin(), a.end());
    for (itr = a.begin(); itr!=a.end(); ++itr) {
      if (itr!=--a.end()){
        printf("%d ",*itr);
      }else{
        printf("%d\n",*itr);
      }
    }
    if (next_permutation(a.begin(), a.end())){
      for (itr = a.begin(); itr!=a.end(); ++itr) {
        if (itr!=--a.end()){
          printf("%d ",*itr);
        }else{
          printf("%d\n",*itr);
        }
      }
    }else{
      printf("");
    }
    return 0;
}

