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;
const int N = 1e5 + 10;
const int inf = 1e9;
int n;
int fa[N];
struct edge {
  int to, Next;
} ed[N];
int edcnt, head[N];
void addedge(int u, int v) {
  ed[++edcnt] = (edge){v, head[u]};
  head[u] = edcnt;
}
int sz[N], son[N], mn[N], mx2[N];
bool can[N];
void dfsson(int u) {
  sz[u] = 1;
  son[u] = 0;
  mn[u] = inf;
  for (int i = head[u]; i; i = ed[i].Next) {
    int v = ed[i].to;
    dfsson(v);
    sz[u] += sz[v];
    if (sz[v] > sz[son[u]]) {
      mx2[u] = sz[son[u]];
      son[u] = v;
      can[u] = true;
    } else if (sz[v] == sz[son[u]])
      can[u] = false;
    else if (sz[v] > mx2[u])
      mx2[u] = sz[v];
    if (sz[v] < mn[u]) mn[u] = sz[v];
  }
  if (fa[u]) {
    if (n - sz[u] > sz[son[u]]) {
      mx2[u] = sz[son[u]];
      son[u] = 0;
      can[u] = true;
    } else if (n - sz[u] == sz[son[u]])
      can[u] = false;
    else if (n - sz[u] > mx2[u])
      mx2[u] = n - sz[u];
    if (n - sz[u] < mn[u]) mn[u] = n - sz[u];
  }
  if (can[u]) {
    if ((!son[u] && n - sz[u] == mn[u]) || (son[u] && sz[son[u]] == mn[u]))
      can[u] = false;
  }
}
struct segtree {
  int ndcnt, root[N];
  int sum[N * 25], ls[N * 25], rs[N * 25];
  void change(int l, int r, int &x, int Pos, int Val) {
    if (!x) x = ++ndcnt;
    sum[x] += Val;
    if (l == r) return;
    int mid = (l + r) >> 1;
    if (Pos <= mid)
      change(l, mid, ls[x], Pos, Val);
    else
      change(mid + 1, r, rs[x], Pos, Val);
  }
  void change(int x, int Pos, int Val) { change(1, n, root[x], Pos, Val); }
  int querypre1(int l, int r, int x, int Pos) {
    if (!sum[x]) return n + 1;
    if (l == r) return l;
    int mid = (l + r) >> 1;
    if (Pos >= r) {
      if (sum[rs[x]])
        return querypre1(mid + 1, r, rs[x], Pos);
      else
        return querypre1(l, mid, ls[x], Pos);
    }
    if (Pos > mid) {
      int ret = querypre1(mid + 1, r, rs[x], Pos);
      if (ret != n + 1) return ret;
    }
    return querypre1(l, mid, ls[x], Pos);
  }
  int querypre1(int x, int Pos) {
    if (Pos < 1) return n + 1;
    return querypre1(1, n, root[x], Pos);
  }
  int querynxt1(int l, int r, int x, int Pos) {
    if (!sum[x]) return 0;
    if (l == r) return l;
    int mid = (l + r) >> 1;
    if (Pos <= l) {
      if (sum[ls[x]])
        return querynxt1(l, mid, ls[x], Pos);
      else
        return querynxt1(mid + 1, r, rs[x], Pos);
    }
    if (Pos <= mid) {
      int ret = querynxt1(l, mid, ls[x], Pos);
      if (ret) return ret;
    }
    return querynxt1(mid + 1, r, rs[x], Pos);
  }
  int querynxt1(int x, int Pos) {
    if (Pos > n) return 0;
    return querynxt1(1, n, root[x], Pos);
  }
  int querypre2(int l, int r, int x, int y, int z, int Pos) {
    if (!(sum[x] - sum[y] - sum[z])) return n + 1;
    if (l == r) return l;
    int mid = (l + r) >> 1;
    if (Pos >= r) {
      if (sum[rs[x]] - sum[rs[y]] - sum[rs[z]])
        return querypre2(mid + 1, r, rs[x], rs[y], rs[z], Pos);
      else
        return querypre2(l, mid, ls[x], ls[y], ls[z], Pos);
    }
    if (Pos > mid) {
      int ret = querypre2(mid + 1, r, rs[x], rs[y], rs[z], Pos);
      if (ret != n + 1) return ret;
    }
    return querypre2(l, mid, ls[x], ls[y], ls[z], Pos);
  }
  int querypre2(int x, int Pos) {
    if (Pos < 1) return n + 1;
    return querypre2(1, n, root[0], root[n + 1], root[x], Pos);
  }
  int querynxt2(int l, int r, int x, int y, int z, int Pos) {
    if (!(sum[x] - sum[y] - sum[z])) return 0;
    if (l == r) return l;
    int mid = (l + r) >> 1;
    if (Pos <= l) {
      if (sum[ls[x]] - sum[ls[y]] - sum[ls[z]])
        return querynxt2(l, mid, ls[x], ls[y], ls[z], Pos);
      else
        return querynxt2(mid + 1, r, rs[x], rs[y], rs[z], Pos);
    }
    if (Pos <= mid) {
      int ret = querynxt2(l, mid, ls[x], ls[y], ls[z], Pos);
      if (ret) return ret;
    }
    return querynxt2(mid + 1, r, rs[x], rs[y], rs[z], Pos);
  }
  int querynxt2(int x, int Pos) {
    if (Pos > n) return 0;
    return querynxt2(1, n, root[0], root[n + 1], root[x], Pos);
  }
  void merge(int l, int r, int &x, int y) {
    if (!x || !y) {
      x = x + y;
      return;
    }
    sum[x] = sum[x] + sum[y];
    if (l == r) return;
    int mid = (l + r) >> 1;
    merge(l, mid, ls[x], ls[y]);
    merge(mid + 1, r, rs[x], rs[y]);
  }
  void merge(int x, int y) { merge(1, n, root[x], root[y]); }
} Sg;
int ans[N];
void dfssolve(int u) {
  bool flag = false;
  if (fa[u]) Sg.change(n + 1, sz[fa[u]], 1);
  for (int i = head[u]; i; i = ed[i].Next) dfssolve(ed[i].to);
  if (can[u]) {
    if (son[u]) {
      ans[u] = sz[son[u]];
      int ret = Sg.querypre1(son[u], (sz[son[u]] - mn[u]) / 2);
      if (ret != n + 1)
        ans[u] = (ans[u] < sz[son[u]] - ret ? ans[u] : sz[son[u]] - ret);
      ret = Sg.querynxt1(son[u], (sz[son[u]] - mn[u] + 1) / 2);
      if (ret) ans[u] = (ans[u] < mn[u] + ret ? ans[u] : mn[u] + ret);
    } else {
      ans[u] = n - sz[u];
      int ret = Sg.querypre1(n + 1, (n - sz[u] - mn[u]) / 2 + sz[u]) - sz[u];
      if (ret != n + 1 - sz[u])
        ans[u] = (ans[u] < n - sz[u] - ret ? ans[u] : n - sz[u] - ret);
      ret = Sg.querynxt1(n + 1, (n - sz[u] - mn[u] + 1) / 2 + sz[u]) - sz[u];
      if (ret != -sz[u]) ans[u] = (ans[u] < mn[u] + ret ? ans[u] : mn[u] + ret);
      flag = true;
      Sg.change(u, sz[u], 1);
      for (int i = head[u]; i; i = ed[i].Next) Sg.merge(u, ed[i].to);
      ret = Sg.querypre2(u, (n - sz[u] - mn[u]) / 2);
      if (ret != n + 1)
        ans[u] = (ans[u] < n - sz[u] - ret ? ans[u] : n - sz[u] - ret);
      ret = Sg.querynxt2(u, (n - sz[u] - mn[u] + 1) / 2);
      if (ret) ans[u] = (ans[u] < mn[u] + ret ? ans[u] : mn[u] + ret);
    }
    ans[u] = (ans[u] > mx2[u] ? ans[u] : mx2[u]);
  } else {
    if (son[u])
      ans[u] = sz[son[u]];
    else
      ans[u] = n - sz[u];
  }
  if (fa[u]) Sg.change(n + 1, sz[fa[u]], -1);
  if (!flag) {
    Sg.change(u, sz[u], 1);
    for (int i = head[u]; i; i = ed[i].Next) Sg.merge(u, ed[i].to);
  }
}
int main() {
  scanf("%d", &n);
  int root;
  for (int i = 1; i <= n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    if (u) {
      fa[v] = u;
      addedge(u, v);
    } else
      root = v;
  }
  dfsson(root);
  for (int i = 1; i <= n; i++) Sg.change(0, sz[i], 1);
  dfssolve(root);
  for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
  return 0;
}
