#include<iostream>
#include<algorithm>
using namespace std;
int main() {
    int n, k = 0;
    cin >> n;
    int v[100000];
    for (int i = 0; i < n; i++)
        cin >> v[i];
    if (!prev_permutation(v, v + n)) {
        ++k;
        next_permutation(v, v + n);
    }
    for (int j = k; j < 3; j++) {
        for (int i = 0; i < n; i++) {
            cout << v[i];
            if (i != n - 1)cout << " ";
        }
        cout << endl;
        if (!next_permutation(v, v + n))
            break;
    }
}
