#include <bits/stdc++.h>

using namespace std;

#define reps(i, n, m) for (int i = (int) (n); i < (int) (m); i++)
#define rep(i, n) reps(i, 0, (n))

int main() {
    int n; cin >> n;
    vector<int> v(n);
    rep(i, n) cin >> v[i];
    auto b = v;
    auto a = v;
    bool aa = prev_permutation(a.begin(), a.end());
    auto c = b;
    bool cc = next_permutation(c.begin(), c.end());
    if (aa) rep(i, n) cout << a[i] << " \n"[i==n-1];
    rep(i, n) cout << b[i] << " \n"[i==n-1];
    if (cc) rep(i, n) cout << c[i] << " \n"[i==n-1];
}
