#include <bits/stdc++.h>
using namespace std;
int n,a[15],c[15];
bool check(int b[])
{
    stack<int> st;
    for(int i=0,j=0;i<n;i++)
    {
       st.push(a[i]);
        while(!st.empty()&&b[j]==st.top())
        {
            st.pop();
            j++;
        }
    }
    return st.empty();
}
int main()
{
    while(cin >> n)
    {
        for(int i=0;i<n;i++)
        {
            cin >> a[i];
            c[i]=a[i];
        }
        sort(c,c+n);
        do{
            if(check(c))
            {
                for(int i=0;i<n;i++)
                    cout << c[i] << " ";
                cout << endl;
            }
        }while(next_permutation(c,c+n));
    }
}