Under construction

Still being built. Things may change

Gomgomi's Part-Time Job

Time limit2sMemory limit1024 MB

Summary
Given a tree and delivery triples (A, B, C), count the ordered pairs of distinct cities where two drumsticks can be bought along the walk A to B to C.
Level

Medium7 of 10

Topics
Tree, DFS, Implementation
Solved
No attempts yet

Problem

Gomgomi delivering

Gomgomi, who is good at running errands, recently got a chance to take a delivery part-time job.

In Gomgomi's country, delivery orders must be handled in pairs, in the order they came in. So once Gomgomi sets out, Gomgomi must visit two cities in order. Each order is written as a tuple of three cities (A,B,C)(A, B, C). Gomgomi starts at city AA, where the restaurant is, travels along the shortest path to city BB, and then travels along the shortest path from BB to CC. The country is a tree: NN cities are connected by N−1N-1 bidirectional roads, forming a connected graph.

Gomgomi likes chicken and tries to buy chicken drumsticks whenever possible. Gomgomi can buy a drumstick in any city passed along the way, but at most one drumstick per city. When the order is finished, there must be exactly two drumsticks.

Each time Gomgomi finishes an order, Gomgomi writes in a notebook the ordered pair of cities where the drumsticks were bought. For example, if a drumstick was bought in city 3 and then in city 1, the pair is written as (3,1)(3, 1).

Find the number of ordered pairs that can be made for each order.

Input

The first line contains the number of cities NN and the number of orders Q (1≤N,Q≤100 000)Q\ (1 \leq N, Q \leq 100\,000), separated by a space.

From the second line through the N−1N-1th line after that, each line contains integers uu and vv, separated by a space. This means there is a bidirectional road connecting city uu and city vv. (1≤u,v≤N)(1 \leq u, v \leq N)

From the N+1N+1th line through the QQ lines after that, each line contains integers AA, BB, and CC (1≤A,B,C≤N)(1 \le A, B, C \le N), separated by spaces, which form the delivery order (A,B,C)(A, B, C).

Output

For each delivery order, print on a separate line the number of ordered pairs that can be made for that order. There are QQ lines in total.

Examples2

  1. Example 1

    Input
    5 3
    1 2
    2 3
    3 4
    3 5
    1 5 4
    1 1 1
    2 3 4
    
    Expected output
    11
    0
    3
    
  2. Example 2

    Input
    8 5
    1 2
    2 3
    3 4
    3 5
    6 4
    5 7
    7 8
    6 1 2
    4 3 1
    7 8 8
    3 1 8
    5 4 2
    
    Expected output
    11
    6
    1
    18
    7