Distinct weights on a tree path

No attempts yetTime limit2sMemory limit512 MB

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 N1N-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. (2N1000002 \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 N1N-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. (1M1000001 \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.