Instant Noodles

시간 제한2초메모리 제한1024 MB

요약
오른쪽 정점에 가중치가 있는 이분 그래프에서 왼쪽 정점의 모든 공집합이 아닌 부분집합 S에 대해 이웃 N(S)의 가중치 합의 최대공약수를 구한다.
난이도

보통10점 중 7점

유형
그래프, 수학, 정수론, 해시맵
정답자
아직 제출이 없습니다

문제

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!

입력

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

The first line of each case description 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 respectively.

The second line contains nn integers c_ic\_i (1≤c_i≤10121 \leq c\_i \leq 10^{12}). The ii-th number describes the integer in the vertex ii of the right half of the graph.

Each of the following mm lines contains a pair of integers u_iu\_i and v_iv\_i (1≤u_i,v_i≤n1 \leq u\_i, v\_i \leq n), describing an edge between the vertex u_iu\_i of the left half and the vertex v_iv\_i of the right half. It is guaranteed that the graph does not contain multiple edges.

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

출력

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

힌트

The greatest common divisor of a set of integers is the largest integer gg such that all elements of the set are divisible by gg.

In the first sample case vertices of the left half and vertices of the right half are pairwise connected, and f(S)f(S) for any non-empty subset is 22, thus the greatest common divisor of these values if also equal to 22.

In the second sample case the subset 1\\{1\\} in the left half is connected to vertices 1,2\\{1, 2\\} of the right half, with the sum of numbers equal to 22, and the subset 1,2\\{1, 2\\} in the left half is connected to vertices 1,2,3\\{1, 2, 3\\} of the right half, with the sum of numbers equal to 33. Thus, f(1)=2f(\\{1\\}) = 2, f(1,2)=3f(\\{1, 2\\}) = 3, which means that the greatest common divisor of all values of f(S)f(S) is 11.

예제1

  1. 예제 1

    입력
    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
    
    예상 출력
    2
    1
    12