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 <iostream>
#include <vector>
#include <queue>
using namespace std;

struct RMQ{
	int n = 1, inf = 1 << 30;
	vector<int> tree;

	RMQ(int n_){
		while((n *= 2) < n_);
		tree.assign(2 * n, inf);
	}

	void update(int k, int x){
		tree[k += n] = x;
		while(k){
			k /= 2;
			tree[k] = min(tree[2 * k], tree[2 * k + 1]);
		}
	}

	int rmq(int a, int b, int k, int l, int r){
		if(r <= a || b <= l) return inf;
		if(a <= l && r <= b) return tree[k];
		return min(rmq(a, b, 2 * k, l, (l + r) / 2), rmq(a, b, 2 * k + 1, (l + r) / 2, r));
	}

	int rmq(int a, int b){
		return rmq(a, b, 1, 0, n);
	}
};

int main(){
	int n;
	cin >> n;
	int p[n], puni[n + 1];
	for(int &i:p) cin >> i;
	for(int i = 0; i < n; i++) puni[p[i]] = i;
	RMQ a(n), b(n);
	for(int i = 0; i < n; i++) (i % 2 ? b : a).update(i, p[i]);
	auto rmq = [&](int l, int r){return (l % 2 ? b : a).rmq(l, r);};
	priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> pq;
	pq.push(make_pair(rmq(0, n), make_pair(0, n)));
	while(!pq.empty()){
		pair<int, pair<int, int>> pp = pq.top();
		pq.pop();
		pair<int, int> p = pp.second;
		if(p.first >= p.second) continue;
		int x = (p.first % 2 ? b : a).rmq(p.first, p.second);
		int y = (p.first % 2 ? a : b).rmq(puni[x], p.second);
		cout << x << ' ' << y << ' ';
		pq.push(make_pair(rmq(p.first, puni[x]), make_pair(p.first, puni[x])));
		pq.push(make_pair(rmq(puni[x] + 1, puni[y]), make_pair(puni[x] + 1, puni[y])));
		pq.push(make_pair(rmq(puni[y] + 1, p.second), make_pair(puni[y] + 1, p.second)));
	}
	cout << endl;
}