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

#LCA0000073. Phục hưng — LCA theo đoạn và gốc (Regnaissance)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Phục hưng — LCA theo đoạn và gốc (Regnaissance)

Regnaissance

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

An undirected tree has vertices labeled 11 through nn. For each query (l,r,x)(l,r,x), temporarily root the tree at xx and compute the LCA of all vertices whose labels belong to [l,r][l,r], endpoints included. The root choice applies only to that query. The label interval does not refer to DFS order. The tree edges never change.

Input

First line: n qn\ q, then n−1n-1 edges u vu\ v, then qq queries l r xl\ r\ x.

Output

For each query print the LCA vertex label under root xx.

Subtasks

General constraints: 1≤n,q≤3⋅1051\le n,q\le 3\cdot10^5; 1≤l≤r≤n1\le l\le r\le n; 1≤x≤n1\le x\le n; đầu vào là cây.

  • 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 6
1 2
1 3
3 4
3 5
1 3 2
1 1 2
2 3 2
4 5 4
2 5 3
4 5 5

Output

2
1
2
4
3
5

Explanation

For (1,1,2)(1,1,2) the query set contains only vertex 11, so its LCA is 11 regardless of root. For (4,5,4)(4,5,4) the root 44 is itself included, and is the LCA.