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

using namespace std;

int main() {
    int n;
    cin >> n;
    vector<int> v(n);
    for (int &i : v) cin >> i;
    vector<int> t = v;
    if (prev_permutation(t.begin(), t.end())) {
        for (int i = 0; i < n; i++) {
            cout << t[i];
            if (i != n - 1) cout << " ";
            else cout << endl;
        }
    }
    for (int i = 0; i < n; i++) {
        cout << v[i];
        if (i != n - 1) cout << " ";
        else cout << endl;
    }
    if (next_permutation(v.begin(), v.end())) {
        for (int i = 0; i < n; i++) {
            cout << v[i];
            if (i != n - 1) cout << " ";
            else cout << endl;
        }
    }
    return 0;
}
