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

#LCA0000043. Tổ tiên chung của một đoạn chỉ số (VO 15 — Tree)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tổ tiên chung của một đoạn chỉ số (VO 15 — Tree)

VO 15 — Tree

Source: VNOJ

Version: Phuoc Hung OJ Extended

Problem Statement

An undirected tree has nn vertices numbered from 11 to nn and is rooted at vertex 11. For each query (u,v)(u,v) with u≤vu\le v, find the deepest vertex that is an ancestor of every vertex with index u,u+1,…,vu,u+1,\ldots,v.

This is a range of vertex indices, not the path between vertices uu and vv. When u=vu=v, the answer is uu.

Input

  • First line: n,qn,q.
  • Next n−1n-1 lines contain undirected edges x,yx,y.
  • Last qq lines contain index ranges u,vu,v with u≤vu\le v.

Output

Print the lowest common ancestor of all vertices in each index range.

Subtasks

General constraints (all subtasks): 1≤n,q≤700001\le n,q\le 70000; 1≤u≤v≤n1\le u\le v\le n.

  • Subtask 1 (20 points): 1≤n≤301\le n\le 30; 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≤700001\le n\le 70000; 1≤q≤700001\le q\le 70000.

Examples

Input

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

Output

2
1
3

Explanation

Vertices 2,3,4,52,3,4,5 are all in the subtree of 22, so query [2,5][2,5] returns 22. Vertices 4,54,5 have LCA 33.