#include <bits/stdc++.h>

using namespace std;

//#define _FILES
#define PB push_back
#define MP make_pair

typedef long long ll;
typedef pair<int,int> pii;

const int maxn = 200005;
struct node
{
    int f, s, len, fp, sp;
};

node t[4*maxn];
int p[maxn];
priority_queue< pair<int, pair<int,int> > > q;
vector< pair<int,int> > ans;
int n;

node combine(const node &a, const node &b)
{
    node res;
    res.len = a.len + b.len;
    if (a.len == 1)
    {
        if (b.len == 1)
        {
            res.f = a.f;
            res.s = b.f;
            res.fp = a.fp;
            res.sp = b.fp;
        }
        else
        {
            if (a.f <= b.s)
            {
                res.fp = a.fp;
            }
            else
            {
                res.fp = b.sp;
            }

            res.f = min(a.f, b.s);
            res.s = b.f;
            res.sp = b.fp;
        }
    }
    else
    {
        if (b.len == 1)
        {
            if (a.len & 1)
            {
                res.f = a.f;
                res.fp = a.fp;

                if (a.s <= b.f)
                {
                    res.sp = a.sp;
                }
                else
                {
                    res.sp = b.fp;
                }
                res.s = min(a.s, b.f);
            }
            else
            {
                if (a.f <= b.f)
                {
                    res.fp = a.fp;
                }
                else
                {
                    res.fp = b.fp;
                }
                res.f = min(a.f, b.f);
                res.s = a.s;
                res.sp = a.sp;
            }
        }
        else
        {
            if (a.len & 1)
            {
                if (a.f<=b.s)
                {
                    res.fp = a.fp;
                }
                else
                {
                    res.fp = b.sp;
                }

                res.f = min(a.f, b.s);

                if (a.s<=b.f)
                {
                    res.sp = a.sp;
                }
                else
                {
                    res.sp = b.fp;
                }
                res.s = min(a.s, b.f);
            }
            else
            {
                if (a.f<=b.f)
                {
                    res.fp = a.fp;
                }
                else
                {
                    res.fp = b.fp;
                }
                res.f = min(a.f, b.f);

                if (a.s<=b.s)
                {
                    res.sp = a.sp;
                }
                else
                {
                    res.sp = b.sp;
                }
                res.s = min(a.s,b.s);
            }
        }
    }
    return res;
}

void build(int v,int tl,int tr)
{
    if (tl == tr)
    {
        t[v].f = t[v].s = p[tl];
        t[v].len = 1;
        t[v].fp = t[v].sp = tl;
    }
    else
    {
        int tm = (tl+tr)/2;
        build(v*2,tl,tm);
        build(v*2+1,tm+1,tr);
        t[v] = combine(t[v*2], t[v*2+1]);
    }
}

node query(int v,int tl,int tr,int l,int r)
{

    if (tl == l && tr == r)
    {
        return t[v];
    }
    else
    {
        int tm = (tl+tr)/2;
        if (tm >= r)
        {
            return query(v*2,tl,tm,l,r);
        }

        if (tm < l)
        {
            return query(v*2+1,tm+1,tr,l,r);
        }
        return  combine(query(v*2,tl,tm,l,tm), query(v*2+1,tm+1,tr,tm+1,r));
    }
}

void calc(int l,int r)
{
    if (l >= r) return;
    node q1 = query(1, 1, n, l, r);
    node q2 = query(1,1,n, q1.fp + 1, r);
    ans.push_back(make_pair(p[q1.fp], p[q2.fp]));

    if (l < q1.fp - 1)
    {
        q.push(make_pair(-query(1,1,n,l,q1.fp-1).f,make_pair(l, q1.fp-1)));
    }

    if (q1.fp+1 < q2.fp-1)
    {
        q.push(make_pair(-query(1,1,n,q1.fp+1, q2.fp-1).f,make_pair(q1.fp+1, q2.fp-1)));
    }

    if (q2.fp+1 < r)
    {
        q.push(make_pair(-query(1,1,n,q2.fp+1, r).f,make_pair(q2.fp+1,r)));
    }
}

int main()
{
    ios_base::sync_with_stdio(false);

    #ifdef _FILES
        freopen("","r",stdin);
        freopen("","w",stdout);
    #endif // _FILES
    cin >> n;
    for (int i=1;i<=n;i++)
    {
        cin >> p[i];
    }

    build(1,1,n);
    calc(1,n);
    while (!q.empty())
    {
        pair<int,pair<int,int>> cur = q.top();
        q.pop();
        calc(cur.second.first, cur.second.second);
    }

    for (int i=0;i<ans.size();i++)
    {
        if (i) cout << " ";
        cout << ans[i].first << " " << ans[i].second;
    }
    cout << endl;
    return 0;
}


Measured memory_bytes: 15244