#include <bits/stdc++.h>

using namespace std;


int main(){
    int len_array;
    scanf("%d",&len_array);
    vector<int> int_array1(len_array);
    vector<int> int_array2(len_array);
    int temp;
    for(int i = 0;i < len_array;i++){
        scanf("%d",&temp),
        int_array1[i] = temp;
    }
    int_array2 = int_array1;
    if(prev_permutation(int_array1.begin(),int_array1.end())){
        for(int i = 0;i < int_array1.size();i++){
            if(i != int_array1.size()-1) printf("%d ",int_array1[i]);
            else printf("%d\n",int_array1[i]);
        }
    }
    for(int i = 0;i < int_array2.size();i++){
        if(i != int_array2.size()-1) printf("%d ",int_array2[i]);
        else printf("%d\n",int_array2[i]);
    }
    if(next_permutation(int_array2.begin(),int_array2.end())){
        for(int i = 0;i < int_array2.size();i++){
            if(i != int_array2.size()-1) printf("%d ",int_array2[i]);
            else printf("%d\n",int_array2[i]);
        }
    }


    return 0;   
}
