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

#CC0000072. Giá trị nhỏ thứ k trên đường đi của cây (COT - Count on a tree)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Giá trị nhỏ thứ k trên đường đi của cây (COT - Count on a tree)

COT - Count on a tree

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a tree with nn vertices, each with an integer weight wiw_i. For query (u,v,k)(u,v,k), consider all vertex weights on the unique path from uu to vv, including both endpoints, and print the kk-th smallest weight. Repeated weights count with multiplicity.

Input

The first line contains n,qn,q. The second contains nn weights. Then follow n−1n-1 tree edges u,vu,v, and qq queries u,v,ku,v,k with 1≤k≤1\le k\le the number of vertices on the path.

Output

For each query, print the kk-th smallest weight on the specified path.

Subtasks

Original constraints: 1≤n,q≤1051\le n,q\le10^5; 1≤u,v≤n1\le u,v\le n; 1≤k≤length⁡(u,v)+11\le k\le \operatorname{length}(u,v)+1. In this PHOJ dataset, ∣wi∣≤109|w_i|\le10^9 (the SPOJ statement does not give an explicit weight bound).

  • Subtask 1 (20 points): 1≤n≤181\le n\le18; 1≤q≤201\le q\le20; ∣wi∣≤120|w_i|\le120; valid tree and path queries.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le2000; 1≤q≤20001\le q\le2000; ∣wi∣≤100000|w_i|\le100000; valid tree and path queries.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le100000; 1≤q≤1000001\le q\le100000; ∣wi∣≤1000000000|w_i|\le1000000000; valid tree and path queries.

Examples

Input

8 5
105 2 9 3 8 5 7 7
1 2
1 3
1 4
3 5
3 6
3 7
4 8
2 5 1
2 5 2
2 5 3
2 5 4
7 8 2

Output

2
8
9
105
7

Explanation

The path from vertex 22 to vertex 55 has weights 2,105,9,82,105,9,8, which sort to 2,8,9,1052,8,9,105. This explains the first four answers.