#include <bits/stdc++.h>
    using namespace std;
     
    #define DUMP(x) cerr << #x << "=" << x << endl
    #define DUMP2(x, y) cerr<<"("<<#x<<", "<<#y<<") = ("<<x<<", "<<y<<")"<< endl
    #define BINARY(x) static_cast<bitset<16> >(x)
     
    #define rep(i,n) for(int i=0;i<(int)(n);i++)
    #define REP(i,m,n) for (int i=m;i<(int)(n);i++)
     
    #define in_range(x, y, w, h) (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))
    #define ALL(a) (a).begin(),(a).end()
     
    typedef long long ll;
    const int INF   = 1e9;
    const ll  INFLL = 1e18;
    typedef pair<int, int> PII;
    int dx[4]={0, -1, 1, 0}, dy[4]={-1, 0, 0, 1};
     
    class RMQ {
        int n;
        vector<int> dat;
        
    public:
        RMQ(int _n) {init(_n);}
        RMQ(){}
        
        void init(int _n) {
            n=1;
            while (n<_n) n*=2;
            dat.resize(2*n-1, INT_MAX);
        }
        
        void update(int k, int a) {
            k += n-1;
            dat[k] = a;
            while (k>0) {
                k = (k-1)/2;
                dat[k] = min(dat[k*2+1], dat[k*2+2]);
            }
        }
        
        int size() {
            return n;
        }
        
        int query(int a, int b, int k, int l, int r) {
            if (r <= a || b <= l) return INT_MAX;
            if (a <= l && r <= b) return dat[k];
            else {
                int vl = query(a, b, k*2+1, l, (l+r)/2);
                int vr = query(a, b, k*2+2, (l+r)/2, r);
                return min(vl, vr);
            }
        }
     
        int query(int l, int r)
        {
            return query(l, r, 0, 0, n);
        }
    };
     
     
    typedef pair<int, pair<int, int>> State;
     
    int main()
    {
        ios::sync_with_stdio(false);
     
        RMQ rmq_odd, rmq_even;
        map<int, int> num2idx;
        int N; cin >> N;
        rmq_odd.init(N);
        rmq_even.init(N);
     
        rep(i, N) {
            int A; cin >> A;
            if (i%2 == 1) rmq_odd.update(i, A);
            else          rmq_even.update(i, A);
            num2idx[A] = i;
        }
     
        priority_queue<State, vector<State>, greater<State>> Q;
        Q.emplace(rmq_even.query(0, N), PII(0, N));
        bool first = true;
        while (Q.size()) {
            if (!first) {
                cout << " ";
            } else {
                first = false;
            }
     
            State s = Q.top(); Q.pop();
            int l = s.second.first, r = s.second.second;
            RMQ *rmq_f, *rmq_s;
     
            if (l >= r) continue;
     
            if (l%2 == 0) {
                rmq_f = &rmq_even;
                rmq_s = &rmq_odd;
            } else {
                rmq_f = &rmq_odd;
                rmq_s = &rmq_even;
            }
     
            int ln, li, rn, ri;
            ln = rmq_f->query(l, r);
            li = num2idx[ln];
            rn = rmq_s->query(li+1, r);
            ri = num2idx[rn];
     
            Q.emplace(rmq_f->query(l, li), PII(l, li));
            Q.emplace(rmq_s->query(li+1, ri), PII(li+1, ri));
            Q.emplace(rmq_f->query(ri+1, r), PII(ri+1, r));
     
            cout << ln << " " << rn;
        }
        cout << endl;
    }

Measured memory_bytes: 19060