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, num):
  # ls = {}
  ls = [[] for a in range(num+1)]
  for l1, l2 in links:
    # print(ls)
    # print(ls.get(l1, []))
    # ls.setdefault(l1, []).append(l2)
    # ls.setdefault(l2, []).append(l1)
    ls[l1].append(l2)
    ls[l2].append(l1)

  ig = [0 for a in range(num+1)]
  order = [0 for a in range(num)]

  order[0] = (1, -1)
  ig[1] = -1
  cur = 1

  ll = ls[1]
  for l in ll:
    order[cur] = (l, 1)
    ig[l] = 1
    cur += 1

  for a in range(1, num):
    c, p = order[a]
    ll = ls[c]
    ll.remove(p)
    for l in ll:
      order[cur] = (l, c)
      ig[l] = c
      cur += 1

  return ls, order, ig



def calcgraph(num, links):
  # ss = time.clock()
  ls, order, ig = makegraph(links, num)
  
  # print(time.clock() - ss)
  # print(g, ig, leaves)

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

  cscratch = {}
  csp = {}

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

    mlen = max([len(a) for a in childs])
    newl = None
    for a in range(len(childs)):
      if mlen == len(childs[a]):
        newl = childs[a]
        childs[a] = []
        break
    mlen2 = max([len(a) for a in childs])

    res = newl
    for c in childs:
      if c is None:
        continue
      for a in range(len(c)):
        res[mlen-a-1] += c[-1-a]

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


  for ii in range(num-1, -1, -1):
    n, p = order[ii]
    ll = ls[n]
    toc = []
    for l in ll:
      toc.append(cscratch[l-1])

    if len(toc) == 0:
      res = [1]
      dom = 0
      domv = 1
    elif len(toc) == 1:
      res = toc[0]
      res.append(1)
      dom = ans[ll[0]-1]
      if dom > 0:
        dom += 1
      domv = ansv[ll[0]-1]
    else:
      res, dom, domv = combine(toc, n)
    ans[n-1] = dom
    ansv[n-1] = domv
    cscratch[n-1] = res

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


# import random

# random.seed(123123)
# num = 1000000
# potentials = list(range(2, num+1))
# random.shuffle(potentials)
# done = [1]
# links = []
# for a in potentials:
#   b = random.randint(0, len(done)-1)
#   links.append((a, done[b]))
#   done.append(a)
# random.shuffle(links)

# import time

# start = time.clock()
# calcgraph(num, links)
# print(time.clock() - start)


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