#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(v[0], o.v[0]), min(v[1], o.v[1]) };
        }
        pair<int, int> v[2];
    };
    using Lazy = int;

    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 j = m; j >= 2; j /= 2) propagate(i / 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;
        for (int j = 0; j < 2; j++) act(i * 2 + j, z[i]);
        z[i] = 0;
    }
    void act(int i, Lazy x) {
        swap(t[i].v[0], t[i].v[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].v[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().v[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).v[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: 11648