#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

struct LazySegmentTree {
    struct Node {
        Node operator*(const Node& o) const {
            return { min(m[0], o.m[0]), min(m[1], o.m[1]) };
        }
        pair<int, int> m[2];
    };
    using Lazy = int8_t;

    LazySegmentTree(int n) : e({ { { 1 << 30, 0 }, { 1 << 30, 0 } } }), n(n) {
        for (m = 2; m < n; m *= 2);
        t = vector<Node>(m + n + n % 2, e);
        z = vector<Lazy>(m, 0);
    }
    Node& operator[](int i) {
        return t[m + i];
    }
    const Node& root() const {
        return t[1];
    }
    void build() {
        for (int i = (n - 1 + m) / 2; i > 0; i--) {
            t[i] = t[i * 2] * t[i * 2 + 1];
        }
    }
    void update(int i, const Node& o) {
        i += m;
        for (int k = 30; k > 0; k--) {
            int j = i >> k;
            if (j == 0) continue;
            propagate(j);
        }
        for (t[i] = o; i /= 2, i > 0;) t[i] = t[i * 2] * t[i * 2 + 1];
    }
    Node query(int l, int r, int i = 1, int il = 0, int ir = -1) {
        if (ir < 0) ir = m;
        if (l <= il && ir <= r) return t[i];
        propagate(i);
        Node o = e;
        int ic = (il + ir) / 2;
        if (l < ic) o = o * query(l, r, i * 2 + 0, il, ic);
        if (ic < r) o = o * query(l, r, i * 2 + 1, ic, ir);
        return o;
    }
    void range_act(int l, int r, Lazy x, int i = 1, int il = 0, int ir = -1) { 
        if (ir < 0) ir = m;
        if (l <= il && ir <= r) {
            act(i, x);
            return;
        }
        propagate(i);
        int ic = (il + ir) / 2;
        if (l < ic) range_act(l, r, x, i * 2 + 0, il, ic);
        if (ic < r) range_act(l, r, x, i * 2 + 1, ic, ir);
        t[i] = t[i * 2] * t[i * 2 + 1];
    }
    void propagate(int i) {
        if (z[i] == 0) return;
        act(i * 2 + 0, z[i]);
        act(i * 2 + 1, z[i]);
        z[i] = 0;
    }
    void act(int i, Lazy x) {
        swap(t[i].m[0], t[i].m[1]);
        if (i < m) z[i] ^= x;
    }
    vector<Node> t;
    vector<Lazy> z;
    const Node e;
    int n, m;
};

struct SegmentTree {
    struct Node {
        Node operator*(const Node& o) const {
            return { min(v, o.v) };
        }
        int v;
    };

    SegmentTree(int n) : e({ 1 << 30 }), n(n) {
        for (m = 2; m < n; m *= 2);
        t = vector<Node>(m + n + n % 2, e);
    }
    Node& operator[](int i) {
        return t[m + i];
    }
    const Node& root() const {
        return t[1];
    }
    void build() {
        for (int i = (n - 1 + m) / 2; i > 0; i--) {
            t[i] = t[i * 2] * t[i * 2 + 1];
        }
    }
    void update(int i, const Node& o) {
        for (t[i += m] = o; i /= 2, i > 0;) t[i] = t[i * 2] * t[i * 2 + 1];
    }
    Node query(int l, int r) {
        Node o[2] = { e, e };
        for (l += m, r += m; l < r; l /= 2, r /= 2) {
            if (r & 1) o[1] = t[--r] * o[1];
            if (l & 1) o[0] = o[0] * t[l++];
        }
        return o[0] * o[1];
    }
    void print() {
        for (int j = 1; j <= m; j *= 2) {
            int k = min(j * 2, m + n);
            for (int i = j; i < k; i++) {
                cout << t[i].v << " \n"[i == k - 1];
            }
        }
    }
    vector<Node> t;
    const Node e;
    int n, m;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;

    LazySegmentTree lst(n);
    for (int i = 0; i < n; i++) {
        auto& p = lst[i].m[i & 1];
        cin >> p.first;
        p.second = i;
    }
    lst.build();

    SegmentTree st(n);
    for (int i = 0; i < n; i += 2) {
        auto p = lst.root().m[0];
        cout << p.first << ' ';
        lst.update(p.second, lst.e);

        int k = min(st.query(p.second + 1, n).v, n);
        auto q = lst.query(p.second + 1, k).m[1];
        cout << q.first << " \n"[i == n - 2];
        lst.update(q.second, lst.e);

        st.update(p.second, { p.second });
        st.update(q.second, { q.second });
        lst.range_act(p.second + 1, q.second, 1);
    }

    return 0;
}

Measured memory_bytes: 10880