#include<bits/stdc++.h>

#define INF 1000010000
#define nl '\n'
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define fi first
#define se second
#define pii pair<int,int>
#define pdd pair<double,double>
#define all(c) (c).begin(), (c).end()
#define SORT(c) sort(all(c))
#define sz(c) (c).size()
#define rep(i,n) for( int i = 0; i < n; ++i )
#define repi(i,n) for( int i = 1 ; i <= n; ++i )
#define repn(i,n) for( int i = n - 1 ; i >= 0 ; --i )
#define repf(j,i,n) for( int j = i ; j < n ; ++j )
#define die(s) {std::cout << s << nl;}
#define dier(s) {std::cout << s; return 0;}
#define vi vector<int>
typedef long long ll;
#define MAXN 202020
using namespace std;
vector<pair<int , pii>> tot;

vi pos;

vector<vi> t(2 , vi(2 * MAXN , INF));
int n;

void build(int par){
    for(int i = n - 1 ; i > 0 ; --i){
        t[par][i] = min(t[par][2 * i] , t[par][2 * i + 1]);
    }
}
 
void modify(int par , int p , int val){
    for(t[par][p += n] = val ; p > 1 ; p /= 2){
        t[par][p / 2] = min(t[par][p] , t[par][p ^ 1]);
    }
}
 
int query(int par , int l , int r){//[l , r)
    int res = INF;
    for(l += n , r += n ; l < r ; l /= 2 , r /= 2){
        if(l % 2){
            res = min(res, t[par][l]);
            ++l;
        }
        if(r % 2){
            --r;
            res = min(res , t[par][r]);
        }
    }
    return res;
}


map<int , vector<pii>> ans;

int last = 0;
map<int , pii> code;
map<pii , int> rcode;

vi g[MAXN];

void solve(vi & p , int l , int r , int k , int prev = -1){
	if(r - l < 1)return;
//	cout << l << " " << r << " " << INF - k <<nl;
	if(r >= sz(p) || l < 0)return;
	if(r - l == 1){
	//	cout << p[l] << " " <<  p[r] << nl;
	//	tot.pb({INF - k , {p[l] , p[r]}});
		code[last] = {p[l] , p[r]};
		rcode[{p[l] , p[r]}] = last;
		if(prev != -1){
			g[prev].pb(last);
		}
		prev = last;
		++last;
		ans[INF - k].pb({p[l] , p[r]});
	}else{
	
	/*	vector<pii> can;
		for(int i = l ; i <= r ; i += 2){
			for(int j = i + 1 ; j <= r ; j += 2){
				can.pb({p[i] , p[j]});
			}
		}
		SORT(can);
		
		pii o1 = can[0];
		*/
	//	cout << l << " " << r << nl;
	//	if(INF - k > 0)exit(0);
		pii o = {0 , 0};
		
		int par = l % 2;
		o.fi = query(par , l , r + 1);
		if(sz(tot) * 2 == sz(p))exit(0);
		int pos1 = pos[o.fi];
	//	o.se = query(par ^ 1 , l , r + 1);
		o.se = query((pos1 + 1) % 2 , pos1 + 1 , r + 1);
	//	cout << "res:" << o.fi << " " << o.se << nl;
	/*	
		cout << l << " " << r << " \n";
		for(int i = l ; i <= r ; i += 2){
			cout << p[i] << " ";
		}
		cout << nl;
		cout << "Seg tree\n";
		cout << o.fi << " " << o.se << nl;
		cout << "Brute\n";
		cout << o1.fi << " " << o1.se << nl;
		for(int i = l ; i <= r ; ++i){
			cout << query(l % 2 , i , i + 1) << " ";
		}
		cout << nl;*/
	/*	for(int i = l ; i <= r ; ++i){
			cout << query((l + 1) % 2 , i , i + 1) << " ";
		}*/
/*		cout << nl;
		cout << nl;*/
	
	//	exit(0);
	//	tot.pb({INF - k , o});
		code[last] = o;
		rcode[o] = last;
		if(prev != -1){
			g[prev].pb(last);
		}
		prev = last;
		++last;
		
		ans[INF - k].pb(o);
		int pos2 = pos[o.se];
		
		rep(q , 2){
			modify(q , pos1 , INF);
			modify(q , pos2 , INF);
	//		cout << query(q , pos1 , pos1 + 1) << " " << query(q , pos2 , pos2 + 1) << nl;
		}
		int d = (r - l <= 4);
		solve(p , pos1 + 1 , pos2 - 1 , k - 1 , prev);
		solve(p , l , pos1 - 1 , k - 1 , prev);
		solve(p , pos2 + 1 , r , k - 1 , prev);
		
	}
}

vi restore(){
	vi res;
	priority_queue<pii , vector<pii> , greater<pii>> q; 
	q.push(code[0]);
	while(!q.empty()){
		pii u = q.top();
		q.pop();
		res.pb(u.fi);
		res.pb(u.se);
		
		for(int c : g[rcode[u]]){
			q.push(code[c]);
		}
	}
	return res;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.precision(0); 


	cin >> n;
	
	pos.resize(n + 1);
	
	vi p(n);
	rep(i , n){
		cin >> p[i];
		pos[p[i]] = i;
		int par = i % 2;
		t[par][n + i] = p[i];
		t[par ^ 1][n + i] = INF;
	}
/*	
	rep(i , n){
		cout << t[0][n + i] << " ";
	}
	cout << nl;
		rep(i , n){
		cout << t[1][n + i] << " ";
	}
	cout << nl;*/
	
	build(0);
	build(1);
	
	solve(p , 0 , n - 1 , INF);
	
	SORT(tot);
	
//	for(auto e : tot){
	//	pii o = e.se;
	//	cout << e.fi << nl;
	//	cout << o.fi << " " << o.se << nl;
	//	cout << o.fi << " " << o.se << " ";
//	}
/*
	for(auto e : ans){
		SORT(e.se);
		for(pii o : e.se){
			cout << o.fi << " " << o.se << " ";
		}
	}
	
	cout << nl;
	rep(i , last){
		cout << "code : {" << code[i].fi << " " << code[i].se << "}" << nl;
		for(int u : g[i]){
			cout << "{" << code[u].fi  << " " << code[u].se << "} ";
		}
		cout << nl;
	}*/
	
	for(int e : restore()){
		cout << e << " ";
	}
	
	return 0;
}

Measured memory_bytes: 51796