Rainbow Trees

Count edge colorings of a small tree with k colors so that any two or three consecutive edges on a path get distinct colors, modulo 1e9+9.

Medium6Dynamic programmingTreeCombinatoricsNo attempts yetTime limit5sMemory limit512 MB

Problem

In graph theory a tree is a connected undirected simple graph with no cycles. A tree with nn nodes always has n1n - 1 edges.

A path in a tree is a sequence of distinct edges that are connected: every two consecutive edges of the sequence share a vertex.

You are given a tree with nn vertices and n1n - 1 edges. You can color each edge in one of kk colors.

A coloring of the edges is a rainbow coloring if the edges of every path of 2 edges and of every path of 3 edges all have different colors. That is, any two consecutive edges have different colors, and any three consecutive edges have three different colors.

Given the tree and the number of colors kk, count the rainbow colorings modulo 10000000091000000009.

Input

The first line contains the number of test cases CC. Each of the CC cases follows in this format.

  • One line with two integers nn and kk. Here nn is the number of nodes of the tree and kk is the number of available colors.
  • n1n - 1 lines, one per edge, each with two integers xx and yy, meaning that an edge joins node xx and node yy. Nodes are numbered from 1 to nn.

Limits

  • 1C1001 \le C \le 100
  • 2n202 \le n \le 20
  • 1k10000000001 \le k \le 1000000000
  • Every node number is between 1 and nn, inclusive.

Output

For each test case print one line in the form Case #X: Y, where XX is the 1-based number of the case and YY is the answer for that case.

Hint

In the first sample case the tree has four nodes, and its three edges all meet at one node. Each pair of these edges is adjacent, so a rainbow coloring gives them three different colors, which leaves 10×9×8=72010 \times 9 \times 8 = 720 colorings.

In the second sample case the tree is a path of 4 edges and there are 3 colors. The first three edges must all have different colors, so they can be colored in 3×2×13 \times 2 \times 1 ways, and then only one color is left for the fourth edge. That gives 6 rainbow colorings.