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;

const int inf = 1e9;
const int maxn = 2e5;
tuple<int, int> mn[2][4 * maxn];

void upd(int p, int c, int v = 1, int l = 0, int r = maxn)
{
    if(r - l == 1)
    {
        mn[p % 2][v] = tuple<int, int>(c, p);
        return;
    }
    int m = (l + r) / 2;
    if(p < m)
        upd(p, c, 2 * v, l, m);
    else
        upd(p, c, 2 * v + 1, m, r);
    mn[p % 2][v] = min(mn[p % 2][2 * v], mn[p % 2][2 * v + 1]);
}

tuple<int, int> get(int a, int b, int c, int v = 1, int l = 0, int r = maxn)
{
    if(a <= l && r <= b)
        return mn[c][v];
    if(r <= a || b <= l)
        return tuple<int, int>(inf, inf);
    int m = (l + r) / 2;
    return min(get(a, b, c, 2 * v, l, m), get(a, b, c, 2 * v + 1, m ,r));
}

set<tuple<int, int, int, int, int>> que;
void query(int a, int b)
{
    if(b - a == 0)
        return;
    auto A = get(a, b, a % 2);
    auto B = get(get<1>(A), b, (a + 1) % 2);
    que.insert(tuple<int, int, int, int, int>(get<0>(A), get<1>(A), get<1>(B), a, b));
}

signed main()
{
    fill(mn[0], mn[0] + 4 * maxn, make_tuple(inf, inf));
    fill(mn[1], mn[1] + 4 * maxn, make_tuple(inf, inf));
    //freopen("input.txt", "r", stdin);
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    int a[n];
    for(int i = 0; i < n; i++)
    {
        cin >> a[i];
        upd(i, a[i]);
    }
    query(0, n);
    vector<int> ans;
    while(!que.empty())
    {
        int x = get<1>(*begin(que));
        int y = get<2>(*begin(que));
        int l = get<3>(*begin(que));
        int r = get<4>(*begin(que));
        que.erase(begin(que));
        ans.push_back(a[x]);
        ans.push_back(a[y]);
        query(l, x);
        query(x + 1, y);
        query(y + 1, r);
    }
    for(auto it: ans)
        cout << it << ' ';
}
//1001