#include <algorithm>
#include <stdio.h>
using namespace std;
int main(){
    bool big=true,less=true;
    int n,a[20],tot=1;
    scanf("%d%d",&n,&a[0]);
    for(int i=1;i<n;i++){
        scanf("%d",&a[i]);
        big&=a[i]<a[i-1];
        less&=a[i]>a[i-1];
        tot*=i;
    }
    tot*=n;
    for(int i=1;i<tot;i++)next_permutation(a,a+n);
    if(!less){
        printf("%d",a[0]);
        for(int i=1;i<n;i++)printf(" %d",a[i]);
        printf("\n");
    }
    next_permutation(a,a+n);
    printf("%d",a[0]);
    for(int i=1;i<n;i++)printf(" %d",a[i]);
    printf("\n");
    if(!big){
        next_permutation(a,a+n);
        printf("%d",a[0]);
        for(int i=1;i<n;i++)printf(" %d",a[i]);
        printf("\n");
    }
}
