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;
typedef pair<int,int> pii;
const int MAX = (2e5)+1;

int n, p[MAX], pos[MAX];
vector<int> res;
priority_queue<pair<pii, pii> > pq;

struct segment_tree {
	int mod, t[2*MAX];
	void build() {
		for(int i = 0; i < n; i++) t[i + n] = (i % 2 == mod ? p[i] : INT_MAX);
		for(int i = n-1; i >= 1; i--) t[i] = min(t[i<<1], t[i<<1|1]);
	}
	int query(int l, int r) {
		++r;
		int ret = INT_MAX;
		for(l += n, r += n; l < r; l >>= 1, r >>= 1) {
			if(l & 1) ret = min(t[l++], ret);
			if(r & 1) ret = min(t[--r], ret);
		}
		return ret;
	}
} st[2];

pii getxy(int l, int r) {
	int x = st[l % 2].query(l, r);
	int y = st[l % 2 ^ 1].query(pos[x]+1, r);
	return make_pair(-x, -y);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	st[0].mod = 0, st[1].mod = 1;
	cin >> n;
	for(int i = 0; i < n; i++) {
		cin >> p[i];
		pos[p[i]] = i;
	}

	st[0].build();
	st[1].build();
	pq.emplace(make_pair(getxy(0, n-1), make_pair(0, n-1)));
	for(int l, r, x, y; !pq.empty(); ) {
		tie(x, y) = pq.top().first; x = -x; y = -y;
		tie(l, r) = pq.top().second;
		pq.pop();
		res.push_back(x);
		res.push_back(y);
		x = pos[x], y = pos[y];
		if(x+1 < y-1) pq.emplace(make_pair(getxy(x+1, y-1), make_pair(x+1, y-1)));
		if(l < x-1) pq.emplace(make_pair(getxy(l, x-1), make_pair(l, x-1)));
		if(y+1 < r) pq.emplace(make_pair(getxy(y+1, r), make_pair(y+1,r)));
	}
	for(int i = 0; i < n; i++) cout << res[i] << ' ';
}