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

#LCA0000051. Đỉnh thứ k trên đường đi (Jump on Tree)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đỉnh thứ k trên đường đi (Jump on Tree)

Jump on Tree

Source: Library Checker

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an undirected tree with vertices indexed from 00 to n−1n-1. On the unique path from uu to vv, the starting vertex uu occupies index 00, the next vertex index 11, and so on.

For each query u,v,ku,v,k, return the vertex at zero-based position kk. If the path contains fewer than k+1k+1 vertices, print -1.

Input

  • The first line contains n,qn,q.
  • The following n−1n-1 lines contain edges u,vu,v.
  • Each of the last qq lines contains u,v,ku,v,k.

Output

Print one vertex index or -1 per query.

Subtasks

General constraints: 1≤n,q≤5⋅1051\le n,q\le5\cdot10^5 (giới hạn PHOJ); 0≤u,v<n0\le u,v<n; 0≤k≤n0\le k\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 4
0 1
1 2
1 3
3 4
2 4 0
2 4 2
2 4 3
2 4 5

Output

2
3
4
-1

Explanation

The path 2−1−3−42-1-3-4 contains vertices at indices 0,1,2,30,1,2,3 respectively. Queries for positions 0,2,30,2,3 return 2,3,42,3,4. Position 55 does not exist, so the answer is -1.