output_description: Print N lines. The v-th (1 \leq v \leq N) line should contain the answer to
  the following question:
  
    * Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N M
      x_1 y_1
      x_2 y_2
      :
      x_{N - 1} y_{N - 1}
attempt: """
  N頂点の木があり、i番目の辺は頂点x_iとy_iを結んでいる。
  各頂点を白または黒で塗り、どの黒い頂点からどの黒い頂点へも黒い頂点のみをたどって到達できる時、
  頂点vが黒であるような頂点の色の組み合わせは何通りか？Mで割ったあまりを求めよ。
  graph_i
  input:
  3 100
  1 2
  2 3
  output:
      3
      4
      3
  
  """
  
  # region header
  import sys, bisect, math, itertools, heapq, collections
  from operator import itemgetter
  
  # a.sort(key=itemgetter(i)) # i番目要素でsort
  from functools import lru_cache
  import copy
  
  # @lru_cache(maxsize=None)
  # sys.setrecursionlimit(10**8)
  input = sys.stdin.readline
  INF = float("inf")
  mod = 10**9 + 7
  eps = 10**-7
  # endregion
  # region input function
  
  
  def inp():
      """
      一つの整数
      """
      return int(input())
  
  
  def inpl():
      """
      一行に複数の整数
      """
      return list(map(int, input().split()))
  
  
  def str_inp():
      """
      文字列をリストとして読み込む
      """
      return list(input()[:-1])
  
  
  # endregion
  
  # setting
  unit = 1  # 単位元
  
  
  def merge(a, b):
      # モノイド演算
      return a * b % M
  
  
  def adj_bu(a, i):
      # bottom up DP
      return a + 1
  
  
  def adj_td(a, i, p):
      # top down DP
      return a + 1
  
  
  def adj_fin(a, i):
      # 最終結果
      return a
  
  
  #####
  
  
  n, M = inpl()
  graph = [[] for _ in range(n)]
  for i in range(n - 1):
      a, b = inpl()
      graph[a - 1].append(b - 1)
      graph[b - 1].append(a - 1)
  parent = [-1] * n
  root = 0  # 0を根とする
  queue = collections.deque([root])
  sorted_graph = []  # トポロジカルソート
  
  # トポロジカルソート
  while queue:
      i = queue.popleft()
      sorted_graph.append(i)
      for a in graph[i]:
          if a != parent[i]:
              parent[a] = i
              graph[a].remove(i)  # graph：隣接リストから子リストにする
              queue.append(a)
  
  # Bottom up DP
  
  BU = [unit] * n  # acc[i]と対応
  res = [0] * n
  for i in sorted_graph[1:][::-1]:
      res[i] = adj_bu(BU[i], i)
      p = parent[i]
      BU[p] = merge(BU[p], res[i])
  res[sorted_graph[0]] = adj_fin(BU[sorted_graph[0]], sorted_graph[0])  # 根だけ別で
  
  
  # Top down DP
  
  TD = [unit] * n
  for i in sorted_graph:
      # 左からDP
      tmp = TD[i]
      for j in graph[i]:
          TD[j] = tmp
          tmp = merge(tmp, res[j])
      # 右からDP
      tmp = unit
      for j in graph[i][::-1]:
          TD[j] = adj_td(merge(TD[j], tmp), j, i)
          tmp = merge(tmp, res[j])
          res[j] = adj_fin(merge(BU[j], TD[j]), j)
  print(*res, sep="\n")
  """
  各頂点に対して結果を出力するので全方位木を考える。
  全方位木では子→親の木DPと親→子の木DPを使う。
  acc[i][p]:ノードiにおける親pを除いた全方向からの累積
  res[i][p]:acc[i][p]に調整を加えたもの
  acc[i]:ノードiにおけるすべての方向からの集約
  """
  
problem_description: Statement
  
  There is a tree with N vertices, numbered 1, 2, \ldots, N. For each i (1 \leq
  i \leq N - 1), the i-th edge connects Vertex x_i and y_i.
  
  Taro has decided to paint each vertex in white or black, so that any black
  vertex can be reached from any other black vertex by passing through only
  black vertices.
  
  You are given a positive integer M. For each v (1 \leq v \leq N), answer the
  following question:
  
    * Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M.
samples: [{"input": "3 100\n    1 2\n    2 3", "output": "3\n    4\n    3\n    \n\nThere are seven ways to paint the vertices, as shown in the figure below.\nAmong them, there are three ways such that Vertex 1 is black, four ways such\nthat Vertex 2 is black and three ways such that Vertex 3 is black.\n\n![](https://img.atcoder.jp/dp/subtree_0_muffet.png)\n\n* * *"}, {"input": "4 100\n    1 2\n    1 3\n    1 4", "output": "8\n    5\n    5\n    5\n    \n\n* * *"}, {"input": "1 100", "output": "1\n    \n\n* * *"}, {"input": "10 2\n    8 5\n    10 8\n    6 5\n    1 5\n    4 8\n    2 10\n    3 6\n    9 2\n    1 7", "output": "0\n    0\n    1\n    1\n    1\n    0\n    1\n    0\n    1\n    1\n    \n\nBe sure to print the answers modulo M."}]