This page is still under construction.

Parts of this page are still being built. What you see may change.

Distinct weights on a tree path

Time limit2sMemory limit512 MB

Level

Not classified yet

Solved
No attempts yet

Problem

You are given a tree with NN vertices. A tree is a connected undirected graph with no cycle. The vertices are numbered from 1 to NN and the edges are numbered from 1 to N−1N-1. Every vertex carries one weight.

Write a program that answers the following query.

  • u v: print how many different weight values appear among the vertices on the path from vertex uu to vertex vv. The path includes both endpoints uu and vv.

Input

The first line contains the number of vertices NN. (2≤N≤1000002 \le N \le 100000)

The second line contains the weights of vertices 1 through NN in order. Every weight is a positive integer no larger than 10000001000000.

Each of the next N−1N-1 lines contains two vertex numbers uu and vv joined by edge ii. The given edges always form a single tree.

The next line contains the number of queries MM. (1≤M≤1000001 \le M \le 100000)

Each of the next MM lines contains one query in the form u v. Both uu and vv are between 1 and NN, and queries where the two values are equal also occur.

Output

Print the answer to each query on its own line, in the order the queries are given.

Examples3

  1. Example 1

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

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

    Input
    10
    10 20 30 40 50 60 70 80 90 100
    1 2
    2 3
    3 4
    4 5
    5 6
    6 7
    7 8
    8 9
    9 10
    5
    1 10
    5 5
    4 7
    10 1
    2 9
    
    Expected output
    10
    1
    4
    10
    8