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

#LCA0000052. Giấc ngủ của chú lười (Sloth Naptime)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Giấc ngủ của chú lười (Sloth Naptime)

Sloth Naptime

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

David's sloth lives on an unweighted tree. In each situation it starts at vertex aa and wants to travel to vertex bb, but has energy to traverse at most cc edges. It always moves along the unique shortest path toward bb and stops upon arriving or exhausting its movement allowance.

For every query, determine where the sloth stops. If a=ba=b, it stays put.

Input

  • The first line contains nn.
  • The next n−1n-1 lines describe edges u,vu,v.
  • The next line contains qq.
  • Each of the final qq lines contains a,b,ca,b,c.

Output

Print the vertex where the sloth stops, one result per query.

Subtasks

General constraints: 1≤n,q≤3⋅1051\le n,q\le3\cdot10^5; 1≤a,b,c,u,v≤n1\le a,b,c,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≤3000001\le n\le 300000; 1≤q≤3000001\le q\le 300000.

Examples

Input

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

Output

5
5
5
5
5

Explanation

Vertex 44 is adjacent to 1,2,3,51,2,3,5. The route 3−4−53-4-5 takes two edges, so both movement allowances 22 and 44 reach 55. Every remaining query likewise allows enough steps to reach vertex 55, giving five identical answers.