Under construction

Still being built. Things may change

Zoo

Time limit5sMemory limit1024 MB

Summary
Given a tree of enclosures labeled with species, answer each query with the highest count of one species on the path between two enclosures.
Level

Hard8 of 10

Topics
Tree, Sliding window, Hash map
Solved
No attempts yet

Problem

Jozo started working at a newly built zoo earlier this year. Today he received a special assignment: find the cause of the steady drop in visitors. While watching the enclosures and the paths between them, Jozo suspected that the tours are too boring, that is, that the zoo tours are too uniform.

The zoo has NN enclosures numbered from 11 to NN and N−1N-1 paths, each connecting two enclosures. There is a path between every pair of enclosures (not necessarily a direct one). Each enclosure holds animals of one species. Enclosure ii holds animals of species viv_i.

Jozo asked you to help him determine the uniformity of a path. The uniformity of a path is the number of occurrences of the most common animal among the enclosures on that path, including the enclosures at both ends. For example, if a path passes through enclosures with animals 22, 33, 11, 22, 22, 33, the uniformity is three, because animal 22 appears three times.

For each of the QQ pairs of enclosures (a,b)(a, b), find the uniformity of the (shortest) path from enclosure aa to enclosure bb.

Input

The first line contains natural numbers NN and QQ (1≤N≤1051 \le N \le 10^5, 1≤Q≤1051 \le Q \le 10^5).

The second line contains NN natural numbers viv_i (1≤vi≤1091 \le v_i \le 10^9), the species of the animals in enclosure ii.

Each of the next N−1N-1 lines contains two natural numbers xix_i and yiy_i (1≤xi,yi≤N1 \le x_i, y_i \le N), the two enclosures connected by the ii-th path.

Each of the next QQ lines contains two natural numbers aia_i and bib_i (1≤ai,bi≤N1 \le a_i, b_i \le N), describing the path from enclosure aia_i to enclosure bib_i whose uniformity must be found.

Output

Print QQ lines. On the ii-th line, print the answer to the ii-th question.

Hint

This explains the first sample. Each node shows the enclosure number and the animal species.

Query 1: the enclosures on the path are 1 and 2. Species 1 and 2 each appear once, so the uniformity is 11.

Query 2: the enclosures on the path are 1, 2, and 4. Species 1 appears twice, so the uniformity is 22.

Query 3: the enclosures on the path are 4, 2, 1, 3, and 7. Species 1 appears three times, in enclosures 1, 4, and 7.

Query 4: the enclosures on the path are 2, 1, and 3. Species 2 appears twice and species 1 appears once.

Query 5: the enclosures on the path are 5, 2, 1, 3, and 6. Species 2 appears four times and species 1 appears once.

Examples3

  1. Example 1

    Input
    7 5
    1 2 2 1 2 2 1
    1 2
    1 3
    2 4
    2 5
    3 6
    3 7
    1 2
    1 4
    4 7
    2 3
    5 6
    
    Expected output
    1
    2
    3
    2
    4
    
  2. Example 2

    Input
    6 4
    1 2 3 1 2 3
    1 2
    2 3
    3 4
    4 5
    5 6
    1 3
    2 5
    1 4
    1 6
    
    Expected output
    1
    2
    2
    2
    
  3. Example 3

    Input
    4 4
    2 5 2 3
    2 1
    2 3
    4 2
    1 2
    1 4
    1 3
    2 3
    
    Expected output
    1
    1
    2
    1