#include<bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
using namespace std;
//using namespace __gnu_pbds;
const int inf=0x3f3f3f3f;
const double eps=1e-6;
const int mod=1000000007;
typedef long long ll;
int modpow(int x,int y,int md=mod){if(y==0)return 1;int ret=modpow(x,y>>1,md);ret=(ll)ret*ret%md;if(y&1)ret=(ll)ret*x%md;return ret;}
const int SIZE=131072;
struct SegTree{
	ll f[SIZE<<2]={},lazy[SIZE<<2]={};
	void addpoint(int pos,int v){
		pos+=SIZE-1; 
		f[pos]=v;
		while(pos){
			pos=pos-1>>1;
			f[pos]=min(f[pos+pos+1],f[pos+pos+2]);
		}
	}
	ll query(int l,int r,int l1=0,int r1=SIZE-1,int k=0){
		if(l<=l1&&r1<=r)return f[k]+lazy[k];
		if(r<l1||r1<l)return (ll)1e18;
		lazy[k+k+1]+=lazy[k];lazy[k+k+2]+=lazy[k];lazy[k]=0;
		int mid=l1+r1>>1;
		ll vl=query(l,r,l1,mid,k+k+1);
		ll vr=query(l,r,mid+1,r1,k+k+2);
		f[k]=min(f[k+k+1]+lazy[k+k+1],f[k+k+2]+lazy[k+k+2]);
		return min(vl,vr);
	}
	void addrange(int l,int r,int v,int l1=0,int r1=SIZE-1,int k=0){
		if(l<=l1&&r1<=r){
			lazy[k]+=v;
			return;
		}
		if(l1>r||r1<l)return;
		lazy[k+k+1]+=lazy[k];lazy[k+k+2]+=lazy[k];lazy[k]=0;
		int mid=l1+r1>>1;
		addrange(l,r,v,l1,mid,k+k+1);
		addrange(l,r,v,mid+1,r1,k+k+2);
		f[k]=min(f[k+k+1]+lazy[k+k+1],f[k+k+2]+lazy[k+k+2]);
		return;
	}
}odd,even;
int n;
int a[222222],pos[222222];
set<pair<int,pair<int,int> > >S;//start,segment
void add_set(int l,int r){
	if(l>r)return;
	if(l&1)S.insert(make_pair(odd.query(l,r),make_pair(l,r)));
	else S.insert(make_pair(even.query(l,r),make_pair(l,r)));
}
int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];//random_shuffle(a+1,a+n+1);
	for(int i=1;i<=n;i++)pos[a[i]]=i;
	for(int i=1;i<=n;i+=2)odd.addpoint(i,a[i]),even.addpoint(i,inf);
	for(int i=2;i<=n;i+=2)even.addpoint(i,a[i]),odd.addpoint(i,inf);
	add_set(1,n);int cnt=0;
	while(S.begin()!=S.end()){
		cnt++;if(cnt>n/2+5)return 0;
		int v1=S.begin()->first;
		pair<int,int>segment=S.begin()->second;
//		cerr<<v1<<" "<<segment.first<<" "<<segment.second<<" "<<even.query(pos[v1],segment.second)<<" "<<odd.query(pos[v1],segment.second)<<endl;
		int v2=(pos[v1]&1?even.query(pos[v1],segment.second):odd.query(pos[v1],segment.second));
		cout<<v1<<" "<<v2<<" ";
		odd.addpoint(pos[v1],inf);even.addpoint(pos[v1],inf);
		odd.addpoint(pos[v2],inf);even.addpoint(pos[v2],inf);
		S.erase(S.begin());int l,r;tie(l,r)=segment;
		add_set(l,pos[v1]-1);add_set(pos[v1]+1,pos[v2]-1);add_set(pos[v2]+1,r);
	}
	return 0;
}