#include <bits/stdc++.h>

using namespace std;

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

    vector<int> a(n), b(n), c(n);
    for (int i = 0; i < n; ++i) {
        int t;
        cin >> t;
        a[i] = t;
        b[i] = t;
        c[i] = t;
    }

    //前の順列
    if (prev_permutation(a.begin(), a.end())) {
        for (int i = 0; i < n; ++i) {
            cout << (i ? " " : "") << a[i];
        }
        cout << endl;
    }

    //与えられた順列
    for (int i = 0; i < n; ++i) {
        cout << (i ? " " : "") << b[i];
    }
    cout << endl;

    //次の順列
    if (next_permutation(c.begin(), c.end())) {
        for (int i = 0; i < n; ++i) {
            cout << (i ? " " : "") << c[i];
        }
        cout << endl;
    }
    return 0;
}
