Problem: Let N be a positive even number.

We have a permutation of (1, 2, ..., N), p = (p_1, p_2, ..., p_N). Snuke is constructing another permutation of (1, 2, ..., N), q, following the procedure below.

First, let q be an empty sequence. Then, perform the following operation until p becomes empty:

* Select two adjacent elements in p, and call them x and y in order. Remove x and y from p (reducing the length of p by 2), and insert x and y, preserving the original order, at the beginning of q.



When p becomes empty, q will be a permutation of (1, 2, ..., N).

Find the lexicographically smallest permutation that can be obtained as q.

Constraints

* N is an even number.
* 2 ≤ N ≤ 2 × 10^5
* p is a permutation of (1, 2, ..., N).

Input

Input is given from Standard Input in the following format:


N
p_1 p_2 ... p_N


Output

Print the lexicographically smallest permutation, with spaces in between.

Examples

Input

4
3 2 4 1


Output

3 1 2 4


Input

2
1 2


Output

1 2


Input

8
4 6 3 2 8 5 7 1


Output

3 1 2 7 4 6 8 5

Solution (in CPP):

Solution:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
using namespace std;
const int maxn=200010;
int n,a[maxn],pl[maxn],q[maxn],st[2][maxn][25];

struct node
{
	int t,l,r;
};
bool operator <(node a,node b) {return a.t>b.t;}
priority_queue <node> h;

int rmq(int f,int l,int r)
{
	int k=(int)(log(r-l+1)/log(2));
	//cout<<"rmq:"<<f<<' '<<l<<' '<<r<<' '<<k<<' '<<min(st[f][l][k],st[f][r-(1<<k)+1][k])<<endl;
	return min(st[f][l][k],st[f][r-(1<<k)+1][k]);
}
int main()
{
	scanf("%d",&n);
	memset(st,0x3f,sizeof(st));
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		pl[a[i]]=i;
		st[i&1][i][0]=a[i];
	}
	for(int k=1;(1<<k)<=n;k++)
	{
		for(int i=1;i<=n-(1<<k)+1;i++)
			for(int f=0;f<=1;f++)
				st[f][i][k]=min(st[f][i][k-1],st[f][i+(1<<(k-1))][k-1]);
	}			
	/*for(int f=0;f<=1;f++)
	{
		for(int i=1;i<=n;i++)
		{
			for(int k=0;(1<<k)<=n;k++)
				cout<<st[f][i][k]<<' ';
			cout<<endl;
		}
		cout<<endl;
	}*/				
	h.push((node){rmq(1,1,n),1,n});
	for(int i=1;i<=n/2;i++)	
	{	
		node w=h.top();
		h.pop();
		//cout<<i<<' '<<w.t<<' '<<w.l<<' '<<w.r<<endl;
		int f=pl[w.t]&1,ano=rmq(f^1,pl[w.t]+1,w.r);
		q[2*i-1]=w.t;q[2*i]=ano;
		//cout<<f<<' '<<ano<<endl;
		if(pl[w.t]>w.l) h.push((node){rmq(f,w.l,pl[w.t]-1),w.l,pl[w.t]-1});
		if(pl[ano]-1>pl[w.t]+1) h.push((node){rmq(f^1,pl[w.t]+1,pl[ano]-1),pl[w.t]+1,pl[ano]-1});
		if(w.r>pl[ano]) h.push((node){rmq(f,pl[ano]+1,w.r),pl[ano]+1,w.r});
	}
	for(int i=1;i<=n;i++)
		printf("%d ",q[i]);
	return 0;
}
/*8
4 6 3 2 8 5 7 1*/