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<bits/stdc++.h>
using namespace std;
int n,a[200010],wei[200010];
struct segs{int l,r,odd,even;}seg[800010];
struct node
{
	int x,y,l,r;
	friend bool operator < (node a,node b){return a.x>b.x;}
};
priority_queue<node> que;
void build(int root,int l,int r)
{
	seg[root].l=l;seg[root].r=r;
	if (l==r)
	{
		seg[root].odd=2100000000;seg[root].even=a[l];
		if (l&1) swap(seg[root].odd,seg[root].even);
		return;
	}
	int mid=(l+r)>>1;
	build(root<<1,l,mid);build((root<<1)+1,mid+1,r);
	seg[root].odd=min(seg[root<<1].odd,seg[(root<<1)+1].odd);
	seg[root].even=min(seg[root<<1].even,seg[(root<<1)+1].even);
}
int query(int root,int l,int r,int p)
{
	if ((seg[root].l>r)||(l>seg[root].r)) return 2100000000;
	if ((l<=seg[root].l)&&(seg[root].r<=r)) return p?seg[root].odd:seg[root].even;
	return min(query(root<<1,l,r,p),query((root<<1)+1,l,r,p));
}
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++) {scanf("%d",&a[i]);wei[a[i]]=i;}
	build(1,1,n);
	int x=query(1,1,n,1),y=query(1,wei[x]+1,n,0);que.push((node){x,y,1,n});
	for (int i=1;i<=n/2;i++)
	{
		node nd=que.top();que.pop();int wx=wei[nd.x],wy=wei[nd.y];
		if (nd.l<wx)
		{
			int x=query(1,nd.l,wx-1,nd.l&1),y=query(1,wei[x],wx-1,(nd.l&1)^1);
			que.push((node){x,y,nd.l,wx-1});
		}
		if (wx+1<wy)
		{
			int x=query(1,wx+1,wy-1,(wx+1)&1),y=query(1,wei[x]+1,wy-1,((wx+1)&1)^1);
			que.push((node){x,y,wx+1,wy-1});
		}
		if (wy<nd.r)
		{
			int x=query(1,wy+1,nd.r,(wy+1)&1),y=query(1,wei[x]+1,nd.r,((wy+1)&1)^1);
			que.push((node){x,y,wy+1,nd.r});
		}
		printf("%d %d ",nd.x,nd.y);
	}
}