#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;


int main(){
	int n;
	cin >> n;
	vector< int > a(n), a_dum(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		a_dum[i] = a[i];
	}
	if (prev_permutation(a_dum.begin(), a_dum.end())) {
		for (int i = 0; i < n; i++) cout << (i == 0 ? "" : " ") << a_dum[i] << flush;
		cout << endl;
	}
	for (int i = 0; i < n; i++) cout << (i == 0 ? "" : " ") << a[i] << flush;
	cout << endl;
	if (next_permutation(a.begin(), a.end())) {
		for (int i = 0; i < n; i++) cout << (i == 0 ? "" : " ") << a[i] << flush;
		cout << endl;
	}
	return 0;
}
