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

using namespace std;
vector<int>v;
void print()
{
    int ans=0;
    for(auto it=v.begin();it!=v.end();it++)
    {
        if(ans!=0)cout<<" ";
        cout<<*it;
        ans++;
    }
    cout<<endl;
}
int main()
{
    int n;
    cin>>n;
    v.resize(n);
    int i,j,k;
    for(i=0;i<n;i++)
    {
        cin>>v[i];
    }

    if(prev_permutation(v.begin(),v.end())==0)
    {
        //cout<<endl;
        next_permutation(v.begin(),v.end());
        print();
    if(next_permutation(v.begin(),v.end())==0)
    {
        //cout<<endl;
    }
    else {
    print();
    }
    }

    else
    {
        print();
        next_permutation(v.begin(),v.end());
        print();
        if(next_permutation(v.begin(),v.end())==0)
        {

        }
        else
        {
            print();
        }
    }

    return 0;
}

