You are given a permutation of numbers which are to and a tree with nodes each node having a number written on it. Then you will be given queries. In each query you will be given 4 numbers: . You have to multiply all the numbers written on the nodes which are on the path from node and , let the multiplication is . Then you have to perform some steps. In each step, you have to:
choose any prime number which appears in the permutation from position to and can perfectly divide .
update the value of by .
The above step should be performed, until can’t be divided by any prime from the range to in the permutation.
You have to count the number of required steps for each query.
The first line contains three numbers , and , denoting the size of the permutation, the number of nodes in the tree and the number of queries. and
The next line contains integers, denoting a permutation of first integers ( to ).
The next line contains integers , the number denotes the number written on the node .
Each of the next lines contains two numbers and , denoting there is an edge between the nodes and .
Each of the next lines contains four integers: , denoting a query. and
For each query, print the required number of steps.
Input | Output |
---|---|
4 5 3 4 2 1 3 6 3 2 5 4 1 2 1 3 3 4 3 5 1 4 4 2 3 4 2 4 2 4 4 5 | 4 2 3 |