// Copyright (C) 2017 __debug.

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; version 3

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; If not, see <http://www.gnu.org/licenses/>.


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>

#define x first
#define y second
#define MP std::make_pair
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif

using std::pair;
using std::vector;
using std::string;

typedef long long LL;
typedef pair<int, int> Pii;

const int oo = 0x3f3f3f3f;

template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, true : false; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, true : false; }
string procStatus()
{
    std::ifstream t("/proc/self/status");
    return string(std::istreambuf_iterator<char>(t), std::istreambuf_iterator<char>());
}
template<typename T> T read(T &x)
{
    int f = 1;
    char ch = getchar();
    for (; !isdigit(ch); ch = getchar())
        f = (ch == '-' ? -1 : 1);
    for (x = 0; isdigit(ch); ch = getchar())
        x = 10 * x + ch - '0';
    return x *= f;
}
template<typename T> void write(T x)
{
    if (x == 0) {
        putchar('0');
        return;
    }
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    static char s[20];
    int top = 0;
    for (; x; x /= 10)
        s[++top] = x % 10 + '0';
    while (top)
        putchar(s[top--]);
}
// EOT

const int MAXN = 2e5 + 5;
const int LOGN = 19;

int N;
int P[MAXN];

void input()
{
    read(N);
    for (int i = 1; i <= N; ++i) {
        read(P[i]);
    }
}

struct RMQ {
    int a[MAXN];
    int minp[LOGN][MAXN];

    inline int lessp(int i, int j)
    {
        return a[i] < a[j] ? i : j;
    }

    void init()
    {
        for (int i = 1; i <= N; ++i) {
            minp[0][i] = i;
        }
        for (int k = 1; (1 << k) <= N; ++k) {
            for (int i = 1; i + (1 << k) - 1 <= N; ++i) {
                int j = i + (1 << (k - 1));
                minp[k][i] = lessp(minp[k - 1][i], minp[k - 1][j]);
            }
        }
    }

    int query(int l, int r)
    {
        int k = 31 - __builtin_clz(r - l + 1);
        return lessp(minp[k][l], minp[k][r - (1 << k) + 1]);
    }
};

RMQ rmq[2];

struct Atom {
    int l, r;

    Atom() { }
    Atom(int l_, int r_): l(l_), r(r_) { assert((r - l) & 1); }

    Pii getval() const
    {
        int x = rmq[l & 1].query(l, r);
        int y = rmq[r & 1].query(x, r);
        return MP(P[x], P[y]);
    }

    friend bool operator< (const Atom &a, const Atom &b)
    {
        return a.getval() > b.getval();
    }
};

void solve()
{
    static int R[MAXN];

    for (int i = 1; i <= N; ++i) {
        R[P[i]] = i;
    }

    for (int i = 1; i <= N; ++i) {
        rmq[i & 1].a[i] = P[i];
        rmq[!(i & 1)].a[i] = +oo;
    }
    rmq[0].init();
    rmq[1].init();

    vector<int> ans;
    std::priority_queue<Atom> q;

    q.push(Atom(1, N));
    while (!q.empty()) {
        Atom u = q.top();
        Pii t = u.getval();
        q.pop();
        ans.push_back(t.x);
        ans.push_back(t.y);
        int tl = R[t.x], tr = R[t.y];
        if (u.l < tl)
            q.push(Atom(u.l, tl - 1));
        if (tl + 1 < tr)
            q.push(Atom(tl + 1, tr - 1));
        if (tr < u.r)
            q.push(Atom(tr + 1, u.r));
    }

    for (auto x : ans) {
        printf("%d ", x);
    }
    puts("");
}

int main()
{
#ifdef __DEBUG
    freopen("E.in", "r", stdin);
    freopen("E.out", "w", stdout);
#endif

    input();
    solve();
}

// 镜湖三百里，菡萏发荷花。
//     -- 李白《子夜吴歌·夏歌》


Measured memory_bytes: 35572