#include using namespace std; #define rep(i,n) for(int i=0;i<(int)(n);i++) int main() { int N; cin>>N; vector p(N); rep(i,N) cin>>p.at(i); rep(i,N) p.at(i)--; vector rp(N); rep(i,N) rp.at(p.at(i))=i; vector nx(N); // 要素iの次の要素 rep(i,N){ if(rp.at(i)==N-1) nx.at(i)=-1; else nx.at(i)=p.at(rp.at(i)+1); } vector stl(N,0); auto get_next=[&](auto self,int x){ if(x==-1||!stl.at(x)) return x; return nx.at(x)=self(self,nx.at(x)); }; vector ans; rep(i,N){ if(stl.at(i)) continue; stl.at(i)=1; int j=get_next(get_next,i); if(j==-1){ stl.at(i)=0; continue; } stl.at(j)=1; ans.push_back(i); ans.push_back(j); } for(int x:ans) cout<