#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)

int main()
{
    ll n,output=3;
    cin >> n;
    vector<ll> a(n);
    rep(i,n) cin >> a[i];
    if(!prev_permutation(a.begin(), a.end())){
        next_permutation(a.begin(), a.end());
        output--;
    };


    rep(i,output){
        rep(i,n)
            cout << (i ? " " : "") << a[i];
        cout << endl;

        if(!next_permutation(a.begin() , a.end())) break;
    };

    return 0;
}
