#include #include using mint = atcoder::modint998244353; //using mint = atcoder::modint1000000007; using namespace atcoder; using namespace std; using ll = long long; using pi = pair; using pl = pair; using vi = vector; using vm = vector; using vvi = vector; using vl = vector; using vvl = vector; using vpi = vector>; using vpl = vector>; #define rep(i,n) for (ll i = 0; i < n; i++) #define replr(i,l,r) for (ll i = l; i < r; i++) set abc = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; const int inf =1e9; const ll infl = 4e18; template bool chmax(T &a, const T& b) {if (a < b) {a = b; return true;}return false;} template bool chmin(T &a, const T& b) {if (a > b) {a = b; return true;}return false;} template T max(vector &v) {T m = 0;for (auto x : v) chmax(m, x);return m;} template T min(vector &v) {T m = inf;for (auto x : v) chmin(m, x);return m;} template void print(vector &v){for(auto x : v){cout<> N; vi p(N);rep(i,N) cin >> p[i]; vi pos(N); vi nex(N,-1); vi pre(N,-1); rep(i,N-1) nex[i] =i+1; replr(i,1,N) pre[i] = i-1; rep(i,N) pos[p[i]-1] = i; vi q; rep(i,N){ int x=pos[i]; if (p[x]==-1) continue; if (nex[x]==-1) continue; q.push_back(p[x]); q.push_back(p[nex[x]]); if (pre[x]!=-1) nex[pre[x]]=nex[nex[x]]; if (nex[nex[x]]!=-1) pre[nex[nex[x]]]=pre[x]; p[x]=-1; p[nex[x]]=-1; } print(q); }