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

#LCA0000056. Tập hợp khẩn cấp (Emergency Assembly)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tập hợp khẩn cấp (Emergency Assembly)

Emergency Assembly

Source: Luogu

Version: Phuoc Hung OJ Extended

Problem Statement

On an island, nn assembly points are connected by n−1n-1 undirected roads forming a tree. Traversing one road costs one coin.

Whenever an assembly is called, three people starting at points x,y,zx,y,z (possibly coincident) must choose one common assembly point pp. Their total cost is the sum of their path lengths to pp. Find a point minimizing this total and print the minimum cost.

Input

  • The first line contains n,qn,q: numbers of points and assemblies.
  • The next n−1n-1 lines describe edges u,vu,v.
  • The final qq lines each contain three starting positions x,y,zx,y,z.

Output

For each query, print two integers p,cp,c: a minimum-cost meeting point and the associated minimum total cost.

Subtasks

General constraints: 1≤n,q≤5⋅1051\le n,q\le5\cdot10^5; 1≤x,y,z,u,v≤n1\le x,y,z,u,v\le n.

  • Subtask 1 (20 points): 1≤n≤401\le n\le 40; 1≤q≤501\le q\le 50.

  • Subtask 2 (30 points): 1≤n≤10001\le n\le 1000; 1≤q≤10001\le q\le 1000.

  • Subtask 3 (50 points): 1≤n≤5000001\le n\le 500000; 1≤q≤5000001\le q\le 500000.

Examples

Input

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

Output

3 4
1 2
4 2

Explanation

For (2,4,5)(2,4,5), meeting at 33 costs 2+1+1=42+1+1=4. For (1,2,3)(1,2,3), meeting at 11 costs 0+1+1=20+1+1=2. For (4,4,5)(4,4,5), meeting at 44 costs 0+0+2=20+0+2=2. These points minimize the respective total travel costs.