You are given a rooted undirected tree consisting of $n$ vertices. Vertex $1$ is the root.

Let's denote a depth array of vertex $x$ as an infinite sequence $[d_{x, 0}, d_{x, 1}, d_{x, 2}, \dots]$, where $d_{x, i}$ is the number of vertices $y$ such that both conditions hold:
 -  $x$ is an ancestor of $y$; -  the simple path from $x$ to $y$ traverses exactly $i$ edges. 
The dominant index of a depth array of vertex $x$ (or, shortly, the dominant index of vertex $x$) is an index $j$ such that:
 -  for every $k &lt; j$, $d_{x, k} &lt; d_{x, j}$; -  for every $k &gt; j$, $d_{x, k} \le d_{x, j}$. 
For every vertex in the tree calculate its dominant index.
The first line contains one integer $n$ ($1 \le n \le 10^6$) — the number of vertices in a tree.

Then $n - 1$ lines follow, each containing two integers $x$ and $y$ ($1 \le x, y \le n$, $x \ne y$). This line denotes an edge of the tree.

It is guaranteed that these edges form a tree.
4
1 2
2 3
3 4
0
0
0
0
4
1 2
1 3
1 4
1
0
0
0
4
1 2
2 3
2 4
2
1
0
0
1
0
2
1 2
0
0
from collections import deque

def makegraph(links):
  ls = {}
  for l1, l2 in links:
    # print(ls)
    # print(ls.get(l1, []))
    ls.setdefault(l1, []).append(l2)
    ls.setdefault(l2, []).append(l1)

  # print(ls)
  g = {}
  ig = {}
  leaves = []
  todo = deque([(-1, 1)])
  while len(todo) > 0:
    pp, p = todo.popleft()
    ll = ls[p]
    c = []
    for l in ll:
      if l == pp:
        continue
      c.append(l)
      ig[l] = p
      todo.append((p, l))
    if len(c) == 0:
      leaves.append(p)
    else:
      g[p] = c
    # print(p)
    # print(ll)
    # print(g)
    # print(ig)
  return g, ig, leaves

def combine(childs):
  if len(childs) == 0:
    return [1], 0

  mlen = max([len(a) for a in childs])
  res = [0 for a in range(mlen)]
  for c in childs:
    for a in range(len(c)):
      res[mlen-a-1] += c[-1-a]

  res.append(1)
  lm = 1
  li = 0
  for a in range(mlen-1, -1, -1):
    if res[a] > lm:
      lm = res[a]
      li = mlen - a
  return res, li

def calcgraph(num, links):
  g, ig, leaves = makegraph(links)
  # print(g, ig, leaves)

  ans = list([0 for a in range(num)])

  child = {}
  cscratch = {}

  todo = deque(leaves)

  while len(todo) > 0:
    n = todo.popleft()
    res, dom = combine(cscratch.get(n, []))
    ans[n-1] = dom
    if n > 1:
      p = ig[n]
      new = cscratch.setdefault(p, [])
      new.append(res)
      if len(new) == len(g[p]):
        todo.append(p)


  return '\n'.join([str(a) for a in ans])

n = int(input())
links = []
for a in range(n-1):
  links.append(list(map(int, input().split())))
print(calcgraph(n, links))
