Under construction

Still being built. Things may change

Daylight

Time limit13sMemory limit512 MB

Summary
Given an unrooted tree and online daily queries (u, v, w), count the vertices within w edges of u or v, with each query decoded from the previous answer.
Level

Hard8 of 10

Topics
Tree, Divide and conquer, Implementation
Solved
No attempts yet

Statement

Noah owns an unrooted tree with nn vertices numbered from 1 to nn. Every morning Noah picks two vertices uu and vv to receive influence from daylight. The influence spreads through the edges, but it disappears once it has passed through more than ww edges.

For each day, count the vertices influenced by the daylight. The input is encrypted, so your solution must work online.

Input

The first line contains TT, the number of test cases.

For each test case, the first line contains nn and mm. Each of the next n−1n-1 lines contains uu and vv, an edge between vertices uu and vv.

Let lastanslastans be the answer to the previous day. At the start of each test case, lastanslastans is 0. Each of the next mm lines contains u′u', v′v', and w′w', and the actual values are

u=((u′+lastans) mod n)+1,v=((v′+lastans) mod n)+1,w=(w′+lastans) mod nu = ((u' + lastans) \bmod n) + 1,\quad v = ((v' + lastans) \bmod n) + 1,\quad w = (w' + lastans) \bmod n

Constraints: 1≤T≤1001 \le T \le 100, 1≤n,m≤1051 \le n, m \le 10^5, 1≤u,v,u′,v′≤n1 \le u, v, u', v' \le n, 0≤w′<n0 \le w' < n.

No more than 10 test cases fail to satisfy n,m≤103n, m \le 10^3, and the input file is at most 32 MiB.

Output

For each day, print the answer on one line.

The output file is at most 7 MiB.

Hint

The decrypted values are as follows. Day 1: u=1u = 1, v=2v = 2, w=0w = 0. Day 2: u=1u = 1, v=2v = 2, w=1w = 1. Day 3: u=1u = 1, v=2v = 2, w=2w = 2. Day 4: u=1u = 1, v=2v = 2, w=3w = 3. Day 5: u=1u = 1, v=2v = 2, w=4w = 4.

Examples1

  1. Example 1

    Input
    1
    5 5
    1 2
    2 3
    2 4
    3 5
    5 1 0
    3 4 4
    1 2 3
    5 1 3
    5 1 4
    
    Expected output
    2
    4
    5
    5
    5