#include <bits/stdc++.h>
using namespace std;
int main(void) {
    int n,a;
    std::deque<int>deq ;
    cin >> n;
    for (int i=0;i<n;i++) {
        cin >> a;
        deq.emplace_back(a);
    }
    if (prev_permutation(deq.begin(),deq.end())!=false) {
        for (int j=0;j<deq.size();j++) {
            if (j==deq.size()-1) {
                cout << deq[j] << endl;
            } else {
                cout << deq[j] << " ";
            }
        }
        if (next_permutation(deq.begin(),deq.end())!=false) {
            for (int k=0;k<deq.size();k++) {
                if (k==deq.size()-1) {
                    cout << deq[k] << endl;
                } else {
                    cout << deq[k] << " ";
                }
            }
        }
        if (next_permutation(deq.begin(),deq.end())!=false) {
             for (int l=0;l<deq.size();l++) {
                if (l==deq.size()-1) {
                    cout << deq[l] << endl;
                } else {
                    cout << deq[l] << " ";
                }
             }
        }
    } else {
        next_permutation(deq.begin(),deq.end());
        for (int m=0;m<deq.size();m++) {
            if (m==deq.size()-1) {
                cout << deq[m] << endl;
            } else {
                cout << deq[m] << " ";
            }
        }
        if (next_permutation(deq.begin(),deq.end())!=false) {
             for (int n=0;n<deq.size();n++) {
                if (n==deq.size()-1) {
                    cout << deq[n] << endl;
                } else {
                    cout << deq[n] << " ";
                }
             }
        }
    }
}

