#ifdef LOCAL111
	#define _GLIBCXX_DEBUG
#else
	#define NDEBUG
#endif
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
const int INF = 1e9;
using namespace std;
template<typename T, typename U> ostream& operator<< (ostream& os, const pair<T,U>& p) { cout << '(' << p.first << ' ' << p.second << ')'; return os; }

#define endl '\n'
#define ALL(a)  (a).begin(),(a).end()
#define SZ(a) int((a).size())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n)  FOR(i,0,n)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#ifdef LOCAL111
	#define DEBUG(x) cout<<#x<<": "<<(x)<<endl
	template<typename T> void dpite(T a, T b){ for(T ite = a; ite != b; ite++) cout << (ite == a ? "" : " ") << *ite; cout << endl;}
#else
	#define DEBUG(x) true
	template<typename T> void dpite(T a, T b){ return; }
#endif
#define F first
#define S second
#define SNP string::npos
#define WRC(hoge) cout << "Case #" << (hoge)+1 << ": "
template<typename T> void pite(T a, T b){ for(T ite = a; ite != b; ite++) cout << (ite == a ? "" : " ") << *ite; cout << endl;}
template<typename T> bool chmax(T& a, T b){if(a < b){a = b; return true;} return false;}
template<typename T> bool chmin(T& a, T b){if(a > b){a = b; return true;} return false;}

typedef long long int LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;

void ios_init(){
	//cout.setf(ios::fixed);
	//cout.precision(12);
#ifdef LOCAL111
	return;
#endif
	ios::sync_with_stdio(false); cin.tie(0);	
}

template <typename T>
class Segtree {
// private:
public:
	
	vector<T> dat;
	T init;
	int n;
	function<T(T,T)> f;

	// T f(T x, T y){
	// 	return ;
	// }

	Segtree(int n_, T init_, function<T(T,T)> f_){
		init = init_; //単位元を代入させる。
		n = 1;
		f = f_;
		while(n_ > n){
			n <<= 1;
		}
		dat = vector<T>(2*n);
		for(int i = 0; i < 2*n; i++){
			dat[i] = init;
		}
	}

	void update(int k, T a){
		k += n-1;
		dat[k] = a;
		while(k > 0){
			k = (k-1)/2;
			dat[k] = f(dat[2*k+1],dat[2*k+2]);
		}
	}

	T _query(int a, int b, int k, int l, int r){
		if(r <= a || b <= l) return  init;

		if(a <= l && r <= b)	return dat[k];
		else{
			T vl = _query(a, b, k*2+1, l, (l+r)/2);
			T vr = _query(a, b, k*2+2, (l+r)/2, r);
			return f(vl,vr);
		}
	}

	T query(int a, int b){
		return _query(a, b, 0, 0, n);
	}
};

int main()
{
	ios_init();
	int n;
	while(cin >> n) {
		vector<int> p(n);
		vector<int> table(n);
		REP(i,n){
			cin >> p[i];
			p[i]--;
			table[p[i]] = i;
		}
		auto mif = [](int x, int y) -> int {return min(x,y);};
		vector<Segtree<int>> seg(2,Segtree<int>(n/2,INF,mif));
		REP(i,n/2){
			DEBUG(i);
			seg[0].update(i,p[2*i]);
			seg[1].update(i,p[2*i+1]);
		}
		dpite(ALL(seg[0].dat));
		dpite(ALL(seg[1].dat));
		using T = tuple<int,int,int,int>;
		vector<int> ans;
		ans.reserve(0);
		priority_queue<T, vector<T>, greater<T>> qu;
		// {
		// 	int l = seg[0].query(0,n/2);
		// 	int r = seg[1].query(table[l],n/2);
		// 	DEBUG(l); DEBUG(r);
		// 	qu.emplace(l,r,0,n);
		// }
		auto ins = [&](int ll, int rr) {
			if(rr-ll <= 1) return;
			DEBUG(ll); DEBUG(rr);
			int l = seg[ll&1].query(ll/2,ll/2+(rr-ll)/2);
			// DEBUG(l); DEBUG(table[l]); DEBUG(table[l]/2+(ll&1)); DEBUG((rr-ll));
			int r = seg[!(ll&1)].query(table[l]/2+(ll&1),ll/2+(rr-ll)/2+(ll&1));
			DEBUG(table[l]); DEBUG(table[r]); DEBUG(l); DEBUG(r);
			qu.emplace(l,r,ll,rr);
		};
		ins(0,n);
		while(!qu.empty()){
			int x, y, l, r;
			tie(x,y,l,r) = qu.top();
			DEBUG(x); DEBUG(y); DEBUG(l); DEBUG(r);
			qu.pop();
			ans.push_back(x+1);
			ans.push_back(y+1);
			ins(l,table[x]);
			ins(table[x]+1,table[y]);
			ins(table[y]+1,r);
			DEBUG(endl);
		}
		pite(ALL(ans));
	}
	return 0;
}


Measured memory_bytes: 7548