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];
vector<int> res;
map<pii, pair<pii, pii> > pq;
 
struct segment_tree {
	int a[MAX], t[2*MAX];
	void build() {
		for(int i = 0; i < n; i++) t[i + n] = i;
		for(int i = n-1; i >= 1; i--) {
			if(a[t[i<<1]] < a[t[i<<1|1]]) t[i] = t[i<<1];
			else t[i] = t[i<<1|1];
		}
	}
	int query(int l, int r) {
		int ret = l;
		++r;
		for(l += n, r += n; l < r; l >>= 1, r >>= 1) {
			if(l & 1) {
				if(a[t[l]] < a[ret]) ret = t[l];
				++l;
			}
			if(r & 1) {
				--r;
				if(a[t[r]] < a[ret]) ret = t[r];
			}
		}
		return ret;
	}
} st[2];
 
pii getxy(int l, int r) {
	int xi = st[l % 2].query(l, r);
	int yi = st[l % 2 ^ 1].query(xi+1, r);
	return make_pair(xi, yi);
}

pii getpxy(int l, int r) {
	pii res = getxy(l, r);
	return make_pair(p[res.first], p[res.second]);
}
 
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	fill(st[0].a, st[0].a+n, INT_MAX);
	fill(st[1].a, st[1].a+n, INT_MAX);
	for(int i = 0; i < n; i++) cin >> p[i], st[i % 2].a[i] = p[i];
 
	st[0].build();
	st[1].build();
 
	pq[getpxy(0, n-1)] = make_pair(getxy(0, n-1), make_pair(0, n-1));
	for(int l, r, xi, yi; !pq.empty(); ) {
		tie(xi, yi) = pq.begin()->second.first;
		tie(l, r) = pq.begin()->second.second;
		assert(r-l % 2 > 0 && yi-xi % 2 > 0);
		pq.erase(pq.begin());
		res.push_back(p[xi]);
		res.push_back(p[yi]);
		if(xi+1 < yi-1) pq[getpxy(xi+1, yi-1)] = make_pair(getxy(xi+1, yi-1), make_pair(xi+1, yi-1));
		if(l < xi-1) pq[getpxy(l, xi-1)] = make_pair(getxy(l, xi-1), make_pair(l, xi-1));
		if(yi+1 < r) pq[getpxy(yi+1, r)] = make_pair(getxy(yi+1, r), make_pair(yi+1, r));
	}
	for(int i = 0; i < n; i++) cout << res[i] << ' ';
}