#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for(int i = 0; i < n; ++i) cin >> a[i];
  auto b = a;
  if(prev_permutation(b.begin(), b.end())) {
    for(int i = 0; i < n; ++i) cout << b[i] << " \n"[i==n-1];
  }
  for(int i = 0; i < n; ++i) cout << a[i] << " \n"[i==n-1];
  auto c = a;
  if(next_permutation(c.begin(), c.end())) {
    for(int i = 0; i < n; ++i) cout << c[i] << " \n"[i==n-1];
  }
  return 0;
}

