Under construction

Still being built. Things may change

Instant Noodles

Time limit2sMemory limit1024 MB

Summary
Given a bipartite graph with weights on the right side, find the gcd of the sums of right-vertex values covered by each nonempty subset of left vertices.
Level

Hard8 of 10

Topics
Hash map, Number theory
Solved
No attempts yet

Problem

Wu got hungry after an intense training session, and came to a nearby store to buy his favourite instant noodles. After Wu paid for his purchase, the cashier gave him an interesting task.

You are given a bipartite graph with positive integers in all vertices of the right half. For a subset SS of vertices of the left half we define N(S)N(S) as the set of all vertices of the right half adjacent to at least one vertex in SS, and f(S)f(S) as the sum of all numbers in vertices of N(S)N(S). Find the greatest common divisor of f(S)f(S) for all possible non-empty subsets SS.

Wu is too tired after his training to solve this problem. Help him!

Input

The first line contains a single integer tt (1≤t≤500 0001 \leq t \leq 500\,000), the number of test cases. Test case descriptions follow.

The first line of each case contains two integers nn and mm (1≤n,m≤500 0001 \leq n, m \leq 500\,000), the number of vertices in either half of the graph and the number of edges.

The second line contains nn integers cic_i (1≤ci≤10121 \leq c_i \leq 10^{12}). The ii-th number is the integer written on vertex ii of the right half.

Each of the following mm lines contains a pair of integers uiu_i and viv_i (1≤ui,vi≤n1 \leq u_i, v_i \leq n), meaning there is an edge between vertex uiu_i of the left half and vertex viv_i of the right half. No multiple edges are given.

Test case descriptions are separated with empty lines. The total of nn across all test cases does not exceed 500,000, and the total of mm does not exceed 500,000 as well.

Output

For each test case, print a single integer, the required greatest common divisor.

Hint

The greatest common divisor of a set of integers is the largest integer gg such that every element of the set is divisible by gg.

In the first sample case, every left vertex is connected to every right vertex, so f(S)=2f(S) = 2 for every non-empty subset SS, and the greatest common divisor is 2.

In the second sample case, the subset {1}\{1\} of the left half is connected to the right vertices {1,2}\{1, 2\}, and their sum is 2. The subset {1,2}\{1, 2\} is connected to the right vertices {1,2,3}\{1, 2, 3\}, with sum 3. So f({1})=2f(\{1\}) = 2 and f({1,2})=3f(\{1, 2\}) = 3, and the greatest common divisor of all values of f(S)f(S) is 1.

Examples1

  1. Example 1

    Input
    3
    2 4
    1 1
    1 1
    1 2
    2 1
    2 2
    
    3 4
    1 1 1
    1 1
    1 2
    2 2
    2 3
    
    4 7
    36 31 96 29
    1 2
    1 3
    1 4
    2 2
    2 4
    3 1
    4 3
    
    Expected output
    2
    1
    12