Given a rooted tree with n nodes. The Night King removes exactly one node from the tree and all the edges associated with it. Doing this splits the tree and forms a forest. The node which is removed is not a part of the forest.

The root of a tree in the forest is the node in that tree which does not have a parent. We define the strength of the forest as the size of largest tree in forest.

Jon Snow wants to minimize the strength of the forest. To do this he can perform the following operation at most once.

He removes the edge between a node and its parent and inserts a new edge between this node and any other node in forest such that the total number of trees in forest remain same.

For each node v you need to find the minimum value of strength of the forest formed when node v is removed.

Input

The first line of the input contains an integer n (1 ≤ n ≤ 105) — the number of vertices in the tree. Each of the next n lines contains a pair of vertex indices ui and vi (1 ≤ ui, vi ≤ n) where ui is the parent of vi. If ui = 0 then vi is the root.

Output

Print n line each containing a single integer. The i-th of them should be equal to minimum value of strength of forest formed when i-th node is removed and Jon Snow performs the operation described above at most once.

Examples

Input

10
0 1
1 2
1 3
1 4
2 5
2 6
3 7
4 8
4 9
5 10


Output

3
4
5
5
5
9
9
9
9
9


Input

2
2 1
0 2


Output

1
1

Note

The tree for first test case is depicted below. <image> When you remove the first node, the tree splits to form the following forest. The strength of this forest is 4. <image> Jon Snow now changes the parent of vertex 10 from 5 to 3. The strength of forest now becomes 3. <image>
#include <bits/stdc++.h>
using namespace std;
struct edge {
  int to, nxxt;
} e[100102 << 1];
struct node {
  int l, r;
} b[100102];
int n, head[100102], cnt = 1, fa[100102], rot, dep[100102], rt[100102],
                     ls[100102 * 52], rs[100102 * 52], he[100102 * 52], js;
int siz[100102], son[100102], dfn[100102], rev[100102], tot[100102], cn,
    ans[100102], tt, duu[100102];
multiset<int> s;
multiset<int>::iterator it;
bool cmp(node a, node b) {
  if (a.l == b.l) return a.r < b.r;
  return a.l < b.l;
}
inline void ins(int u, int v) {
  e[cnt].to = v;
  e[cnt].nxxt = head[u];
  head[u] = cnt++;
}
void chng(int &o, int ind, int l, int r, int x, int y) {
  o = ++js;
  ls[o] = ls[ind], rs[o] = rs[ind], he[o] = he[ind] + y;
  if (l == r) return;
  int mid = (l + r) >> 1;
  if (x <= mid)
    chng(ls[o], ls[ind], l, mid, x, y);
  else
    chng(rs[o], rs[ind], mid + 1, r, x, y);
}
int askhe(int id1, int id2, int l, int r, int al, int ar) {
  if (al > ar) return 0;
  if (al <= l && r <= ar) return he[id2] - he[id1];
  int mid = (l + r) >> 1, ret = 0;
  if (al <= mid) ret += askhe(ls[id1], ls[id2], l, mid, al, ar);
  if (ar > mid) ret += askhe(rs[id1], rs[id2], mid + 1, r, al, ar);
  return ret;
}
int askth(int id1, int id2, int l, int r, int k) {
  if (l == r) return l;
  int mid = (l + r) >> 1, tva = he[ls[id2]] - he[ls[id1]];
  if (k <= tva)
    return askth(ls[id1], ls[id2], l, mid, k);
  else
    return askth(rs[id1], rs[id2], mid + 1, r, k - tva);
}
void df5(int te) {
  siz[te] = 1;
  for (int i = head[te]; i; i = e[i].nxxt) {
    int j = e[i].to;
    dep[j] = te;
    df5(j);
    siz[te] += siz[j];
    if (siz[j] > siz[son[te]]) son[te] = j;
  }
}
void dfs(int te) {
  if (te == son[fa[te]])
    tot[te] = tot[fa[te]];
  else
    tot[te] = te;
  dfn[te] = ++cn, rev[cn] = te;
  if (son[te]) dfs(son[te]);
  for (int i = head[te]; i; i = e[i].nxxt)
    if (e[i].to ^ son[te]) dfs(e[i].to);
}
inline int askpre(int l, int r, int x) {
  if (askhe(rt[l], rt[r], 1, n, x, x)) return x;
  int su = askhe(rt[l], rt[r], 1, n, 1, x), mn = 1e9, ret;
  if (he[rt[r]] - he[rt[l]] != su) {
    int ps = askth(rt[l], rt[r], 1, n, su + 1);
    if (abs(ps - x) < mn) mn = abs(ps - x), ret = ps;
  }
  if (su) {
    int ps = askth(rt[l], rt[r], 1, n, su);
    if (abs(ps - x) < mn) mn = abs(ps - x), ret = ps;
  }
  return ret;
}
void clfs(int te) {
  int mxs = 0, mxw, mns = 1e9, mnw, ses = 0, sew;
  if (te ^ rot) {
    mxs = mns = n - siz[te], mxw = mnw = fa[te];
  }
  for (int i = head[te]; i; i = e[i].nxxt) {
    int j = e[i].to;
    if (siz[j] > mxs)
      ses = mxs, sew = mxw, mxs = siz[j], mxw = j;
    else if (siz[j] > ses)
      ses = siz[j], sew = j;
    if (siz[j] < mns) mns = siz[j], mnw = j;
  }
  if (duu[te] == 1)
    ans[te] = n - 1;
  else if (mxs - mns <= 1)
    ans[te] = mxs;
  else {
    if (mxw == fa[te]) {
      ans[te] = 1e9;
      tt = 0;
      int x = te;
      b[++tt] = (node){dfn[te], dfn[te] + siz[te] - 1};
      while (x) {
        b[++tt] = (node){dfn[tot[x]], dfn[x]};
        x = fa[tot[x]];
      }
      sort(b + 1, b + 1 + tt, cmp);
      if (b[1].l > 1) {
        int cj = (mxs - mns) / 2;
        int tv = askpre(0, b[1].l - 1, cj);
        ans[te] = min(ans[te], max(ses, max(mxs - tv, mns + tv)));
      }
      for (int i = 1; i < tt; i++)
        if (b[i].r + 1 < b[i + 1].l) {
          int cj = (mxs - mns) / 2;
          int tv = askpre(b[i].r, b[i + 1].l - 1, cj);
          ans[te] = min(ans[te], max(ses, max(mxs - tv, mns + tv)));
        }
      if (b[tt].r < n) {
        int cj = (mxs - mns) / 2;
        int tv = askpre(b[tt].r, n, cj);
        ans[te] = min(ans[te], max(ses, max(mxs - tv, mns + tv)));
      }
      int cj = (mxs - mns) / 2;
      if (!s.empty()) {
        it = s.lower_bound(siz[te] + cj);
        if ((*it) == siz[te] + cj)
          ans[te] = min(ans[te], max(ses, max(mxs - cj, mns + cj)));
        else {
          if (it != s.end()) {
            int tv = *it;
            ans[te] =
                min(ans[te],
                    max(ses, max(mxs - (tv - siz[te]), mns + (tv - siz[te]))));
          }
          if (it != s.begin()) {
            it--;
            int tv = *it;
            ans[te] =
                min(ans[te],
                    max(ses, max(mxs - (tv - siz[te]), mns + (tv - siz[te]))));
          }
        }
      }
    } else {
      int cj = (mxs - mns) / 2;
      int tv = askpre(dfn[mxw], dfn[mxw] + siz[mxw] - 1, cj);
      ans[te] = max(ses, max(mxs - tv, mns + tv));
    }
  }
  s.insert(siz[te]);
  for (int i = head[te]; i; i = e[i].nxxt) {
    int j = e[i].to;
    clfs(j);
  }
  s.erase(s.lower_bound(siz[te]));
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    if (x == 0)
      rot = y;
    else
      fa[y] = x, ins(x, y), duu[x]++, duu[y]++;
  }
  df5(rot);
  dfs(rot);
  if (n == 1) {
    puts("0");
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    if (rev[i] == rot)
      rt[i] = rt[i - 1];
    else
      chng(rt[i], rt[i - 1], 1, n, siz[rev[i]], 1);
  }
  clfs(rot);
  for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
}
