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<cstdio>
#include<queue>
using namespace std;
int n,tot,p[200001],rnk[200001],t[2][200001][18],log_2[200001];
struct result {
    int fir,sec;
    int son[3];
    inline bool operator <(result ano)const
    {
        return fir>ano.fir;
    }
}res[100001];
priority_queue<result>q;
inline int min(int x,int y)
{
    return x<=y?x:y;
}
void init_log2()
{
    for(int i=2;i<=n;++i) {
        log_2[i]=log_2[i>>1]+1;
    }
    return;
}
void init_t()
{
    for(int i=1;i<=n;++i) {
        t[i&1][i][0]=p[i],t[(i&1)^1][i][0]=n+1;
    }
    for(int i=0;i<2;++i) {
        for(int j=1;j<18;++j) {
            for(int k=1;k+(1<<j)-1<=n;++k) {
                t[i][k][j]=min(t[i][k][j-1],t[i][k+(1<<j-1)][j-1]);
            }
        }
    }
    return;
}
int query(int opt,int l,int r)
{
    int k=log_2[r-l+1];
    return min(t[opt][l][k],t[opt][r-(1<<k)+1][k]);
}
int func(int l,int r)
{
    if(l>r) {
        return 0;
    }
    int nl=rnk[res[++tot].fir=query(l&1,l,r)];
    int nr=rnk[res[tot].sec=query(r&1,nl+1,r)];
    int ret=tot;
    res[ret].son[0]=func(l,nl-1);
    res[ret].son[1]=func(nr+1,r);
    res[ret].son[2]=func(nl+1,nr-1);
    return ret;
}
int main()
{
    scanf("%d",&n);
    init_log2();
    for(int i=1;i<=n;++i) {
        scanf("%d",&p[i]);
        rnk[p[i]]=i;
    }
    init_t();
    func(1,n);
    q.push(res[1]);
    while(!q.empty()) {
        result now=q.top();
        q.pop();
        printf("%d %d ",now.fir,now.sec);
        for(int i=0;i<3;++i) {
            if(now.son[i]) {
                q.push(res[now.son[i]]);
            }
        }
    }
    return 0;
}