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

#SRD0000093. Đỉnh gần nhất (Nearest vertex)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đỉnh gần nhất (Nearest vertex)

Nearest vertex

Source: MarisaOJ

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an undirected tree with nn vertices, numbered from 11 to nn. Vertex ii has an integer value AiA_i. The distance between two vertices is the number of edges on their unique simple path.

Every query specifies a starting vertex uu and a value xx. Find the minimum distance from uu to any vertex labelled xx. A destination vertex can be reused for different queries. If no vertex carries value xx, print -1.

If the starting vertex uu itself has value xx, the answer is 00.

Input

  • The first line contains n,qn,q.
  • The second line contains nn integer values A1,…,AnA_1,\ldots,A_n.
  • The next n−1n-1 lines each contain endpoints u,vu,v of an undirected tree edge.
  • Each of the final qq lines contains a starting vertex uu and the target value xx.

Output

Print one line per query: the minimum number of edges to a matching vertex, or -1 if no matching vertex exists.

Subtasks

  • Subtask 1 (10 points): n≤30n\le 30, q≤50q\le 50.
  • Subtask 2 (20 points): n≤700n\le 700, q≤700q\le 700.
  • Subtask 3 (30 points): n≤10000n\le 10000, q≤10000q\le 10000.
  • Subtask 4 (40 points): n≤50000n\le 50000, q≤50000q\le 50000.

Complete conditions for all subtasks, subject to the tighter limits above: 1≤n,q≤5⋅1041\le n,q\le 5\cdot 10^4, 1≤Ai,x≤n1\le A_i,x\le n, 1≤u,v≤n1\le u,v\le n.

Examples

Input

3 2
1 2 3
1 2
1 3
2 2
1 3

Output

0
1

Explanation

The tree edges are (1,2)(1,2) and (1,3)(1,3), and the values at vertices 1,2,31,2,3 are 1,2,31,2,3. For query (2,2)(2,2), vertex 22 itself has the requested value, yielding 0. For query (1,3)(1,3), the only vertex with value 33 is vertex 33, one edge from vertex 11, yielding 1.