|
<p> |
|
A group of <strong>N</strong> Foxen reside in a peaceful forest community. |
|
Each Fox's property consists of a tree stump as well as an underground den. |
|
There are <strong>N</strong> - 1 two-way paths on the ground running amongst the tree stumps, |
|
with the <em>i</em>th path connecting the stumps belonging to two different Foxen |
|
<strong>A<sub>i</sub></strong> and <strong>B<sub>i</sub></strong>, |
|
such that all <strong>N</strong> stumps can be reached from one another by following a sequence of paths. |
|
Similarly, there are <strong>N</strong> - 1 underground tunnels running amongst the dens, |
|
with the <em>i</em>th tunnel connecting the dens belonging to Foxen |
|
<strong>C<sub>i</sub></strong> and <strong>D<sub>i</sub></strong>, |
|
such that all <strong>N</strong> dens can be reached from one another. |
|
There's additionally a passageway connecting the tree stump and den belonging to the 1st Fox, |
|
which is the only way in the whole forest to get underground from the surface and vice versa. |
|
</p> |
|
|
|
<p> |
|
At night the Foxen sleep in their dens, but during the daytime, they like to emerge and relax lazily on their tree stumps. |
|
Each day, every Fox takes a trip from their den to their tree stump, |
|
taking the unique shortest path through the system of tunnels and paths to get there. |
|
However, this often requires passing through other Foxen's properties, which they don't appreciate a whole lot. |
|
To compensate, the Foxen have started charging each other tolls for said passage. |
|
They don't have much of a currency, but Foxen do love crackers, so those will do. |
|
Over a given period of <strong>M</strong> days, on the <em>i</em>th day, two different Foxen |
|
<strong>W<sub>i</sub></strong> and <strong>X<sub>i</sub></strong> will each charge tolls for one of their pieces of property. |
|
If <strong>Y<sub>i</sub></strong> = "T", |
|
then Fox <strong>W<sub>i</sub></strong> will be charging tolls for passage through their tree stump. |
|
Otherwise, if <strong>Y<sub>i</sub></strong> = "D", |
|
then Fox <strong>W<sub>i</sub></strong> will instead be charging tolls for passage through their den. |
|
Similarly, Fox <strong>X<sub>i</sub></strong> will be charging tolls for passage through either their tree stump |
|
(if <strong>Z<sub>i</sub></strong>= "T") or their den (if <strong>Z<sub>i</sub></strong>= "D"). |
|
</p> |
|
|
|
<p> |
|
Each day, whenever a Fox passes through another Fox's den or stump which is subject to tolls on that day, |
|
they'll normally need to pay up with 2 crackers. However, if they've already paid a toll earlier on that same trip, |
|
then the property-owning Fox will take pity and only charge them 1 cracker instead of 2. |
|
As such, a Fox's daily trip may end up costing them at most 3 crackers. A Fox will never charge themselves a toll, of course. If a pair of Foxen both owe each other crackers, they'll still both pay up as normal, rather than attempting to minimize the number of cracker transactions performed. |
|
</p> |
|
|
|
<p> |
|
The Foxen are having some trouble keeping track of how many crackers they owe one another. On each of the <strong>M</strong> days, they'd like to count up the total number of crackers which will be charged as part of the tolls for the <strong>N</strong> trips taken on that day. To avoid dealing with too many large numbers, they'd like to combine these <strong>M</strong> cracker counts into a single value as follows (where <strong>V<sub>i</sub></strong> is the <em>i</em>th day's count): |
|
</p> |
|
|
|
<p> |
|
|
|
( ... (((<strong>V<sub>1</sub></strong> * 12,345) + <strong>V<sub>2</sub></strong>) * 12,345 + <strong>V<sub>3</sub></strong>) ... * 12,345 + <strong>V<sub>M</sub></strong>) modulo 1,000,000,007 |
|
</p> |
|
|
|
<p> |
|
Please help the Foxen compute this combined value! |
|
</p> |
|
|
|
<h3>Input</h3> |
|
|
|
<p> |
|
Input begins with an integer <strong>T</strong>, the number of different communities of Foxen. |
|
For each community of Foxen, there is first a line containing the space-separated integers <strong>N</strong> and <strong>M</strong>. |
|
Then <strong>N - 1</strong> lines follow, the <em>i</em>th of which contains the space-separated integers |
|
<strong>A<sub>i</sub></strong> and <strong>B<sub>i</sub></strong>. |
|
Then <strong>N - 1</strong> lines follow, the <em>i</em>th of which contains the space-separated integers |
|
<strong>C<sub>i</sub></strong> and <strong>D<sub>i</sub></strong>. |
|
Then <strong>M</strong> lines follow, the <em>i</em>th of which contains the integers |
|
<strong>W<sub>i</sub></strong> and <strong>X<sub>i</sub></strong> and the characters |
|
<strong>Y<sub>i</sub></strong> and <strong>Z<sub>i</sub></strong>, all separated by spaces. |
|
</p> |
|
|
|
<h3>Output</h3> |
|
|
|
<p> |
|
For the <em>i</em>th community of Foxen, print a line containing "Case #<strong>i</strong>: " |
|
followed by a single integer, the requested combined value based on the <strong>M</strong> days' cracker counts, modulo 1,000,000,007. |
|
</p> |
|
|
|
<h3>Constraints</h3> |
|
|
|
<p> |
|
1 ≤ <strong>T</strong> ≤ 30<br /> |
|
2 ≤ <strong>N</strong> ≤ 500,000 <br /> |
|
1 ≤ <strong>M</strong> ≤ 500,000 <br /> |
|
1 ≤ |
|
<strong>A<sub>i</sub></strong>, <strong>B<sub>i</sub></strong>, |
|
<strong>C<sub>i</sub></strong>, <strong>D<sub>i</sub></strong>, |
|
<strong>W<sub>i</sub></strong>, <strong>X<sub>i</sub></strong> |
|
≤ <strong>N</strong> <br /> |
|
Both the sum of <strong>N</strong> values and the sum of <strong>M</strong> values across all <strong>T</strong> cases do not exceed 1,500,000. |
|
</p> |
|
|
|
<h3>Explanation of Sample</h3> |
|
|
|
<p> |
|
In the first case, Fox 1 doesn't need to pay any tolls to get from its den to its tree stump, while Fox 2 must pay 2 crackers to complete its trip due to passing through Fox's 1 tree stump. |
|
</p> |
|
|
|
<p> |
|
In the second case, 5 crackers will be charged on the first day (the 3 Foxen must pay 0, 2, and 3 crackers, respectively), 2 crackers will be charged on the second day, and none will be charged on the third day. This results in a final answer of (((5 * 12,345) + 2) * 12,345) + 0) modulo 1,000,000,007 = 762,019,815. |
|
</p> |
|
|
|
<p> |
|
In the third case, 7, 6, and 4 crackers will be charged on each of the three days, respectively. |
|
</p> |
|
|