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>
#define LL long long
using namespace std;
const int maxn=2e5+10;

typedef pair<int,pair<int,int> > triple;
int a[maxn];
int pos[maxn];
priority_queue<triple> q;
int D[maxn];
int dp[21][maxn];

int rmq(int l,int r)
{
    int c=(r-l)/2+1;
    int d=D[c];
    return min(dp[d][l],dp[d][r-2*((1<<d)-1)]);
}

void add(int l,int r)
{
    if(l>r)
        return ;
    int v=rmq(l,r-1);
    q.push(make_pair(-v,make_pair(l,r)));
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]),--a[i],pos[a[i]]=i;
    for(int i=1;i<=n;i++){
        int d=D[i-1];
        if(i>=1<<(d+1))d++;
        D[i]=d;
    }
    for(int i=0;i<n;i++)
        dp[0][i]=a[i];
    for(int d=1,len=2;d<=20;d++,len*=2){
        for(int i=0;i<n;i++){
            dp[d][i]=dp[d-1][i];
            if(i+len<n)
                dp[d][i]=min(dp[d-1][i],dp[d-1][i+len]);
        }
    }

    add(0,n-1);
    for(int i=0;i<n/2;i++){
        triple T=q.top();
        q.pop();
        int l=T.second.first,r=T.second.second;
        int v1=rmq(l,r-1);
        int pos1=pos[v1];
        int v2=rmq(pos1+1,r);
        int pos2=pos[v2];
        printf("%d %d ",v1+1,v2+1);
        add(l,pos1-1);
        add(pos1+1,pos2-1);
        add(pos2+1,r);
    }
    return 0;
}