/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#ifndef MAJK_LIB
#define MAJK_LIB

#include <vector>
#include <stack>
#include <iostream>
#include <unordered_map>
#include <map>
#include <iomanip>
#include <set>
#include <functional>
#include <fstream>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <string>
#include <sstream>
#include <queue>
#include <bitset>
using namespace std;

#define x first
#define y second
constexpr int MOD = 1000000007;

typedef std::pair<int,int> pii;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;


template<typename T, typename F> T foo(T t, T g, F f) {
	return t;
}


template <typename T, typename U> std::istream&operator>>(std::istream&i, pair<T,U>&p) {i >> p.x >> p.y; return i;}
template<typename T>std::istream&operator>>(std::istream&i,vector<T>&t) {for(auto&v:t){i>>v;}return i;}
template<typename T>std::ostream&operator<<(std::ostream&o,const vector<T>&t) {for(size_t i=0;i<t.size();++i){o<<t[i]<<" \n"[i == t.size()-1];}return o;}
template<typename T> using minheap = priority_queue<T, vector<T>, greater<T>>;
template<typename T> using maxheap = priority_queue<T, vector<T>, less<T>>;
auto fraclt = [](const pii&a,const pii&b) { return (ll)a.x * b.y < (ll)b.x * a.y; };
struct cmpfrac { bool operator()(const pii&a,const pii&b)const { return (ll)a.x * b.y < (ll)b.x * a.y; }};
template <typename T> bool in(T a, T b, T c) { return a <= b && b < c; }
int logceil(ll x) {int b=0;while(x){x>>=1;++b;}return b;}

namespace std {
    template<typename T,typename U>struct hash<pair<T,U>>{hash<T>t;hash<U>u;size_t operator()(const pair<T,U>&p)const{return t(p.x)^(u(p.y)<<7);}};
}
template<typename T,typename F>T bsh(T l,T h,const F&f){T r=-1,m;while(l<=h){m=(l+h)/2;if(f(m)){l=m+1;r=m;}else{h=m-1;}}return r;}
template<typename T,typename F>T bsl(T l,T h,const F&f){T r=-1,m;while(l<=h){m=(l+h)/2;if(f(m)){h=m-1;r=m;}else{l=m+1;}}return r;}
template<typename T> T gcd(T a,T b) { if (a<b) swap(a,b); return b?gcd(b,a%b):a; }

template<typename T>class vector2:public vector<vector<T>>{public:vector2(){} vector2(size_t a,size_t b,T t=T()):vector<vector<T>>(a,vector<T>(b,t)){}};
template<typename T>class vector3:public vector<vector<vector<T>>>{public:vector3(){} vector3(size_t a,size_t b,size_t c,T t=T()):vector<vector<vector<T>>>(a,vector<vector<T>>(b,vector<T>(c,t))){}};
template <typename T> struct bounded_priority_queue {
	inline bounded_priority_queue(ui X) : A(X), B(0) {}
	inline void push(ui L, T V) { B = max(B, L); A[L].push(V); }
	inline const T &top() const { return A[B].front(); }
	inline void pop() { A[B].pop(); while (B > 0 && A[B].empty()) --B; }
	inline bool empty() const { return A[B].empty(); }
	inline void clear() { B = 0; for (auto &a: A) a = queue<T>(); }
private:
	vector<queue<T>> A; ui B;
};


#endif


template<typename T> struct Fenwick {
	explicit Fenwick(size_t N, T t=T()):F(N,t),N(N),t(t){}
	explicit Fenwick(const vector<T>&A, T t=T()):F(A),N(A.size()),t(t){
		for(int i=0;i<N;i++){int j=i+lsb(i+1);if(j<N)F[j]+=F[i];}}
	void add(int i, T v){while(i<=N){F[i]+=v;i+=lsb(i+1);}}
	T sum(int i)const{T sum(t);while(i){sum+=F[i-1];i-=lsb(i);}return sum;}
	T range(int i,int j)const{T s(t);while(j>i){s+=F[j-1];j-=lsb(j);}while(i>j){s-=F[i-1];i-=lsb(i);}return s;}
	T get(int i)const{return range(i,i+1);}
	constexpr int lsb(int i)const{return i&-i;}
	vector<T> F;size_t N;T t;
};

template<typename F>struct NoCombine {
    const int NOCOMB=-2;
    void setup(size_t){}void op(F&p,F n,size_t,size_t,size_t){p=n;}F up(F l,F r,size_t){return l==r?l:NOCOMB;}
    void down(F&u,F&l,F&r,size_t,size_t,size_t){if(u!=NOCOMB){l=r=u;}u=NOCOMB;}};
template<typename F,typename Op>struct Persistent{Op o;void setup(size_t){}void op(F&p,F n,size_t,size_t, size_t){p=n;}F up(F l, F r, size_t){return o(l,r);}void down(F&,F&,F&,size_t,size_t, size_t b) {}};

template<typename F,typename SetOp,typename CombineOp,typename PowerOp>struct LazyProp{
    void setup(size_t s){this->s=s;L=new F[s]();}
    void down(F&u,F&l,F&r,size_t i,size_t a,size_t b){op(l,L[i],2*i,a,(a+b)/2);op(r,L[i],2*i+1,(a+b)/2,b);L[i]=0;}
    void op(F&p,F n,size_t i,size_t a,size_t b){p=sop(p,pop(n,b-a));if(i<this->s)this->L[i]=sop(this->L[i],n);}
    F up(F l,F r,size_t){return cop(l,r);}SetOp sop;CombineOp cop;PowerOp pop;F*L;size_t s;
};

template <typename F> struct AddOp { F operator()(F a, F b) { return a+b; }};
template <typename F> struct MultOp { F operator()(F a, F b) { return a*b; }};
template <typename F> struct IdempOp { F operator()(F a, F b) { return a; }};
template <typename F> struct MinOp { F operator()(F a, F b) { return std::min(a,b); }};
template <typename F> struct MaxOp { F operator()(F a, F b) { return std::max(a,b); }};

template <typename F, typename Combine> struct SegTree {
	void setup(size_t s, F def) {
		size = 1<<logceil(s);
		T = vector<F>(2*size, def);
		for (size_t i = size-1; i > 0; i--) T[i] = op.up(T[2*i],T[2*i+1], i);
		op.setup(2*size);
	}

	void setup(vector<F> & data, F def = F()) {
		size = 1<<logceil(data.size());
		T = vector<F>(2*size, def);
		copy(data.begin(), data.end(), T.begin() + size);
		for (size_t i = size-1; i > 0; i--) T[i] = op.up(T[2*i],T[2*i+1], i);
		op.setup(2*size);
	}

	inline void put(size_t x, F n) { put(x, x, n); }
	inline void put(size_t from, size_t to, F n) { put2(from, to+1, n, 1, 0, size); }
	inline F get(size_t x) { return get(x, x); }
	inline F get(size_t from, size_t to) { return get2(from, to+1, 1, 0, size); }

	void put2(size_t from, size_t to, F n, size_t i, size_t a, size_t b) {
		if (from <= a && to >= b) { op.op(T[i], n, i, a, b); return; }
		if (from >= b || to <= a) { return; }
		op.down(T[i], T[2*i], T[2*i+1], i, a, b);
		put2(from, to, n, 2*i, a, (a+b)/2);
		put2(from, to, n, 2*i+1, (a+b)/2, b);
		T[i] = op.up(T[2*i], T[2*i+1], i);
	}

	F get2(size_t from, size_t to, size_t i, size_t a, size_t b) {
		if (from <= a && to >= b) { return T[i]; }
		op.down(T[i], T[2*i], T[2*i+1], i, a, b);
		if (to <= (a+b)/2) { return get2(from, to, 2*i, a, (a+b)/2); }
		else if (from >= (a+b)/2) { return get2(from, to, 2*i+1, (a+b)/2, b); }
		else return op.up(get2(from, to, 2*i, a, (a+b)/2), get2(from, to, 2*i+1, (a+b)/2, b), i);
	}

	size_t size;
	vector<F> T;
	Combine op;
};

template<typename T> using SetGetTree = SegTree<T, NoCombine<T>>;
template<typename T> using AddSumTree = SegTree<T, LazyProp<T, AddOp<T>, AddOp<T>, MultOp<T>>>;
template<typename T> using AddMaxTree = SegTree<T, LazyProp<T, AddOp<T>, MaxOp<T>, IdempOp<T>>>;
template<typename T> using AddMinTree = SegTree<T, LazyProp<T, AddOp<T>, MinOp<T>, IdempOp<T>>>;
template<typename T> using SetMinTree = SegTree<T, Persistent<T, MinOp<T>>>;
template<typename T> using SetMaxTree = SegTree<T, Persistent<T, MaxOp<T>>>;

// #include "../l/mod.h"

class E {
public:
	struct group {
		int l, r;
		int a, b;
		bool operator<(const group&g) const {
			return l < g.l;
		}
	};

	vector<int> I;
	SetMinTree<int> T[2];
	minheap<pair<int, group>> H;

	void addgroup(int l, int r) {
		if (l>r) {
			return;
		}
		int len = (r-l+1)/2;
		int lowest = T[l%2].get(l/2, l/2+len-1);
		int lowestId = I[lowest];
		int second = T[(l+1)%2].get((lowestId+1)/2,r/2);
		H.push({lowest, {l, r, lowest, second}});

	}

	void solve(istream& cin, ostream& cout) {
		int N; cin >> N;
		vector<int> P(N); cin >> P;
		vector<int> Podd(N/2), Peven(N/2);
		I.resize(N);
		for (int i = 0; i < N; ++i) {
			P[i]--;
			I[P[i]] = i;
			if (i%2) {
				Podd[i/2] = P[i];
			} else {
				Peven[i/2] = P[i];
			}
  		}

		T[1].setup(Podd);
		T[0].setup(Peven);


		addgroup(0, N-1);

		for (int i = 0; i < N/2; ++i) {
			auto x = H.top();
			H.pop();
			cout << x.y.a+1 << ' ' << x.y.b+1 << ' ';
			addgroup(x.y.l, I[x.y.a]-1);
			addgroup(I[x.y.a]+1, I[x.y.b]-1);
			addgroup(I[x.y.b]+1, x.y.r);

		}
		cout << endl;

    }
};


int main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
	E solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
    return 0;
}


Measured memory_bytes: 7672