This page is still under construction.

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

Master Zhu and Rikka

Time limit3sMemory limit512 MB

Summary
Given a rooted tree with values on vertices, answer queries that ask for the GCD of sums of values occurring exactly a times and exactly b times in a subtree or on a path.
Level

Hard9 of 10

Topics
Tree, DFS, Prefix sum, Number theory
Solved
No attempts yet

Problem

As everyone knows, Master Zhu is the most powerful man in the universe. He has infinite power to protect the world.

A magical rooted tree grows in Master Zhu's garden. The tree has nn vertices and n−1n - 1 edges. The root of the tree is vertex 11. Each vertex ii holds an amount of Zhu power equal to a_ia\_i.

Little Rikka is a curious girl: she has mm questions to ask Master Zhu. But Master Zhu is busy protecting our world right now, so he wants you to help him answer Rikka's questions.

Each of Rikka's questions has the format "tt uu vv aa bb".

  • If tt is 11, then uu equals vv, and Rikka looks at the subtree rooted at vertex uu and wants to know the GCD (greatest common divisor) of S_aS\_a and S_bS\_b, where S_aS\_a is the sum of the numbers that appear exactly aa times in this subtree and S_bS\_b is the sum of the numbers that appear exactly bb times in this subtree.
  • If tt is 22, Rikka looks at the simple path between vertices uu and vv and wants to know the GCD of T_aT\_a and T_bT\_b, where T_aT\_a is the sum of the numbers that appear exactly aa times on this path and T_bT\_b is the sum of the numbers that appear exactly bb times on this path.

Here, for any xx, we define GCD(x,0)=GCD(0,x)=x\mathrm{GCD} (x, 0) = \mathrm{GCD} (0, x) = x.

Input

The first line of input contains an integer TT, the number of test cases (1≤T≤101 \le T \le 10).

The first line of each test case contains two integers nn and mm: the number of vertices in the magical tree and the number of Rikka's questions (1≤n,m≤1051 \le n, m \le 10^5).

The next line contains nn integers a_1a\_1, a_2a\_2, …\ldots, a_na\_n: the amount of Zhu power in each vertex (1≤a_i≤1091 \le a\_i \le 10^9).

Each of the next n−1n - 1 lines contains two integers uu and vv and denotes an edge connecting vertices uu and vv (1≤u,v≤n1 \le u, v \le n). It is guaranteed that these edges form a tree.

Each of the next mm lines contains one of Rikka's questions in the format described above (1≤u,v≤n1 \le u, v \le n, 1≤a,b≤n1 \le a, b \le n).

Output

For each of Rikka's questions, print the answer on a separate line.

Examples1

  1. Example 1

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