#include "bits/stdc++.h"
#include <functional>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a) (a).begin(),(a).end()
#define pb emplace_back
#define INF (1e9+1)

//0-index 半開区間セグツリー(値の更新or加算 , 最小値or最大値or和) verified AOJ DSL_2,DSL_3
template<class T>
class segtree{
private:
    vector<T> dat;
    int _size;  //the number of leaves
    T _init;
    
public:
    segtree(int __size=0 , T init=numeric_limits<T>::max() ):_size(__size) , _init(init){
        if(__size==0)return ;
        int x=1;
        while(x<_size)x*=2;
        _size = x;
        dat.resize(_size*2);
    }
    
    void init(vector<T> v){
        for(int i=_size;i<_size+v.size();i++) dat[i] = v[i-_size];
        for(int i=_size-1;i>=1;i--)dat[i] = min(dat[i*2],dat[i*2+1]); //minimum
        //for(int i=_size-1;i>=1;i--)dat[i] = max(dat[i*2],dat[i*2+1]); //maximum
        //for(int i=_size-1;i>=1;i--)dat[i] = dat[i*2]+dat[i*2+1];      //sum
    }
    
    void update(int m,T x){
        int i = m+_size;
        //dat[i] += x;    //addition
        dat[i] = x;     //initialize
        for(; i!=1; i/=2){
            dat[i/2] = min(dat[i],dat[i^1]);    //minimum
            //dat[i/2] = max(dat[i],dat[i^1]);    //maximum
            //dat[i/2] = dat[i]+dat[i^1];         //sum
        }
    }
    
    // call find(s,t)
    T find(int s,int t,int num=1,int l=0,int r=-1){
        if(r==-1)r=_size; //I couldn't "int b=_size".
        if(r<=s||t<=l)return _init;
        if(s<=l&&r<=t)return dat[num];
        return min( find(s,t,num*2,l,(l+r)/2) , find(s,t,num*2+1,(l+r)/2,r) );    //minimum
        //return max( find(s,t,num*2,l,(l+r)/2) , find(s,t,num*2+1,(l+r)/2,r) );    //maximum
        //return find(s,t,num*2,l,(l+r)/2) + find(s,t,num*2+1,(l+r)/2,r);           //sum
    }
    int size(){return _size;}
};



int main(){
    int n;
    cin>>n;
    vector<int> v(n);
    rep(i,n)cin>>v[i];
    
    segtree<pii> even(n,pii(INF,INF)),odd(n,pii(INF,INF));
    
    vector<pii> oddv(n,pii(INF,INF)),evenv(n,pii(INF,INF));
    rep(i,n){
        oddv[i].second = i;
        evenv[i].second = i;
        if(i%2==0)evenv[i].first = v[i];
        else oddv[i].first = v[i];
        
    }
    even.init(evenv);
    odd.init(oddv);
    
    auto trans = [&](pii ind){ assert(ind.first>=0);return v[ind.first]; };
    
    auto eval = [&](int l, int r){
        if(r-l<=0)return pii(-1,-1);
        int s,t;
        if(l%2==0){
            s = even.find(l,r).second;
            t = odd.find(s+1,r).second;
        }else{
            s = odd.find(l,r).second;
            t = even.find(s+1,r).second;
        }
        return pii(s,t);
    };
    
    priority_queue<pair<int,pii>,vector<pair<int,pii>>,greater<pair<int,pii>>> pq;
    pii res = eval(0,n);
    pq.push( make_pair(v[res.first],pii(0,n)) );
    
    vector<string> ans;
    while( pq.size() ){
        int s,t,l,r;
        tie(l,r) = pq.top().second;
        tie(s,t) = eval(l,r);
        stringstream sa,sb;
        sa<<v[s];
        sb<<v[t];
        ans.pb(sa.str());
        ans.pb(sb.str());
        
        //l s, s+1 t, t+1 r
        pq.pop();
        if(s-l>0)       pq.push( make_pair(trans(eval(l,s)),pii(l,s)) );
        if(t-(s+1)>0)   pq.push( make_pair(trans(eval(s+1,t)),pii(s+1,t)) );
        if(r-(t+1)>0)   pq.push( make_pair(trans(eval(t+1,r)),pii(t+1,r)) );
    }
    rep(i,ans.size()){
        if(i)cout<<" ";
        cout<<ans[i];
    }
    cout<<endl;
    
}


Measured memory_bytes: 25060