notdivisible

Time limit: 1.75s Memory limit: 128MB Input: Output:

Task

You have a tree (a connected acyclic graph) of NN nodes indexed from 11 to NN. Each node ii has an integer value Vi>1V_i > 1 assigned to it.

You are given QQ queries, where each query ii consists of two integers AiA_i and BiB_i. Your task is to find the minimum value (greater than 1) that is not divisible by any of the assigned values on the path between AiA_i and BiB_i, including the values assigned to AiA_i and BiB_i.

Input

The first line contains one integer NN (2N100 0002 \le N \le 100\ 000), the number of nodes. The second line contains NN integers V1,V2,,VNV_1, V_2, \ldots, V_N (2Vi100 000 0002 \le V_i \le 100\ 000\ 000).

Each of the following N1N-1 lines contains two integers aa and bb, meaning that there is an edge between aa and bb.

The next line contains one integer QQ (1Q100 0001 \le Q \le 100\ 000), the number of queries. Each of the next QQ lines contains two integers AiA_i and BiB_i, describing a query.

For tests worth 55 points: N,Q1000N, Q \le 1000 and every node is connected to at most 22 other nodes.

For tests worth 55 points: N,Q1000N, Q \le 1000.

For tests worth 1010 points: Vi3V_i \le 3 for each i=1Ni=1\ldots N and every node is connected to at most 22 other nodes.

For tests worth 1010 points: Vi3V_i \le 3 for each i=1Ni=1\ldots N.

For tests worth 1010 points: All ViV_i values are unique for i=1Ni=1\ldots N and every node is connected to at most 22 other nodes.

For tests worth 1515 points: All ViV_i values are unique for i=1Ni=1\ldots N.

For tests worth 2020 points: Every node is connected to at most 22 other nodes.

For tests worth 2525 points: No additional limitations.

Output

For each query, you need to write a single line with an integer representing the answer to that query.

stdin

9
7 25 8 4 1000000 6 11 3 2
5 7
5 1
5 6
7 3
1 2
1 4
6 8
2 9
3
8 9
3 8
4 9

stdout

5
2
3

Notes

In the sample case:

  • Node values on the path of the first query are 33, 66, 10000001000000, 77, 2525, 22. The minimum integer not divisible by any of them is 55.
  • Node values on the path of the second query are 88, 1111, 10000001000000, 66, 33. The minimum integer not divisible by any of them is 22.
  • Node values on the path of the third query are 44, 77, 2525, 22. The minimum integer not divisible by any of them is 33.

Log in or sign up to be able to send submissions!