#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
//------------------------------------------------------------------------------
int MOD = 10;
struct ModInt {
    unsigned x; ModInt() : x(0) { }
    ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    int get() const { return (int)x; }
    ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
    ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
    ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
    ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
    ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
    ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
    ModInt inverse() const {
        long long a = x, b = MOD, u = 1, v = 0;
        while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); }
        return ModInt(u);
    }
    bool operator==(ModInt that) const { return x == that.x; }
    bool operator!=(ModInt that) const { return x != that.x; }
    ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : MOD - x; return t; }
};
typedef ModInt mint;
//------------------------------------------------------------------------------
int N;
vector<vector<int>> G;

mint dp[100010];

void dfs1(int curr, int prev = -1)
{
    dp[curr] = 1;
    for (const int next : G[curr])
    {
        if (next == prev) continue;
        dfs1(next, curr);
        dp[curr] *= dp[next] + 1;
    }
}

mint ans[100010];

void dfs2(int curr, int prev = -1)
{
    ans[curr] = 1;
    for (const int next : G[curr]) ans[curr] *= dp[next] + 1;

    const int n = G[curr].size();
    vector<mint> l(n), r(n);
    rep(i, 0, n) l[i] = r[i] = dp[G[curr][i]] + 1;
    rep(i, 0, n - 1) l[i + 1] *= l[i];
    rrep(i, n - 1, 1) r[i - 1] *= r[i];

    rep(i, 0, n)
    {
        const int next = G[curr][i];
        if (next == prev) continue;
        dp[curr] = 1;
        if (i - 1 >= 0) dp[curr] *= l[i - 1];
        if (i + 1 < n) dp[curr] *= r[i + 1];
        dfs2(next, curr);
    }
}

void _main()
{
    cin >> N >> MOD;
    G.resize(N);
    rep(i, 0, N - 1)
    {
        int x, y;
        cin >> x >> y;
        --x; --y;
        G[x].push_back(y);
        G[y].push_back(x);
    }

    dfs1(0);
    dfs2(0);

    rep(i, 0, N) cout << ans[i].get() << endl;
}


Measured memory_bytes: 18688