#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define N 3000000
int n,root,a[N],b[N],last[N],head[N],nxt[N],fa[N],q[N],pos[N],sum[N];
int getint(){
char ch; int sum=0;
for (ch=getchar();ch<'0' || ch>'9';ch=getchar());
for (;ch>='0' && ch<='9';ch=getchar()) sum=sum*10+ch-'0';
return sum;
}
void add(int x,int y){nxt[y]=head[x]; head[x]=y;}
int getlast(int x){return x==last[x]?x:getlast(last[x]);}
int main(){
n=getint();
for (int i=1;i<=n;i++) last[i]=i;
for (int i=1;i<=n;i++){
fa[i]=getint(); a[i]=getint();
if (a[i]){b[i]=a[i]; last[a[i]]=a[i]-1;}
if (fa[i]!=i) add(fa[i],i); else{root=i; fa[root]=0;}
}
int tt=0,ww=1; q[1]=root; a[0]=n+1;
while (tt<ww){
int u=q[++tt];
if (!a[u]){a[u]=getlast(a[fa[u]]-1); pos[a[u]]=u; sum[a[u]]++;}
for (int i=head[u];i;i=nxt[i]) q[++ww]=i;
}
int tmp=0;
for (int i=1;i<=n;i++){
if (last[i]==i) tmp++;
if (sum[i] && tmp==1) b[pos[i]]=i;
tmp-=sum[i];
}
for (int i=1;i<=n;i++) printf("%d\n",b[i]);
return 0;
}