#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long LL;
typedef pair<int,int> P;
const LL mod=1000000007;
const LL LINF=1LL<<60;
const int INF=1<<30;



struct SegmentTree{
    private:
        int n;
        vector<int> node;

    public:
        SegmentTree(vector<int> v){
            int sz = v.size();
            n = 1;
            while(n < sz) n *= 2;
            node.resize(2*n-1,INF);
            for (int i = 0; i < sz; i++) node[i+n-1] = v[i];
            for (int i = n-2; i >= 0; i--) node[i] = min(node[2*i+1],node[2*i+2]);
        }

        void update(int x,int val){
            x += n-1;
            node[x] = val;
            while(x > 0){
                x = (x - 1) / 2;
                node[x] = min(node[2*x+1],node[2*x+2]);
            }
        }

        int getmin(int a, int b, int k=0, int l=0, int r=-1){
            if (r < 0) r = n;
            if(r <= a || b <= l) return INF;
            if(a <= l && r <= b) return node[k];
            int vl = getmin(a, b, 2*k+1, l , (l+r)/2);
            int vr = getmin(a, b, 2*k+2, (l+r)/2,r);
            return min(vl, vr);
        }
};


map<int,int> ma;


int main(){
    int n;cin >> n;
    vector<int> a(n,n+1),b(n,n+1);
    for (int i = 0; i < n; i++) {
        if(i%2){
            cin >> b[i];
            ma[b[i]] = i;
        }
        else{
            cin >> a[i];
            ma[a[i]] = i;
        }
    }
    SegmentTree seg1(a),seg2(b);
    vector<int> ans;
    priority_queue<pair<P,pair<P,bool>>,vector<pair<P,pair<P,bool>>>, greater<pair<P,pair<P,bool>>>> pq;
    pq.push(make_pair(make_pair(0,0),make_pair(make_pair(0,n),false)));
    while(!pq.empty()){
        auto p = pq.top();pq.pop();
        int l = p.second.first.first, r = p.second.first.second;
        if(r<=l) continue;
        bool c = p.second.second;
        if(c==0){
            int ll = seg1.getmin(l,r);
            int rr = seg2.getmin(ma[ll]+1,r);
            ans.push_back(ll);
            ans.push_back(rr);
            int li = ma[ll], ri = ma[rr];
            int xx = seg1.getmin(l,li);
            int yy = seg2.getmin(ma[xx]+1,li);
            pq.push(make_pair(make_pair(xx,yy),make_pair(make_pair(l,li),c)));
            xx = seg2.getmin(li+1,ri), yy = seg1.getmin(ma[xx]+1,ri);
            pq.push(make_pair(make_pair(xx,yy),make_pair(make_pair(li+1,ri),c^1)));
            xx = seg1.getmin(ri+1,r), yy = seg2.getmin(ma[xx]+1,r);
            pq.push(make_pair(make_pair(xx,yy),make_pair(make_pair(ri+1,r),c)));
        }
        else{
            int ll = seg2.getmin(l,r);
            int rr = seg1.getmin(ma[ll]+1,r);
            ans.push_back(ll);
            ans.push_back(rr);
            int li = ma[ll], ri = ma[rr];
            int xx = seg2.getmin(l,li);
            int yy = seg1.getmin(ma[xx]+1,li);
            pq.push(make_pair(make_pair(xx,yy),make_pair(make_pair(l,li),c)));
            xx = seg1.getmin(li+1,ri), yy = seg2.getmin(ma[xx]+1,ri);
            pq.push(make_pair(make_pair(xx,yy),make_pair(make_pair(li+1,ri),c^1)));
            xx = seg2.getmin(ri+1,r), yy = seg1.getmin(ma[xx]+1,r);
            pq.push(make_pair(make_pair(xx,yy),make_pair(make_pair(ri+1,r),c)));
        }
    }
    for (int i = 0; i < n; i++) {
        cout << ans[i] << (i==n-1?"\n":" ");
    }
    return 0;
}


Measured memory_bytes: 23660