import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
    std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
    std.typecons, std.container, std.format, std.datetime;

// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on

void main()
{
    long N, M;
    scan(N, M);
    auto children = new long[][](N);
    foreach (_; 0 .. N - 1)
    {
        long x, y;
        scan(x, y);
        x--, y--;
        children[x] ~= y;
        children[y] ~= x;
    }
    auto dp = new BigInt[](N);
    void dfs1(long p, long v)
    {
        // writefln("%s %s", p, v);
        foreach (c; children[v])
            if (c != p)
                dfs1(v, c);
        dp[v] = 1;
        foreach (c; children[v])
            if (c != p)
                dp[v] *= dp[c] + 1;
    }

    dfs1(-1, 0);
    // writeln(dp);

    void dfs2(long p, long v)
    {
        if (p != -1)
        {
            dp[v] = dp[v] * ((dp[p] / (dp[v] + 1)) + 1);
            // writeln(dp);

        }
        foreach (c; children[v])
            if (c != p)
                dfs2(v, c);
    }

    dfs2(-1, 0);
    foreach (d; dp)
        writeln(d % M);
}
