PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#LCA0000091. Tổng lũy thừa độ sâu trên đường đi (Sum of Powers of Depths)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tổng lũy thừa độ sâu trên đường đi (Sum of Powers of Depths)

Sum of Powers of Depths

Source: Luogu

Version: Phuoc Hung OJ Extended

Problem Statement

An undirected tree of nn vertices is rooted at vertex 11. The depth dvd_v of vertex vv is the number of edges on the path from vertex 11 to vv; in particular d1=0d_1=0.

For each of mm queries (u,v,k)(u,v,k), sum dxkd_x^k over all vertices of the unique path from uu to vv, including both endpoints. Print the result modulo 998244353998244353. Different queries may use different exponents kk. Since k≥1k\ge1, the root contributes 00 if it lies on the path.

Input

The first line contains nn; the next n−1n-1 lines contain undirected edges u,vu,v. Next comes mm, followed by mm queries u,v,ku,v,k.

Output

Print one integer per query: the requested sum modulo 998244353998244353.

Subtasks

  • Subtask 1 (20 points): 1≤n≤351\le n\le 35; 1≤m≤351\le m\le 35; 1≤k≤501\le k\le50; 1≤u,v≤n1\le u,v\le n; edges form an undirected tree rooted at 11.

  • Subtask 2 (30 points): 1≤n≤12001\le n\le 1200; 1≤m≤12001\le m\le 1200; 1≤k≤501\le k\le50; 1≤u,v≤n1\le u,v\le n; edges form an undirected tree rooted at 11.

  • Subtask 3 (50 points): 1≤n≤3000001\le n\le 300000; 1≤m≤3000001\le m\le 300000; 1≤k≤501\le k\le50; 1≤u,v≤n1\le u,v\le n; edges form an undirected tree rooted at 11.

Examples

Input

5
1 2
1 3
2 4
2 5
2
1 4 5
5 4 45

Output

33
503245989

Explanation

The root has depth 00; vertices 2,32,3 have depth 11; vertices 4,54,5 have depth 22. Path 1−2−41-2-4 yields 05+15+25=330^5+1^5+2^5=33. Path 5−2−45-2-4 yields (245+145+245) mod 998244353=503245989(2^{45}+1^{45}+2^{45})\bmod 998244353=503245989.