#include <bits/stdc++.h>
#define ll long long
using namespace std;

const long long MAXVAL = (long long) 1e18 + 1;
const long long MOD = 1000000007;
const int INF = 1000000000 + 1;

long long poww(int x, long long pow) {
  if (pow == 0ll ) return 1;
  if (pow == 1ll) return x % MOD;
  long long ans = poww(x, pow >> 1) % MOD;
  ans = (ans * ans) % MOD;
  if ((pow & 1) == 1) {
    ans = (ans * x ) % MOD;
  }
  return ans % MOD;
}

ll gcd(ll x, ll y) {
  while (y) {
    x %= y;
    swap(x, y);
  }
  return x;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int q = 1;
  cin >> q;

  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  while (q-- >0) {
    int n;
    cin >> n;
    int m;
    cin >> m;
    vector<ll> a(n, 0);
    ll p[n];
    for (int i = 0 ;i < n; ++i) cin >> p[i];
    vector<vector<int>> gr(n);
    for (int i  =0; i < m; ++i) {
      int x, y;
      cin >> x >> y;
      --x;
      --y;
      a[x] += p[y];
      gr[x].push_back(y);
    }
    ll ans = 0;
    for (int i = 0; i < n; ++i) ans = gcd(ans, a[i]);
    int pow = n;
      unordered_set<int> cur;
    for (int i  =0; i < pow; ++i) {
      int v = i;
      for (auto next : gr[v]) {
        cur.insert(next);
      }
    }
    ll ans0 = 0;
    for (auto x : cur) {
      ans0 += p[x];
    }
    ans = gcd(ans, ans0);
    int time = 0;
    auto dis = uniform_int_distribution<int>(2, n);
    auto dis2 = uniform_int_distribution<int>(0, n - 1);
    while (time < max(5 * (n + m), 1000)) {
      int pow = dis(rng);
      pow = min(pow, n);
      unordered_set<int> cur;
      for (int i  =0; i < pow; ++i) {
        int v = dis2(rng);
        for (auto next : gr[v]) {
          cur.insert(next);
        }
      }
      ll ans0 = 0;
      for (auto x : cur) {
        ans0 += p[x];
      }
      ans = gcd(ans, ans0);
      time += pow;
    }
    cout << ans << "\n";
  }
  return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;

const long long MAXVAL = (long long) 1e18 + 1;
const long long MOD = 1000000007;
const int INF = 1000000000 + 1;

long long poww(int x, long long pow) {
  if (pow == 0ll ) return 1;
  if (pow == 1ll) return x % MOD;
  long long ans = poww(x, pow >> 1) % MOD;
  ans = (ans * ans) % MOD;
  if ((pow & 1) == 1) {
    ans = (ans * x ) % MOD;
  }
  return ans % MOD;
}

ll gcd(ll x, ll y) {
  while (y) {
    x %= y;
    swap(x, y);
  }
  return x;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int q = 1;
  cin >> q;

  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  while (q-- >0) {
    int n;
    cin >> n;
    int m;
    cin >> m;
    vector<ll> a(n, 0);
  vector<bool> iso(n, true);
    ll p[n];
    for (int i = 0 ;i < n; ++i) cin >> p[i];
    vector<vector<int>> gr(n);
    vector<int> h1(n, 0);
    vector<int> h2(n, 0);
    for (int i  =0; i < m; ++i) {
      int x, y;
      cin >> x >> y;
      --x;
      --y;
      a[x] += p[y];
      gr[x].push_back(y);
      iso(y) = false;
      h1[y] = (h1[y] * 31ll + x) % MOD;
      h2[y] = (h2[y] * 37ll + x) % MOD;
    }
    vector<bool> there(n, true);
    map<pair<int, int>, int> grouping;
    for (int i = 0; i < n; ++i) {
      if (iso[i]) continue;
      pair<int, int> key = {h1[i], h2[i]};
      if (grouping.find(key) != grouping.end()) {
        int ex = grouping[key];
        there[i] = false;
        p[ex] += p[i];
      } else {
        grouping[key] = i;
      }
    }

    ll ans = 0;
    for (int i = 0; i < n; ++i) ans = gcd(ans, a[i]);
    for (int i = 0; i < n; ++i) {
      if (there[i] && !iso[i]) {
        ans = gcd(ans, p[i]);
      }
    }
    int pow = n;
    unordered_set<int> cur;
    for (int i  =0; i < pow; ++i) {
      int v = i;
      for (auto next : gr[v]) {
        cur.insert(next);
      }
    }
    ll ans0 = 0;
    for (auto x : cur) {
      if (there[x] && !iso[x]) {
        ans0 += p[x];
      }
    }
    ans = gcd(ans, ans0);
    //int time = 0;
    //auto dis = uniform_int_distribution<int>(2, n);
    //auto dis2 = uniform_int_distribution<int>(0, n - 1);
    //while (time < max(5 * (n + m), 1000)) {
      //int pow = dis(rng);
      //pow = min(pow, n);
      //unordered_set<int> cur;
      //int i = 0;
      //while (i < pow) {
        //int v = dis2(rng);
        //for (auto next : gr[v]) {
          //cur.insert(next);
        //}
        //++i
      //}
      //ll ans0 = 0;
      //for (auto x : cur) {
        //ans0 += p[x];
      //}
      //ans = gcd(ans, ans0);
      //time += pow;
    //}
    cout << ans << "\n";
  }
  return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;

const long long MAXVAL = (long long) 1e18 + 1;
const long long MOD = 1000000007;
const int INF = 1000000000 + 1;

long long poww(int x, long long pow) {
  if (pow == 0ll ) return 1;
  if (pow == 1ll) return x % MOD;
  long long ans = poww(x, pow >> 1) % MOD;
  ans = (ans * ans) % MOD;
  if ((pow & 1) == 1) {
    ans = (ans * x ) % MOD;
  }
  return ans % MOD;
}

ll gcd(ll x, ll y) {
  if (x == 0) return y;
  while (y) {
    x %= y;
    swap(x, y);
  }
  return x;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int q = 1;
  cin >> q;

  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  while (q-- >0) {
    int n;
    cin >> n;
    int m;
    cin >> m;
    ll p[n];
    for (int i = 0 ;i < n; ++i) cin >> p[i];
    vector<int> h1(n, 0);
    vector<int> h2(n, 0);
    vector<int> deg(n, 0);
    for (int i  =0; i < m; ++i) {
      int x, y;
      cin >> x >> y;
      --x;
      --y;
      h1[y] = (1ll * h1[y] * 31ll + x) % MOD;
      h2[y] = (1ll * h2[y] * 37ll + x) % MOD;
      deg[y]++;
    }
    vector<bool> there(n, true);
    map<pair<int,pair<int, int>>, int> grouping;
    for (int i = 0; i < n; ++i) {
      if (!deg[i]) continue;
      //assert(h1[i] || h2[i]);
      //if (!h1[i] && !h2[i]) cout << " FOUND " << i << endl;
      auto key = make_pair(deg[i], make_pair( h1[i], h2[i]));
      if (grouping.find(key) != grouping.end()) {
        int ex = grouping[key];
        there[i] = false;
        assert (there[ex]);
        p[ex] += p[i];
        p[i] = -INF;
      } else {
        grouping[key] = i;
      }
    }

    ll ans = 0ll;
    for (int i = 0; i < n; ++i) {
      if (there[i] && deg[i]) {
        ans = gcd(ans, p[i]);
      }
    }
    cout << ans << "\n";
  }
  return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;

const long long MAXVAL = (long long) 1e18 + 1;
const long long MOD = 1000000007;
const int INF = 1000000000 + 1;

long long poww(int x, long long pow) {
  if (pow == 0ll ) return 1;
  if (pow == 1ll) return x % MOD;
  long long ans = poww(x, pow >> 1) % MOD;
  ans = (ans * ans) % MOD;
  if ((pow & 1) == 1) {
    ans = (ans * x ) % MOD;
  }
  return ans % MOD;
}

ll gcd(ll x, ll y) {
  while (y) {
    x %= y;
    swap(x, y);
  }
  return x;
}

int rnd(int n) {
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int q = 1;
  cin >> q;

  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  while (q-- >0) {
    int n;
    cin >> n;
    int m;
    cin >> m;
    vector<ll> a(n, 0);
    ll p[n];
    for (int i = 0 ;i < n; ++i) cin >> p[i];
    vector<vector<int>> gr(n);
    for (int i  =0; i < m; ++i) {
      int x, y;
      cin >> x >> y;
      --x;
      --y;
      a[x] += p[y];
      gr[x].push_back(y);
    }
    ll ans = 0;
    for (int i = 0; i < n; ++i) ans = gcd(ans, a[i]);
    int time = 0;
    auto dis = uniform_int_distribution<int>(1, 3 +( n >> 1));
    auto dis2 = uniform_int_distribution<int>(0, n - 1);
    while (time < max(2 * (n + m), 1000)) {
      int pow = dis(rng);
      pow = min(pow, n);
      unordered_set<int> cur;
      for (int i  =0; i < pow; ++i) {
        int v = dis2(rng);
        for (auto next : gr[v]) {
          cur.insert(next);
        }
      }
      ll ans0 = 0;
      for (auto x : cur) {
        ans0 += p[x];
      }
      ans = gcd(ans, ans0);
      time += pow;
    }
    cout << ans << "\n";
  }
  return 0;
}


#include <bits/stdc++.h>

#define PI 3.14159265358979323846264338327950288419

using namespace std;

const int ITER = 3;
const int LOWER = 100000000;
const int UPPER = 300000000;

int pr[ITER];

int tc, n, m;
int ohash[500005];
int edges[500005][2];
long long val[500005];
int ran_num[500005];

int rand_num(int mod){
	int ran32 = (rand() % RAND_MAX) * RAND_MAX + rand() % RAND_MAX;
	return ran32 % mod;
}

bool is_prime(int num){
	if(num == 1){
		return false;
	}
	for(int i = 2; i * i <= num; i++){
		if(num % i == 0){
			return false;
		}
	}
	return true;
}

void find_random_primes(){
	for(int i = 0; i < ITER; i++){
		int cand = rand_num(UPPER - LOWER + 1) + LOWER;
		while(!is_prime(cand)){
			cand++;
		}
		pr[i] = cand;
	}
}

void input(){
	scanf("%d %d", &n, &m);
	for(int i = 1; i <= n; i++){
		scanf("%lld", &val[i]);
	}
	for(int i = 1; i <= m; i++){
		scanf("%d %d", &edges[i][0], &edges[i][1]);
	}
}

void calc_hash(int it){
	ran_num[0] = 1;
	for(int i = 1; i <= n; i++){
		ran_num[i] = (1LL * ran_num[i - 1] * 29) % pr[it];
		ohash[i] = 0;
	}
	for(int i = 1; i <= m; i++){
		ohash[edges[i][1]] = (ohash[edges[i][1]] + ran_num[edges[i][0]]) % pr[it];
	}
}

long long gcd(long long a, long long b){
	if(b == 0) return a;
	else return gcd(b, a % b);
}

void solve(){
	find_random_primes();
	input();
	long long ans = 0;
	for(int i = 0; i < ITER; i++){
		calc_hash(i);
		map<int, long long> M;
		for(int j = 1; j <= n; j++){
			if(!M.count(ohash[j])){
				M[ohash[j]] = val[j];
			}else{
				M[ohash[j]] += val[j];
			}
		}
		long long g = 0;
		for(auto it: M){
			g = gcd(g, it.second);
		}
		ans = gcd(ans, g);
	}
	printf("%lld\n", ans);
	return;
}

int main(){
	srand(time(0));
	scanf("%d", &tc);
	for(int i = 1; i <= tc; i++){
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>

#define PI 3.14159265358979323846264338327950288419

using namespace std;

const int ITER = 3;
const int LOWER = 100000000;
const int UPPER = 300000000;

int pr[ITER];

int tc, n, m;
int ohash[500005];
int edges[500005][2];
bool in_image[500005];
long long val[500005];
int ran_num[500005];

int rand_num(int mod){
	int ran32 = (rand() % RAND_MAX) * RAND_MAX + rand() % RAND_MAX;
	return ran32 % mod;
}

bool is_prime(int num){
	if(num == 1){
		return false;
	}
	for(int i = 2; i * i <= num; i++){
		if(num % i == 0){
			return false;
		}
	}
	return true;
}

void find_random_primes(){
	for(int i = 0; i < ITER; i++){
		int cand = rand_num(UPPER - LOWER + 1) + LOWER;
		while(!is_prime(cand)){
			cand++;
		}
		pr[i] = cand;
	}
}

void input(){
	scanf("%d %d", &n, &m);
	for(int i = 1; i <= n; i++){
		scanf("%lld", &val[i]);
		in_image[val[i]] = false;
	}
	for(int i = 1; i <= m; i++){
		scanf("%d %d", &edges[i][0], &edges[i][1]);
		in_image[edges[i][1]] = true;
	}
	for(int i = 1; i <= n; i++){
		if(!in_image[i]){
			// this is irrelevant
			val[i] = 0LL;
		}
	}
}

void calc_hash(int it){
	ran_num[0] = 1;
	for(int i = 1; i <= n; i++){
		ran_num[i] = (1LL * ran_num[i - 1] * 29) % pr[it];
		ohash[i] = 0;
	}
	for(int i = 1; i <= m; i++){
		ohash[edges[i][1]] = (ohash[edges[i][1]] + ran_num[edges[i][0]]) % pr[it];
	}
}

long long gcd(long long a, long long b){
	if(b == 0) return a;
	else return gcd(b, a % b);
}

void solve(){
	find_random_primes();
	input();
	long long ans = 0;
	for(int i = 0; i < ITER; i++){
		calc_hash(i);
		map<int, long long> M;
		for(int j = 1; j <= n; j++){
			if(!M.count(ohash[j])){
				M[ohash[j]] = val[j];
			}else{
				M[ohash[j]] += val[j];
			}
		}
		long long g = 0;
		for(auto it: M){
	//		printf("hash %d has sum %lld\n", it.first, it.second);
			g = gcd(g, it.second);
		}
		ans = gcd(ans, g);
	}
	printf("%lld\n", ans);
	return;
}

int main(){
	srand(time(0));
	scanf("%d", &tc);
	for(int i = 1; i <= tc; i++){
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>

#define PI 3.14159265358979323846264338327950288419

using namespace std;

const int ITER = 3;
const int LOWER = 100000000;
const int UPPER = 300000000;

int pr[ITER];

int tc, n, m;
int ohash[500005];
int edges[500005][2];
long long val[500005];
int ran_num[500005];

int rand_num(int mod){
	int ran32 = (rand() % RAND_MAX) * RAND_MAX + rand() % RAND_MAX;
	return ran32 % mod;
}

bool is_prime(int num){
	if(num == 1){
		return false;
	}
	for(int i = 2; i * i <= num; i++){
		if(num % i == 0){
			return false;
		}
	}
	return true;
}

void find_random_primes(){
	for(int i = 0; i < ITER; i++){
		int cand = rand_num(UPPER - LOWER + 1) + LOWER;
		while(!is_prime(cand)){
			cand++;
		}
		pr[i] = cand;
	}
}

void input(){
	scanf("%d %d", &n, &m);
	for(int i = 1; i <= n; i++){
		scanf("%lld", &val[i]);
	}
	for(int i = 1; i <= m; i++){
		scanf("%d %d", &edges[i][0], &edges[i][1]);
	}
}

void calc_hash(int it){
	for(int i = 1; i <= n; i++){
		ran_num[i] = rand_num(pr[it]);
		ohash[i] = 0;
	}
	for(int i = 1; i <= m; i++){
		ohash[edges[i][1]] = (ohash[edges[i][1]] + ran_num[edges[i][0]]) % pr[it];
	}
}

long long gcd(long long a, long long b){
	if(b == 0) return a;
	else return gcd(b, a % b);
}

void solve(){
	find_random_primes();
	input();
	long long ans = 0;
	for(int i = 0; i < ITER; i++){
		calc_hash(i);
		map<int, long long> M;
		for(int j = 1; j <= n; j++){
			if(!M.count(ohash[j])){
				M[ohash[j]] = val[j];
			}else{
				M[ohash[j]] += val[j];
			}
		}
		long long g = 0;
		for(auto it: M){
			g = gcd(g, it.second);
		}
		ans = gcd(ans, g);
	}
	printf("%lld\n", ans);
	return;
}

int main(){
	srand(time(0));
	scanf("%d", &tc);
	for(int i = 1; i <= tc; i++){
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>

#define PI 3.14159265358979323846264338327950288419

using namespace std;

const int ITER = 3;
const int LOWER = 100000000;
const int UPPER = 300000000;

int pr[ITER];

int tc, n, m;
int ohash[500005];
int edges[500005][2];
bool in_image[500005];
long long val[500005];
int ran_num[500005];

map<int, long long> M;

int rand_num(int mod){
	int ran32 = (rand() % RAND_MAX) * RAND_MAX + rand() % RAND_MAX;
	return ran32 % mod;
}

bool is_prime(int num){
	if(num == 1){
		return false;
	}
	for(int i = 2; i * i <= num; i++){
		if(num % i == 0){
			return false;
		}
	}
	return true;
}

void find_random_primes(){
	for(int i = 0; i < ITER; i++){
		int cand = rand_num(UPPER - LOWER + 1) + LOWER;
		while(!is_prime(cand)){
			cand++;
		}
		pr[i] = cand;
	}
}

void input(){
	scanf("%d %d", &n, &m);
	for(int i = 1; i <= n; i++){
		scanf("%lld", &val[i]);
		in_image[i] = false;
	}
	for(int i = 1; i <= m; i++){
		scanf("%d %d", &edges[i][0], &edges[i][1]);
		in_image[edges[i][1]] = true;
	}
	for(int i = 1; i <= n; i++){
		if(!in_image[i]){
			// this is irrelevant
			val[i] = 0LL;
		}
	}
}

void calc_hash(int it){
	ran_num[0] = 1;
	for(int i = 1; i <= n; i++){
		ran_num[i] = (1LL * ran_num[i - 1] * 29) % pr[it];
		ohash[i] = 0;
	}
	for(int i = 1; i <= m; i++){
		ohash[edges[i][1]] = (ohash[edges[i][1]] + ran_num[edges[i][0]]) % pr[it];
	}
}

long long gcd(long long a, long long b){
	if(b == 0) return a;
	else return gcd(b, a % b);
}

void solve(){
	find_random_primes();
	input();
	long long ans = 0;
	for(int i = 0; i < ITER; i++){

		M.clear();

		calc_hash(i);
		for(int j = 1; j <= n; j++){
			if(!M.count(ohash[j])){
				M[ohash[j]] = val[j];
			}else{
				M[ohash[j]] += val[j];
			}
		}
		long long g = 0;
		for(auto it: M){
	//		printf("hash %d has sum %lld\n", it.first, it.second);
			g = gcd(g, it.second);
		}
		ans = gcd(ans, g);
	}
	printf("%lld\n", ans);
	return;
}

int main(){
	srand(time(0));
	scanf("%d", &tc);
	for(int i = 1; i <= tc; i++){
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>

#define PI 3.14159265358979323846264338327950288419

using namespace std;

const int ITER = 3;
const int LOWER = 100000000;
const int UPPER = 300000000;

int pr[ITER];

int tc, n, m;
int ohash[500005];
int edges[500005][2];
bool in_image[500005];
long long val[500005];
int ran_num[500005];

map<int, long long> M;

int rand_num(int mod){
	int ran32 = (rand() % RAND_MAX) * RAND_MAX + rand() % RAND_MAX;
	return ran32 % mod;
}

bool is_prime(int num){
	if(num == 1){
		return false;
	}
	for(int i = 2; i * i <= num; i++){
		if(num % i == 0){
			return false;
		}
	}
	return true;
}

void find_random_primes(){
	for(int i = 0; i < ITER; i++){
		int cand = rand_num(UPPER - LOWER + 1) + LOWER;
		while(!is_prime(cand)){
			cand++;
		}
		pr[i] = cand;
	}
}

void input(){
	scanf("%d %d", &n, &m);
	for(int i = 1; i <= n; i++){
		scanf("%lld", &val[i]);
		in_image[i] = false;
	}
	for(int i = 1; i <= m; i++){
		scanf("%d %d", &edges[i][0], &edges[i][1]);
		in_image[edges[i][1]] = true;
	}
	for(int i = 1; i <= n; i++){
		if(!in_image[i]){
			// this is irrelevant
			val[i] = 0LL;
		}
	}
}

void calc_hash(int it){
	ran_num[0] = 1;
	for(int i = 1; i <= n; i++){
		ran_num[i] = (1LL * ran_num[i - 1] * 29) % pr[it];
		ohash[i] = 0;
	}
	for(int i = 1; i <= m; i++){
		ohash[edges[i][1]] = (ohash[edges[i][1]] + ran_num[edges[i][0]]) % pr[it];
	}
}

long long gcd(long long a, long long b){
	if(b == 0) return a;
	else return gcd(b, a % b);
}

void solve(){
	input();
	long long ans = 0;
	for(int i = 0; i < ITER; i++){

		M.clear();

		calc_hash(i);
		for(int j = 1; j <= n; j++){
			if(!M.count(ohash[j])){
				M[ohash[j]] = val[j];
			}else{
				M[ohash[j]] += val[j];
			}
		}
		long long g = 0;
		for(auto it: M){
	//		printf("hash %d has sum %lld\n", it.first, it.second);
			g = gcd(g, it.second);
		}
		ans = gcd(ans, g);
	}
	printf("%lld\n", ans);
	return;
}

int main(){
	srand(time(0));
	find_random_primes();
	scanf("%d", &tc);
	for(int i = 1; i <= tc; i++){
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>

#define PI 3.14159265358979323846264338327950288419

using namespace std;

const int ITER = 3;
const int LOWER = 100000000;
const int UPPER = 300000000;

int pr[ITER];

int tc, n, m;
int ohash[500005];
int edges[500005][2];
bool in_image[500005];
long long val[500005];
int ran_num[500005];

int rand_num(int mod){
	int ran32 = (rand() % RAND_MAX) * RAND_MAX + rand() % RAND_MAX;
	return ran32 % mod;
}

bool is_prime(int num){
	if(num == 1){
		return false;
	}
	for(int i = 2; i * i <= num; i++){
		if(num % i == 0){
			return false;
		}
	}
	return true;
}

void find_random_primes(){
	for(int i = 0; i < ITER; i++){
		int cand = rand_num(UPPER - LOWER + 1) + LOWER;
		while(!is_prime(cand)){
			cand++;
		}
		pr[i] = cand;
	}
}

void input(){
	scanf("%d %d", &n, &m);
	for(int i = 1; i <= n; i++){
		scanf("%lld", &val[i]);
		in_image[i] = false;
	}
	for(int i = 1; i <= m; i++){
		scanf("%d %d", &edges[i][0], &edges[i][1]);
		in_image[edges[i][1]] = true;
	}
	for(int i = 1; i <= n; i++){
		if(!in_image[i]){
			// this is irrelevant
			val[i] = 0LL;
		}
	}
}

void calc_hash(int it){
	ran_num[0] = 1;
	for(int i = 1; i <= n; i++){
		ran_num[i] = (1LL * ran_num[i - 1] * 29) % pr[it];
		ohash[i] = 0;
	}
	for(int i = 1; i <= m; i++){
		ohash[edges[i][1]] = (ohash[edges[i][1]] + ran_num[edges[i][0]]) % pr[it];
	}
}

long long gcd(long long a, long long b){
	if(b == 0) return a;
	else return gcd(b, a % b);
}

void solve(){
	find_random_primes();
	input();
	long long ans = 0;
	for(int i = 0; i < ITER; i++){
		calc_hash(i);
		map<int, long long> M;
		for(int j = 1; j <= n; j++){
			if(!M.count(ohash[j])){
				M[ohash[j]] = val[j];
			}else{
				M[ohash[j]] += val[j];
			}
		}
		long long g = 0;
		for(auto it: M){
	//		printf("hash %d has sum %lld\n", it.first, it.second);
			g = gcd(g, it.second);
		}
		ans = gcd(ans, g);
	}
	printf("%lld\n", ans);
	return;
}

int main(){
	srand(time(0));
	scanf("%d", &tc);
	for(int i = 1; i <= tc; i++){
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
#define pi 3.1415926535897932384626
using namespace std;

struct P{
  int x,y;
  bool operator < (const P &a )const{
    //if(y!=a.y)
    return y<a.y;
  };
};

int a,b,c,d,i,k,n,m,e,dx[10]={1,0,-1,0,1,1,-1,-1},dy[10]={0,1,0,-1,1,-1,1,-1};
int o[500101];
long long l[500101];
long long j[500101];

long long x,y,z,mod=1000000009;
//string r;
char r[1];

P u[1];
queue<int> q;
stack<int> s;

vector<int> v[500101];

bool as(P a,P b)
{
  return a.x>b.x;
}

unsigned long long mix(unsigned long long o){
    o+=0x9e3779b97f4a7c15;
    o=(o^(o>>30))*0xbf58476d1ce4e5b9;
    o=(o^(o>>27))*0x94d049bb133111eb;
    return o^(o>>31);
}

long long gcd(long long x,long long y)
{
  if(y==0) return x;
  return gcd(y,x%y);
}

int main()
{

  scanf("%d",&e);
  for(int tt=1;tt<=e;tt++)
  {
    map<unsigned long long,long long> p;
    unsigned long long salt = (chrono::steady_clock::now().time_since_epoch().count());
    scanf("%d %d",&a,&b);
    for(int t=1;t<=a;t++)
      scanf("%lld",&l[t]);
    for(int t=1;t<=b;t++)
    {
      scanf("%d %d",&n,&m);
      //c=max(c,n);
      v[m].push_back(n);
    }

    for(int t=1;t<=a;t++)
    {
      unsigned long long x=0;

      for(int h=0;h<v[t].size();h++)
      x^=mix(v[t][h]^salt);
      p[x]+=l[t];
    }
    x=0;
    for(int t=1;t<=a;t++)
    {

      unsigned long long z=0;
      int w=0;
      for(int h=0;h<v[t].size();h++)
      {
      z^=mix(v[t][h]^salt);
      w=1;
      }
      if(w) x=gcd(x,p[z]);

    }
    printf("%lld\n",x);
    for(int t=1;t<=a;t++)
      v[t].clear();
  }
}


#include<bits/stdc++.h>
#define pi 3.1415926535897932384626
using namespace std;

struct P{
  int x,y;
  bool operator < (const P &a )const{
    //if(y!=a.y)
    return y<a.y;
  };
};

int a,b,c,d,i,k,n,m,e,dx[10]={1,0,-1,0,1,1,-1,-1},dy[10]={0,1,0,-1,1,-1,1,-1};
int o[500101];
long long l[500101];
long long j[500101];

long long x,y,z,mod=1000000009;
//string r;
char r[1];

P u[1];
queue<int> q;
stack<int> s;

vector<int> v[500101];

bool as(P a,P b)
{
  return a.x>b.x;
}

long long gcd(long long x,long long y)
{
  if(y==0) return x;
  return gcd(y,x%y);
}

int main()
{

  scanf("%d",&e);
  for(int tt=1;tt<=e;tt++)
  {
    map<vector<int>,long long> p;
    scanf("%d %d",&a,&b);
    for(int t=1;t<=a;t++)
      scanf("%lld",&l[t]);
    for(int t=1;t<=b;t++)
    {
      scanf("%d %d",&n,&m);
      //c=max(c,n);
      v[m].push_back(n);
    }

    for(int t=1;t<=a;t++)
    {
      vector<int> v1;
      sort(v[t].begin(),v[t].end());
      for(int h=0;h<v[t].size();h++)
      v1.push_back(v[t][h]);
      p[v1]+=l[t];
    }
    x=0;
    for(int t=1;t<=a;t++)
    {
      vector<int> v1;

      int w=0;
      for(int h=0;h<v[t].size();h++)
      {
      v1.push_back(v[t][h]);
      w=1;
      }
      if(w) x=gcd(x,p[v1]);

    }
    printf("%lld\n",x);
    for(int t=1;t<=a;t++)
      v[t].clear();
  }
}


#include<bits/stdc++.h>
#define pi 3.1415926535897932384626
using namespace std;

struct P{
  int x,y;
  bool operator < (const P &a )const{
    //if(y!=a.y)
    return y<a.y;
  };
};

int a,b,c,d,i,k,n,m,e,dx[10]={1,0,-1,0,1,1,-1,-1},dy[10]={0,1,0,-1,1,-1,1,-1};
int o[500101];
long long l[500101];
long long j[500101];

long long x,y,z,mod=1000000009;
//string r;
char r[1];

P u[1];
queue<int> q;
stack<int> s;

vector<int> v[500101];

bool as(P a,P b)
{
  return a.x>b.x;
}

long long gcd(long long x,long long y)
{
  if(y==0) return x;
  return gcd(y,x%y);
}

int main()
{

  scanf("%d",&e);
  for(int tt=1;tt<=e;tt++)
  {
    map<vector<bool>,long long> p;
    scanf("%d %d",&a,&b);
    for(int t=1;t<=a;t++)
      scanf("%lld",&l[t]);
    for(int t=1;t<=b;t++)
    {
      scanf("%d %d",&n,&m);
      v[m].push_back(n);
    }
    vector<bool> v1;
    v1.resize(a);
    for(int t=1;t<=a;t++)
    {
      for(int h=0;h<v[t].size();h++)
      v1[v[t][h]-1]=true;
      p[v1]+=l[t];
      for(int h=0;h<v[t].size();h++)
      v1[v[t][h]-1]=false;
    }
    x=0;
    for(int t=1;t<=a;t++)
    {
      for(int h=0;h<v[t].size();h++)
      v1[v[t][h]-1]=true;
      x=gcd(x,p[v1]);
      for(int h=0;h<v[t].size();h++)
      v1[v[t][h]-1]=false;
    }
    printf("%lld\n",x);
    for(int t=1;t<=a;t++)
      v[t].clear();
  }
}


#include<bits/stdc++.h>
#define pi 3.1415926535897932384626
using namespace std;

struct P{
  int x,y;
  bool operator < (const P &a )const{
    //if(y!=a.y)
    return y<a.y;
  };
};

int a,b,c,d,i,k,n,m,e,dx[10]={1,0,-1,0,1,1,-1,-1},dy[10]={0,1,0,-1,1,-1,1,-1};
int o[500101];
int l[500101];
long long j[500101];

long long x,y,z,mod=1000000009;
//string r;
char r[1];

P u[1];
queue<int> q;
stack<int> s;
map<long long,long long> p;
vector<int> v;

bool as(P a,P b)
{
  return a.x>b.x;
}

long long gcd(long long x,long long y)
{
  if(y==0) return x;
  return gcd(y,x%y);
}

int main()
{
  o[0]=1;
  for(int t=1;t<=500000;t++)
    o[t]=o[t-1]*2%mod;
  scanf("%d",&e);
  for(int tt=1;tt<=e;tt++)
  {
    p.clear();
    scanf("%d %d",&a,&b);
    for(int t=1;t<=a;t++)
      scanf("%d",&l[t]);
    for(int t=1;t<=b;t++)
    {
      scanf("%d %d",&n,&m);
      q.push(m);
      j[m]+=o[n-1];
      j[m]%=mod;
    }
    for(int t=1;t<=a;t++)
      p[j[t]]+=l[t];
    x=0;
    for(int t=1;t<=a;t++)
      x=gcd(x,p[j[t]]);
    printf("%lld\n",x);
    for(;q.size();q.pop())
      j[q.front()]=0;
  }
}


#include<bits/stdc++.h>
#define pi 3.1415926535897932384626
using namespace std;

struct P{
  int x,y;
  bool operator < (const P &a )const{
    //if(y!=a.y)
    return y<a.y;
  };
};

int a,b,c,d,i,k,n,m,e,dx[10]={1,0,-1,0,1,1,-1,-1},dy[10]={0,1,0,-1,1,-1,1,-1};
int o[500101];
int l[500101];
long long j[500101];

long long x,y,z,mod=1000000009;
//string r;
char r[1];

P u[1];
queue<int> q;
stack<int> s;
map<long long,long long> p;
vector<int> v;

bool as(P a,P b)
{
  return a.x>b.x;
}

long long gcd(long long x,long long y)
{
  if(y==0) return x;
  return gcd(y,x%y);
}

int main()
{
  o[0]=1;
  for(int t=1;t<=500000;t++)
    o[t]=o[t-1]*2%mod;
  scanf("%d",&e);
  for(int tt=1;tt<=e;tt++)
  {
    p.clear();
    scanf("%d %d",&a,&b);
    for(int t=1;t<=a;t++)
      scanf("%d",&l[t]);
    for(int t=1;t<=b;t++)
    {
      scanf("%d %d",&n,&m);
      q.push(m);
      j[m]+=o[n-1];
    }
    for(int t=1;t<=a;t++)
      p[j[t]]+=l[t];
    x=0;
    for(int t=1;t<=a;t++)
      x=gcd(x,p[j[t]]);
    printf("%lld\n",x);
    for(;q.size();q.pop())
      j[q.front()]=0;
  }
}


#include<bits/stdc++.h>
#define pi 3.1415926535897932384626
using namespace std;

struct P{
  int x,y;
  bool operator < (const P &a )const{
    //if(y!=a.y)
    return y<a.y;
  };
};

int a,b,c,d,i,k,n,m,e,dx[10]={1,0,-1,0,1,1,-1,-1},dy[10]={0,1,0,-1,1,-1,1,-1};
int o[500101];
long long l[500101];
long long j[500101];

long long x,y,z,mod=1000000009*1000000007;
//string r;
char r[1];

P u[1];
queue<int> q;
stack<int> s;
map<long long,long long> p;
vector<int> v;

bool as(P a,P b)
{
  return a.x>b.x;
}

long long gcd(long long x,long long y)
{
  if(y==0) return x;
  return gcd(y,x%y);
}

int main()
{
  o[0]=1;
  for(int t=1;t<=500000;t++)
    o[t]=o[t-1]*2%mod;
  scanf("%d",&e);
  for(int tt=1;tt<=e;tt++)
  {
    p.clear();
    scanf("%d %d",&a,&b);
    for(int t=1;t<=a;t++)
      scanf("%lld",&l[t]);
    for(int t=1;t<=b;t++)
    {
      scanf("%d %d",&n,&m);
      q.push(m);
      j[m]+=o[n-1];
      j[m]%=mod;
    }
    for(int t=1;t<=a;t++)
      p[j[t]]+=l[t];
    x=0;
    for(int t=1;t<=a;t++)
    if(j[t])
      x=gcd(x,p[j[t]]);
    printf("%lld\n",x);
    for(;q.size();q.pop())
      j[q.front()]=0;
  }
}


#include<bits/stdc++.h>
#define pi 3.1415926535897932384626
using namespace std;

struct P{
  int x,y;
  bool operator < (const P &a )const{
    //if(y!=a.y)
    return y<a.y;
  };
};

int a,b,c,d,i,k,n,m,e,dx[10]={1,0,-1,0,1,1,-1,-1},dy[10]={0,1,0,-1,1,-1,1,-1};
int o[500101];
long long l[500101];
long long j[500101];

long long x,y,z,mod=1000000009;
//string r;
char r[1];

P u[1];
queue<int> q;
stack<int> s;

vector<int> v[500101];

bool as(P a,P b)
{
  return a.x>b.x;
}

long long gcd(long long x,long long y)
{
  if(y==0) return x;
  return gcd(y,x%y);
}

int main()
{

  scanf("%d",&e);
  for(int tt=1;tt<=e;tt++)
  {
    map<vector<bool>,long long> p;
    scanf("%d %d",&a,&b);
    for(int t=1;t<=a;t++)
      scanf("%lld",&l[t]);
    for(int t=1;t<=b;t++)
    {
      scanf("%d %d",&n,&m);
      //c=max(c,n);
      v[m].push_back(n);
    }

    for(int t=1;t<=a;t++)
    {
      vector<bool> v1;
      v1.resize(a);
      for(int h=0;h<v[t].size();h++)
      v1[v[t][h]-1]=true;
      p[v1]+=l[t];
    }
    x=0;
    for(int t=1;t<=a;t++)
    {
      vector<bool> v1;
      v1.resize(a);
      int w=0;
      for(int h=0;h<v[t].size();h++)
      {
      v1[v[t][h]-1]=true;
      w=1;
      }
      if(w) x=gcd(x,p[v1]);
      for(int h=0;h<v[t].size();h++)
      v1[v[t][h]-1]=false;
    }
    printf("%lld\n",x);
    for(int t=1;t<=a;t++)
      v[t].clear();
  }
}


#include<bits/stdc++.h>
#define pi 3.1415926535897932384626
using namespace std;

struct P{
  int x,y;
  bool operator < (const P &a )const{
    //if(y!=a.y)
    return y<a.y;
  };
};

int a,b,c,d,i,k,n,m,e,dx[10]={1,0,-1,0,1,1,-1,-1},dy[10]={0,1,0,-1,1,-1,1,-1};
int o[500101];
long long l[500101];
long long j[500101];

long long x,y,z,mod=1000000009;
//string r;
char r[1];

P u[1];
queue<int> q;
stack<int> s;
map<vector<bool>,long long> p;
vector<int> v[500101];

bool as(P a,P b)
{
  return a.x>b.x;
}

long long gcd(long long x,long long y)
{
  if(y==0) return x;
  return gcd(y,x%y);
}

int main()
{

  scanf("%d",&e);
  for(int tt=1;tt<=e;tt++)
  {
    p.clear();
    scanf("%d %d",&a,&b);
    for(int t=1;t<=a;t++)
      scanf("%lld",&l[t]);
    for(int t=1;t<=b;t++)
    {
      scanf("%d %d",&n,&m);
      v[m].push_back(n);
    }
    vector<bool> v1;
    v1.resize(a);
    for(int t=1;t<=a;t++)
    {
      for(int h=0;h<v[t].size();h++)
      v1[v[t][h]-1]=true;
      p[v1]+=l[t];
      for(int h=0;h<v[t].size();h++)
      v1[v[t][h]-1]=false;
    }
    x=0;
    for(int t=1;t<=a;t++)
    {
      for(int h=0;h<v[t].size();h++)
      v1[v[t][h]-1]=true;
      x=gcd(x,p[v1]);
      for(int h=0;h<v[t].size();h++)
      v1[v[t][h]-1]=false;
    }
    printf("%lld\n",x);
    for(int t=1;t<=a;t++)
      v[t].clear();
  }
}


#include<bits/stdc++.h>
#define pi 3.1415926535897932384626
using namespace std;

struct P{
  int x,y;
  bool operator < (const P &a )const{
    //if(y!=a.y)
    return y<a.y;
  };
};

int a,b,c,d,i,k,n,m,e,dx[10]={1,0,-1,0,1,1,-1,-1},dy[10]={0,1,0,-1,1,-1,1,-1};
int o[500101];
long long l[500101];
long long j[500101];

long long x,y,z,mod=1000000009;
//string r;
char r[1];

P u[1];
queue<int> q;
stack<int> s;

vector<int> v[500101];

bool as(P a,P b)
{
  return a.x>b.x;
}

long long gcd(long long x,long long y)
{
  if(y==0) return x;
  return gcd(y,x%y);
}

int main()
{

  scanf("%d",&e);
  for(int tt=1;tt<=e;tt++)
  {
    map<vector<bool>,long long> p;
    scanf("%d %d",&a,&b);
    for(int t=1;t<=a;t++)
      scanf("%lld",&l[t]);
    for(int t=1;t<=b;t++)
    {
      scanf("%d %d",&n,&m);
      //c=max(c,n);
      v[m].push_back(n);
    }
    vector<bool> v1;
    v1.resize(a);
    for(int t=1;t<=a;t++)
    {
      for(int h=0;h<v[t].size();h++)
      v1[v[t][h]-1]=true;
      p[v1]+=l[t];
      for(int h=0;h<v[t].size();h++)
      v1[v[t][h]-1]=false;
    }
    x=0;
    for(int t=1;t<=a;t++)
    {
      int w=0;
      for(int h=0;h<v[t].size();h++)
      {
      v1[v[t][h]-1]=true;
      w=1;
      }
      if(w) x=gcd(x,p[v1]);
      for(int h=0;h<v[t].size();h++)
      v1[v[t][h]-1]=false;
    }
    printf("%lld\n",x);
    for(int t=1;t<=a;t++)
      v[t].clear();
  }
}


#include<bits/stdc++.h>
#define pi 3.1415926535897932384626
using namespace std;

struct P{
  int x,y;
  bool operator < (const P &a )const{
    //if(y!=a.y)
    return y<a.y;
  };
};

int a,b,c,d,i,k,n,m,e,dx[10]={1,0,-1,0,1,1,-1,-1},dy[10]={0,1,0,-1,1,-1,1,-1};
int o[500101];
int l[500101];
long long j[500101];

long long x,y,z,mod=1000000009;
//string r;
char r[1];

P u[1];
queue<int> q;
stack<int> s;
map<vector<bool>,long long> p;
vector<int> v[500101];

bool as(P a,P b)
{
  return a.x>b.x;
}

long long gcd(long long x,long long y)
{
  if(y==0) return x;
  return gcd(y,x%y);
}

int main()
{

  scanf("%d",&e);
  for(int tt=1;tt<=e;tt++)
  {
    p.clear();
    scanf("%d %d",&a,&b);
    for(int t=1;t<=a;t++)
      scanf("%d",&l[t]);
    for(int t=1;t<=b;t++)
    {
      scanf("%d %d",&n,&m);
      v[m].push_back(n);
    }
    vector<bool> v1;
    v1.resize(a);
    for(int t=1;t<=a;t++)
    {
      for(int h=0;h<v[t].size();h++)
      v1[v[t][h]-1]=true;
      p[v1]+=l[t];
      for(int h=0;h<v[t].size();h++)
      v1[v[t][h]-1]=false;
    }
    x=0;
    for(int t=1;t<=a;t++)
    {
      for(int h=0;h<v[t].size();h++)
      v1[v[t][h]-1]=true;
      x=gcd(x,p[v1]);
      for(int h=0;h<v[t].size();h++)
      v1[v[t][h]-1]=false;
    }
    printf("%lld\n",x);
    for(int t=1;t<=a;t++)
      v[t].clear();
  }
}


#include<bits/stdc++.h>
#define pi 3.1415926535897932384626
using namespace std;

struct P{
  int x,y;
  bool operator < (const P &a )const{
    //if(y!=a.y)
    return y<a.y;
  };
};

int a,b,c,d,i,k,n,m,e,dx[10]={1,0,-1,0,1,1,-1,-1},dy[10]={0,1,0,-1,1,-1,1,-1};
long long o[500101];
long long l[500101];
long long j[500101];

long long x,y,z,mod=1000000009*1000000007;
//string r;
char r[1];

P u[1];
queue<int> q;
stack<int> s;
map<long long,long long> p;
vector<int> v;

bool as(P a,P b)
{
  return a.x>b.x;
}

long long gcd(long long x,long long y)
{
  if(y==0) return x;
  return gcd(y,x%y);
}

int main()
{
  o[0]=1;
  for(int t=1;t<=500000;t++)
    o[t]=o[t-1]*2%mod;
  scanf("%d",&e);
  for(int tt=1;tt<=e;tt++)
  {
    p.clear();
    scanf("%d %d",&a,&b);
    for(int t=1;t<=a;t++)
      scanf("%lld",&l[t]);
    for(int t=1;t<=b;t++)
    {
      scanf("%d %d",&n,&m);
      q.push(m);
      j[m]+=o[n-1];
      j[m]%=mod;
    }
    for(int t=1;t<=a;t++)
      p[j[t]]+=l[t];
    x=0;
    for(int t=1;t<=a;t++)
    if(j[t])
      x=gcd(x,p[j[t]]);
    printf("%lld\n",x);
    for(;q.size();q.pop())
      j[q.front()]=0;
  }
}


#include<bits/stdc++.h>
#define pi 3.1415926535897932384626
using namespace std;

struct P{
  int x,y;
  bool operator < (const P &a )const{
    //if(y!=a.y)
    return y<a.y;
  };
};

int a,b,c,d,i,k,n,m,e,dx[10]={1,0,-1,0,1,1,-1,-1},dy[10]={0,1,0,-1,1,-1,1,-1};
int o[500101];
long long l[500101];
long long j[500101];

long long x,y,z,mod=1000000009;
//string r;
char r[1];

P u[1];
queue<int> q;
stack<int> s;
map<long long,long long> p;
vector<int> v;

bool as(P a,P b)
{
  return a.x>b.x;
}

long long gcd(long long x,long long y)
{
  if(y==0) return x;
  return gcd(y,x%y);
}

int main()
{
  o[0]=1;
  for(int t=1;t<=500000;t++)
    o[t]=o[t-1]*2%mod;
  scanf("%d",&e);
  for(int tt=1;tt<=e;tt++)
  {
    p.clear();
    scanf("%d %d",&a,&b);
    for(int t=1;t<=a;t++)
      scanf("%lld",&l[t]);
    for(int t=1;t<=b;t++)
    {
      scanf("%d %d",&n,&m);
      q.push(m);
      j[m]+=o[n-1];
      j[m]%=mod;
    }
    for(int t=1;t<=a;t++)
      p[j[t]]+=l[t];
    x=0;
    for(int t=1;t<=a;t++)
    if(j[t])
      x=gcd(x,p[j[t]]);
    printf("%lld\n",x);
    for(;q.size();q.pop())
      j[q.front()]=0;
  }
}


#include<bits/stdc++.h>
#define pi 3.1415926535897932384626
using namespace std;

struct P{
  int x,y;
  bool operator < (const P &a )const{
    //if(y!=a.y)
    return y<a.y;
  };
};

int a,b,c,d,i,k,n,m,e,dx[10]={1,0,-1,0,1,1,-1,-1},dy[10]={0,1,0,-1,1,-1,1,-1};
int o[500101];
long long l[500101];
long long j[500101];

long long x,y,z,mod=(long long)1000000009*1000000007;
//string r;
char r[1];

P u[1];
queue<int> q;
stack<int> s;
map<long long,long long> p;
vector<int> v;

bool as(P a,P b)
{
  return a.x>b.x;
}

long long gcd(long long x,long long y)
{
  if(y==0) return x;
  return gcd(y,x%y);
}

int main()
{
  o[0]=1;
  for(int t=1;t<=500000;t++)
    o[t]=o[t-1]*2%mod;
  scanf("%d",&e);
  for(int tt=1;tt<=e;tt++)
  {
    p.clear();
    scanf("%d %d",&a,&b);
    for(int t=1;t<=a;t++)
      scanf("%lld",&l[t]);
    for(int t=1;t<=b;t++)
    {
      scanf("%d %d",&n,&m);
      q.push(m);
      j[m]+=o[n-1];
      j[m]%=mod;
    }
    for(int t=1;t<=a;t++)
      p[j[t]]+=l[t];
    x=0;
    for(int t=1;t<=a;t++)
    if(j[t])
      x=gcd(x,p[j[t]]);
    printf("%lld\n",x);
    for(;q.size();q.pop())
      j[q.front()]=0;
  }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,a,b) for(int i=(a),_end_=(b);i<=_end_;i++)
#define DREP(i,a,b) for(int i=(a),_end_=(b);i>=_end_;i--)
#define EREP(i,u) for(int i=start[u];i;i=e[i].next)
#define fi first
#define se second
#define mkr(a,b) make_pair(a,b)
#define SZ(A) ((int)A.size())
template<class T>inline void chkmin(T &a,T b){ if(a>b)a=b;}
template<class T>inline void chkmax(T &a,T b){ if(a<b)a=b;}
inline int read()
{
	int s=0,f=1; char ch=getchar();
	while(!isdigit(ch) && ch!='-')ch=getchar();
	if(ch=='-')ch=getchar(),f=-1;
	while(isdigit(ch))s=s*10+ch-'0',ch=getchar();
	return ~f?s:-s;
}

const ll maxn=5e5+20;

ll c[maxn];
int n,m;
vector<int>ed[maxn];
int tot;
set<int>s[maxn];
int vis[maxn],id[maxn];
int Vis[maxn],Tim,ID[maxn];

inline void init()
{
	n=read();m=read();
	REP(i,1,n)scanf("%lld",c+i),ed[i].clear();
	REP(i,1,tot)s[i].clear(); tot=0;
	REP(i,1,m)
	{
		int u=read(),v=read();
		ed[u].push_back(v);
	}
	tot=1;
	REP(i,1,n)s[1].insert(i),id[i]=1,Vis[i]=0,ID[i]=0,vis[i]=0;
	Tim=0;
	REP(i,1,n)
	{
		++Tim;
		for(int j:ed[i])
		{
			int x=id[j];
			s[x].erase(j);
			vis[j]=x;
		}
		for(int j:ed[i])
		{
			if(Vis[vis[j]]==Tim)
			{

			}else Vis[vis[j]]=Tim,ID[vis[j]]=++tot;
			id[j]=ID[vis[j]];
			s[ID[vis[j]]].insert(j);
		}
	}
}

inline void doing()
{
	ll ans=0;
	REP(i,1,tot)
	{
		ll sum=0;
		for(ll x:s[i])sum+=c[x];//,cerr<<x<<" ";
		//cerr<<sum<<endl;
		ans=__gcd(ans,sum);
	}
	printf("%lld\n",ans);
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("c.in","r",stdin);
	freopen("c.out","w",stdout);
#endif
	int t=read();
	while(t--)
	{
		init();
		doing();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,a,b) for(int i=(a),_end_=(b);i<=_end_;i++)
#define DREP(i,a,b) for(int i=(a),_end_=(b);i>=_end_;i--)
#define EREP(i,u) for(int i=start[u];i;i=e[i].next)
#define fi first
#define se second
#define mkr(a,b) make_pair(a,b)
#define SZ(A) ((int)A.size())
template<class T>inline void chkmin(T &a,T b){ if(a>b)a=b;}
template<class T>inline void chkmax(T &a,T b){ if(a<b)a=b;}
inline int read()
{
	int s=0,f=1; char ch=getchar();
	while(!isdigit(ch) && ch!='-')ch=getchar();
	if(ch=='-')ch=getchar(),f=-1;
	while(isdigit(ch))s=s*10+ch-'0',ch=getchar();
	return ~f?s:-s;
}

const ll maxn=5e5+20;

ll c[maxn];
int n,m,in[maxn];
vector<int>ed[maxn];
int tot;
set<int>s[maxn];
int vis[maxn],id[maxn];
int Vis[maxn],Tim,ID[maxn];

inline void init()
{
	n=read();m=read();
	REP(i,1,n)scanf("%lld",c+i),ed[i].clear(),in[i]=0;
	REP(i,1,tot)s[i].clear(); tot=0;
	REP(i,1,m)
	{
		int u=read(),v=read();
		ed[u].push_back(v);in[v]++;
	}
	tot=1;
	REP(i,1,n)s[1].insert(i),id[i]=1,Vis[i]=0,ID[i]=0,vis[i]=0;
	Tim=0;
	REP(i,1,n)
	{
		++Tim;
		for(int j:ed[i])
		{
			int x=id[j];
			s[x].erase(j);
			vis[j]=x;
		}
		for(int j:ed[i])
		{
			if(Vis[vis[j]]==Tim)
			{

			}else Vis[vis[j]]=Tim,ID[vis[j]]=++tot;
			id[j]=ID[vis[j]];
			s[ID[vis[j]]].insert(j);
		}
	}
}

inline void doing()
{
	ll ans=0;
	REP(i,1,tot)if(!s[i].empty())
	{
		ll sum=0;
		for(ll x:s[i])sum+=c[x];//,cerr<<x<<" ";
		//cerr<<sum<<endl;
		if(in[*s[i].begin()])ans=__gcd(ans,sum);
	}
	printf("%lld\n",ans);
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("c.in","r",stdin);
	freopen("c.out","w",stdout);
#endif
	int t=read();
	while(t--)
	{
		init();
		doing();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,a,b) for(int i=(a),_end_=(b);i<=_end_;i++)
#define DREP(i,a,b) for(int i=(a),_end_=(b);i>=_end_;i--)
#define EREP(i,u) for(int i=start[u];i;i=e[i].next)
#define fi first
#define se second
#define mkr(a,b) make_pair(a,b)
#define SZ(A) ((int)A.size())
template<class T>inline void chkmin(T &a,T b){ if(a>b)a=b;}
template<class T>inline void chkmax(T &a,T b){ if(a<b)a=b;}
inline int read()
{
	int s=0,f=1; char ch=getchar();
	while(!isdigit(ch) && ch!='-')ch=getchar();
	if(ch=='-')ch=getchar(),f=-1;
	while(isdigit(ch))s=s*10+ch-'0',ch=getchar();
	return ~f?s:-s;
}

const ll maxn=1e6+20;

ll c[maxn];
int n,m,in[maxn];
vector<int>ed[maxn];
int tot;
set<int>s[maxn];
int vis[maxn],id[maxn];
int Vis[maxn],Tim,ID[maxn];

inline void init()
{
	n=read();m=read();
	REP(i,1,n)scanf("%lld",c+i),ed[i].clear(),in[i]=0;
	REP(i,1,tot)s[i].clear(); tot=0;
	REP(i,1,m)
	{
		int u=read(),v=read();
		ed[u].push_back(v);in[v]++;
	}
	tot=1;
	REP(i,1,n)s[1].insert(i),id[i]=1,Vis[i]=0,ID[i]=0,vis[i]=0;
	Tim=0;
	REP(i,1,n)
	{
		++Tim;
		for(int j:ed[i])
		{
			int x=id[j];
			s[x].erase(j);
			vis[j]=x;
		}
		for(int j:ed[i])
		{
			if(Vis[vis[j]]==Tim)
			{

			}else Vis[vis[j]]=Tim,ID[vis[j]]=++tot;
			id[j]=ID[vis[j]];
			s[ID[vis[j]]].insert(j);
		}
	}
}

inline void doing()
{
	ll ans=0;
	REP(i,1,tot)if(!s[i].empty())
	{
		ll sum=0;
		for(ll x:s[i])sum+=c[x];//,cerr<<x<<" ";
		//cerr<<sum<<endl;
		if(in[*s[i].begin()])ans=__gcd(ans,sum);
	}
	printf("%lld\n",ans);
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("c.in","r",stdin);
	freopen("c.out","w",stdout);
#endif
	int t=read();
	while(t--)
	{
		init();
		doing();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,a,b) for(int i=(a),_end_=(b);i<=_end_;i++)
#define DREP(i,a,b) for(int i=(a),_end_=(b);i>=_end_;i--)
#define EREP(i,u) for(int i=start[u];i;i=e[i].next)
#define fi first
#define se second
#define mkr(a,b) make_pair(a,b)
#define SZ(A) ((int)A.size())
template<class T>inline void chkmin(T &a,T b){ if(a>b)a=b;}
template<class T>inline void chkmax(T &a,T b){ if(a<b)a=b;}
inline int read()
{
	int s=0,f=1; char ch=getchar();
	while(!isdigit(ch) && ch!='-')ch=getchar();
	if(ch=='-')ch=getchar(),f=-1;
	while(isdigit(ch))s=s*10+ch-'0',ch=getchar();
	return ~f?s:-s;
}

const ll maxn=2e6+20;

ll c[maxn];
int n,m,in[maxn];
vector<int>ed[maxn];
int tot;
set<int>s[maxn];
int vis[maxn],id[maxn];
int Vis[maxn],Tim,ID[maxn];

inline void init()
{
	n=read();m=read();
	REP(i,1,n)scanf("%lld",c+i),ed[i].clear(),in[i]=0;
	   REP(i,1,tot)Vis[i]=ID[i]=vis[i]=0;
	REP(i,1,tot)s[i].clear(); tot=0;
	REP(i,1,m)
	{
		int u=read(),v=read();
		ed[u].push_back(v);in[v]++;
	}
	tot=1;
	REP(i,1,n)s[1].insert(i),id[i]=1,Vis[i]=0,ID[i]=0,vis[i]=0;
	Tim=0;
	REP(i,1,n)
	{
		++Tim;
		for(int j:ed[i])
		{
			int x=id[j];
			s[x].erase(j);
			vis[j]=x;
		}
		for(int j:ed[i])
		{
			if(Vis[vis[j]]==Tim)
			{

			}else Vis[vis[j]]=Tim,ID[vis[j]]=++tot;
			id[j]=ID[vis[j]];
			s[ID[vis[j]]].insert(j);
		}
	}
}

inline void doing()
{
	ll ans=0;
	REP(i,1,tot)if(!s[i].empty())
	{
		ll sum=0;
		for(ll x:s[i])sum+=c[x];//,cerr<<x<<" ";
		//cerr<<sum<<endl;
		if(in[*s[i].begin()])ans=__gcd(ans,sum);
	}
	printf("%lld\n",ans);
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("c.in","r",stdin);
	freopen("c.out","w",stdout);
#endif
	int t=read();
	while(t--)
	{
		init();
		doing();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,a,b) for(int i=(a),_end_=(b);i<=_end_;i++)
#define DREP(i,a,b) for(int i=(a),_end_=(b);i>=_end_;i--)
#define EREP(i,u) for(int i=start[u];i;i=e[i].next)
#define fi first
#define se second
#define mkr(a,b) make_pair(a,b)
#define SZ(A) ((int)A.size())
template<class T>inline void chkmin(T &a,T b){ if(a>b)a=b;}
template<class T>inline void chkmax(T &a,T b){ if(a<b)a=b;}
inline int read()
{
	int s=0,f=1; char ch=getchar();
	while(!isdigit(ch) && ch!='-')ch=getchar();
	if(ch=='-')ch=getchar(),f=-1;
	while(isdigit(ch))s=s*10+ch-'0',ch=getchar();
	return ~f?s:-s;
}

const ll maxn=1e6+20;

ll c[maxn];
int n,m,in[maxn];
vector<int>ed[maxn];
int tot;
set<int>s[maxn];
int vis[maxn],id[maxn];
int Vis[maxn],Tim,ID[maxn];

inline void init()
{
	n=read();m=read();
	REP(i,1,n)scanf("%lld",c+i),ed[i].clear(),in[i]=0;
	REP(i,1,tot)s[i].clear(); tot=0;
	REP(i,1,m)
	{
		int u=read(),v=read();
		ed[u].push_back(v);in[v]++;
	}
	tot=1;
	REP(i,1,n)s[1].insert(i),id[i]=1,Vis[i]=0,ID[i]=0,vis[i]=0;
	   REP(i,1,tot)Vis[i]=ID[i]=vis[i]=0;
	Tim=0;
	REP(i,1,n)
	{
		++Tim;
		for(int j:ed[i])
		{
			int x=id[j];
			s[x].erase(j);
			vis[j]=x;
		}
		for(int j:ed[i])
		{
			if(Vis[vis[j]]==Tim)
			{

			}else Vis[vis[j]]=Tim,ID[vis[j]]=++tot;
			id[j]=ID[vis[j]];
			s[ID[vis[j]]].insert(j);
		}
	}
}

inline void doing()
{
	ll ans=0;
	REP(i,1,tot)if(!s[i].empty())
	{
		ll sum=0;
		for(ll x:s[i])sum+=c[x];//,cerr<<x<<" ";
		//cerr<<sum<<endl;
		if(in[*s[i].begin()])ans=__gcd(ans,sum);
	}
	printf("%lld\n",ans);
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("c.in","r",stdin);
	freopen("c.out","w",stdout);
#endif
	int t=read();
	while(t--)
	{
		init();
		doing();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,a,b) for(int i=(a),_end_=(b);i<=_end_;i++)
#define DREP(i,a,b) for(int i=(a),_end_=(b);i>=_end_;i--)
#define EREP(i,u) for(int i=start[u];i;i=e[i].next)
#define fi first
#define se second
#define mkr(a,b) make_pair(a,b)
#define SZ(A) ((int)A.size())
template<class T>inline void chkmin(T &a,T b){ if(a>b)a=b;}
template<class T>inline void chkmax(T &a,T b){ if(a<b)a=b;}
inline int read()
{
	int s=0,f=1; char ch=getchar();
	while(!isdigit(ch) && ch!='-')ch=getchar();
	if(ch=='-')ch=getchar(),f=-1;
	while(isdigit(ch))s=s*10+ch-'0',ch=getchar();
	return ~f?s:-s;
}

const ll maxn=2e6+20;

ll c[maxn];
int n,m,in[maxn];
vector<int>ed[maxn];
int tot;
set<int>s[maxn];
int vis[maxn],id[maxn];
int Vis[maxn],Tim,ID[maxn];

inline void init()
{
	n=read();m=read();
	REP(i,1,n)scanf("%lld",c+i),ed[i].clear(),in[i]=0;
	REP(i,1,tot)s[i].clear(); tot=0;
	REP(i,1,m)
	{
		int u=read(),v=read();
		ed[u].push_back(v);in[v]++;
	}
	tot=1;
	REP(i,1,n)s[1].insert(i),id[i]=1,Vis[i]=0,ID[i]=0,vis[i]=0;
	   REP(i,1,tot)Vis[i]=ID[i]=vis[i]=0;
	Tim=0;
	REP(i,1,n)
	{
		++Tim;
		for(int j:ed[i])
		{
			int x=id[j];
			s[x].erase(j);
			vis[j]=x;
		}
		for(int j:ed[i])
		{
			if(Vis[vis[j]]==Tim)
			{

			}else Vis[vis[j]]=Tim,ID[vis[j]]=++tot;
			id[j]=ID[vis[j]];
			s[ID[vis[j]]].insert(j);
		}
	}
}

inline void doing()
{
	ll ans=0;
	REP(i,1,tot)if(!s[i].empty())
	{
		ll sum=0;
		for(ll x:s[i])sum+=c[x];//,cerr<<x<<" ";
		//cerr<<sum<<endl;
		if(in[*s[i].begin()])ans=__gcd(ans,sum);
	}
	printf("%lld\n",ans);
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("c.in","r",stdin);
	freopen("c.out","w",stdout);
#endif
	int t=read();
	while(t--)
	{
		init();
		doing();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,a,b) for(int i=(a),_end_=(b);i<=_end_;i++)
#define DREP(i,a,b) for(int i=(a),_end_=(b);i>=_end_;i--)
#define EREP(i,u) for(int i=start[u];i;i=e[i].next)
#define fi first
#define se second
#define mkr(a,b) make_pair(a,b)
#define SZ(A) ((int)A.size())
template<class T>inline void chkmin(T &a,T b){ if(a>b)a=b;}
template<class T>inline void chkmax(T &a,T b){ if(a<b)a=b;}
inline int read()
{
	int s=0,f=1; char ch=getchar();
	while(!isdigit(ch) && ch!='-')ch=getchar();
	if(ch=='-')ch=getchar(),f=-1;
	while(isdigit(ch))s=s*10+ch-'0',ch=getchar();
	return ~f?s:-s;
}

const ll maxn=5e5+20;

ll c[maxn];
int n,m;
vector<int>ed[maxn];
int tot;
set<int>s[maxn];
int vis[maxn],id[maxn];
int Vis[maxn],Tim,ID[maxn];

inline void init()
{
	n=read();m=read();
	REP(i,1,n)scanf("%lld",c+i),ed[i].clear();
	REP(i,1,tot)s[i].clear(); tot=0;
	REP(i,1,m)
	{
		int u=read(),v=read();
		ed[u].push_back(v);
	}
	tot=1;
	REP(i,1,n)s[1].insert(i),id[i]=1,Vis[i]=0,ID[i]=0;
	Tim=0;
	REP(i,1,n)
	{
		++Tim;
		for(int j:ed[i])
		{
			int x=id[j];
			s[x].erase(j);
			vis[j]=x;
		}
		for(int j:ed[i])
		{
			if(Vis[vis[j]]==Tim)
			{

			}else Vis[vis[j]]=Tim,ID[vis[j]]=++tot;
			id[j]=ID[vis[j]];
			s[ID[vis[j]]].insert(j);
		}
	}
}

inline void doing()
{
	ll ans=0;
	REP(i,1,tot)
	{
		ll sum=0;
		for(ll x:s[i])sum+=c[x];//,cerr<<x<<" ";
		//cerr<<endl;
		ans=__gcd(ans,sum);
	}
	printf("%lld\n",ans);
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("c.in","r",stdin);
	freopen("c.out","w",stdout);
#endif
	int t=read();
	while(t--)
	{
		init();
		doing();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,a,b) for(int i=(a),_end_=(b);i<=_end_;i++)
#define DREP(i,a,b) for(int i=(a),_end_=(b);i>=_end_;i--)
#define EREP(i,u) for(int i=start[u];i;i=e[i].next)
#define fi first
#define se second
#define mkr(a,b) make_pair(a,b)
#define SZ(A) ((int)A.size())
template<class T>inline void chkmin(T &a,T b){ if(a>b)a=b;}
template<class T>inline void chkmax(T &a,T b){ if(a<b)a=b;}
inline int read()
{
	int s=0,f=1; char ch=getchar();
	while(!isdigit(ch) && ch!='-')ch=getchar();
	if(ch=='-')ch=getchar(),f=-1;
	while(isdigit(ch))s=s*10+ch-'0',ch=getchar();
	return ~f?s:-s;
}

const ll maxn=2e6+20;

ll c[maxn];
int n,m,in[maxn];
vector<int>ed[maxn];
int tot;
set<int>s[maxn];
int vis[maxn],id[maxn];
int Vis[maxn],Tim,ID[maxn];

inline void init()
{
	n=read();m=read();
	REP(i,1,n)scanf("%lld",c+i),ed[i].clear(),in[i]=0;
	REP(i,1,tot)s[i].clear(); tot=0;
	   REP(i,1,tot)Vis[i]=ID[i]=vis[i]=0;
	REP(i,1,m)
	{
		int u=read(),v=read();
		ed[u].push_back(v);in[v]++;
	}
	tot=1;
	REP(i,1,n)s[1].insert(i),id[i]=1,Vis[i]=0,ID[i]=0,vis[i]=0;
	Tim=0;
	REP(i,1,n)
	{
		++Tim;
		for(int j:ed[i])
		{
			int x=id[j];
			s[x].erase(j);
			vis[j]=x;
		}
		for(int j:ed[i])
		{
			if(Vis[vis[j]]==Tim)
			{

			}else Vis[vis[j]]=Tim,ID[vis[j]]=++tot;
			id[j]=ID[vis[j]];
			s[ID[vis[j]]].insert(j);
		}
	}
}

inline void doing()
{
	ll ans=0;
	REP(i,1,tot)if(!s[i].empty())
	{
		ll sum=0;
		for(ll x:s[i])sum+=c[x];//,cerr<<x<<" ";
		//cerr<<sum<<endl;
		if(in[*s[i].begin()])ans=__gcd(ans,sum);
	}
	printf("%lld\n",ans);
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("c.in","r",stdin);
	freopen("c.out","w",stdout);
#endif
	int t=read();
	while(t--)
	{
		init();
		doing();
	}
	return 0;
}


#include <iostream>
#include <fstream>
#include <algorithm>
#include <iomanip>
#include <random>
#include <ctime>
#include <bitset>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <cstring>
#include <queue>
#include <deque>
#include <list>
#include <stack>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define ld long double
#define ull unsigned long long
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(x) x.begin(), x.end()
#define fast_io cout.tie(0), cin.tie(0), ios_base::sync_with_stdio(0)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using namespace std;
ld eps = (ld)1 / 1e15;
ll inf = 1e18, mod2 = 998244353, mod1 = 1e9 + 7;
ll sqr(ll a) { return a * a; }
ll qb(ll a) { return a * a * a; }
ll gcd(ll a, ll b) { return !a ? b : gcd(b % a, a); }
ll binpow(ll a, ll b, ll mod) { return b ? (b % 2 ? (a * (sqr(binpow(a, b / 2, mod)) % mod)) % mod : sqr(binpow(a, b / 2, mod)) % mod) : 1; }
ll binmult(ll a, ll b, ll mod) { return b ? (b % 2 ? (2 * binmult(a, b / 2, mod) + a) % mod : (2 * binmult(a, b / 2, mod)) % mod) : 0; }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
vector<ll> vc[500001];
ll b[500001];
bool used[500001];

int main()
{
    //freopen("majorhouse.in", "r", stdin), freopen("majorhouse.out", "w", stdout);
    fast_io;
    ll q, n, m, i, x, y, g;
    map<vector<ll>, ll> mp;
    cin >> q;
    while (q--)
    {
        cin >> n >> m;
        mp.clear();
        for (i = 1; i <= n; i++) cin >> b[i], vc[i].clear(), used[i] = 0;
        for (i = 1; i <= m; i++)
        {
            cin >> x >> y;
            vc[y].pb(x);
            used[y] = 1;
        }
        for (i = 1; i <= n; i++)
        {
            if (!used[i]) continue;
            if (mp.find(vc[i]) == mp.end()) mp.insert({vc[i], 0});
            mp[vc[i]] += b[i];
        }
        g = 0;
        for (auto f: mp)
        {
            if (!g) g = f.second;
            else g = gcd(g, f.second);
        }
        cout << g << '\n';
    }
    return 0;
}


#include <iostream>
#include <fstream>
#include <algorithm>
#include <iomanip>
#include <random>
#include <ctime>
#include <bitset>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <cstring>
#include <queue>
#include <deque>
#include <list>
#include <stack>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define ld long double
#define ull unsigned long long
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(x) x.begin(), x.end()
#define fast_io cout.tie(0), cin.tie(0), ios_base::sync_with_stdio(0)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using namespace std;
ld eps = (ld)1 / 1e15;
ll inf = 1e18, mod2 = 998244353, mod1 = 1e9 + 7;
ll sqr(ll a) { return a * a; }
ll qb(ll a) { return a * a * a; }
ll binpow(ll a, ll b, ll mod) { return b ? (b % 2 ? (a * (sqr(binpow(a, b / 2, mod)) % mod)) % mod : sqr(binpow(a, b / 2, mod)) % mod) : 1; }
ll binmult(ll a, ll b, ll mod) { return b ? (b % 2 ? (2 * binmult(a, b / 2, mod) + a) % mod : (2 * binmult(a, b / 2, mod)) % mod) : 0; }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
vector<int> vc[500001];
ll b[500001];

int main()
{
    //freopen("majorhouse.in", "r", stdin), freopen("majorhouse.out", "w", stdout);
    fast_io;
    int q, n, m, i, x, y;
    ll g;
    map<vector<int>, ll> mp;
    cin >> q;
    while (q--)
    {
        cin >> n >> m;
        mp.clear();
        for (i = 1; i <= n; i++) cin >> b[i], vc[i].clear();
        for (i = 1; i <= m; i++)
        {
            cin >> x >> y;
            vc[y].pb(x);
        }
        for (i = 1; i <= n; i++)
        {
            if (vc[i].empty()) continue;
            //sort(all(vc[i]));
            if (mp.find(vc[i]) == mp.end()) mp.insert({vc[i], 0});
            mp[vc[i]] += b[i];
        }
        g = 0;
        for (auto f: mp)
            g = __gcd(g, f.second);
        cout << g << '\n';
    }
    return 0;
}


#include <iostream>
#include <fstream>
#include <algorithm>
#include <iomanip>
#include <random>
#include <ctime>
#include <bitset>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <cstring>
#include <queue>
#include <deque>
#include <list>
#include <stack>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define ld long double
#define ull unsigned long long
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(x) x.begin(), x.end()
#define fast_io cout.tie(0), cin.tie(0), ios_base::sync_with_stdio(0)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using namespace std;
ld eps = (ld)1 / 1e15;
ll inf = 1e18, mod2 = 998244353, mod1 = 1e9 + 7;
ll sqr(ll a) { return a * a; }
ll qb(ll a) { return a * a * a; }
ll gcd(ll a, ll b) { return !a ? b : gcd(b % a, a); }
ll binpow(ll a, ll b, ll mod) { return b ? (b % 2 ? (a * (sqr(binpow(a, b / 2, mod)) % mod)) % mod : sqr(binpow(a, b / 2, mod)) % mod) : 1; }
ll binmult(ll a, ll b, ll mod) { return b ? (b % 2 ? (2 * binmult(a, b / 2, mod) + a) % mod : (2 * binmult(a, b / 2, mod)) % mod) : 0; }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
vector<ll> vc[500001];
ll b[500001], prm = 500009, st[500001], md = 1e18 + 3;
bool used[500001];
ll make_hash(vector<ll> vc)
{
    ll s = 0;
    for (int i = 0; i < vc.size(); i++)
        s += binmult(vc[i], st[i + 1], md), s %= md;
    return s;
}

int main()
{
    //freopen("majorhouse.in", "r", stdin), freopen("majorhouse.out", "w", stdout);
    fast_io;
    ll q, n, m, i, x, y, g, t;
    unordered_map<ll, ll> ump;
    cin >> q;
    for (i = 2, st[1] = prm; i <= 500000; i++)
        st[i] = binmult(st[i - 1], prm, md), st[i] %= md;
    while (q--)
    {
        cin >> n >> m;
        ump.clear();
        for (i = 1; i <= n; i++) cin >> b[i], vc[i].clear(), used[i] = 0;
        for (i = 1; i <= m; i++)
        {
            cin >> x >> y;
            vc[y].pb(x);
            used[y] = 1;
        }
        for (i = 1; i <= n; i++)
        {
            if (!used[i]) continue;
            sort(all(vc[i]));
            t = make_hash(vc[i]);
            if (ump.find(t) == ump.end()) ump.insert({t, 0});
            ump[t] += b[i];
        }
        g = 0;
        for (auto f: ump)
            g = gcd(g, f.second);
        cout << g << '\n';
    }
    return 0;
}


#include <iostream>
#include <fstream>
#include <algorithm>
#include <iomanip>
#include <random>
#include <ctime>
#include <bitset>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <cstring>
#include <queue>
#include <deque>
#include <list>
#include <stack>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define ld long double
#define ull unsigned long long
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(x) x.begin(), x.end()
#define fast_io cout.tie(0), cin.tie(0), ios_base::sync_with_stdio(0)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using namespace std;
ld eps = (ld)1 / 1e15;
ll inf = 1e18, mod2 = 998244353, mod1 = 1e9 + 7;
ll sqr(ll a) { return a * a; }
ll qb(ll a) { return a * a * a; }
ll gcd(ll a, ll b) { return !a ? b : gcd(b % a, a); }
ll binpow(ll a, ll b, ll mod) { return b ? (b % 2 ? (a * (sqr(binpow(a, b / 2, mod)) % mod)) % mod : sqr(binpow(a, b / 2, mod)) % mod) : 1; }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
vector<ll> vc[500001];
ll b[500001], prm = 500009;
bool used[500001];
int make_hash(vector<ll> vc)
{
    ll s = 0;
    for (int i = 0; i < vc.size(); i++)
        s += vc[i] * binpow(prm, i + 1, mod2), s %= mod2;
    return s;
}

int main()
{
    //freopen("majorhouse.in", "r", stdin), freopen("majorhouse.out", "w", stdout);
    fast_io;
    ll q, n, m, i, x, y, g, t;
    unordered_map<int, int> ump;
    cin >> q;
    while (q--)
    {
        cin >> n >> m;
        ump.clear();
        for (i = 1; i <= n; i++) cin >> b[i], vc[i].clear(), used[i] = 0;
        for (i = 1; i <= m; i++)
        {
            cin >> x >> y;
            vc[y].pb(x);
            used[y] = 1;
        }
        for (i = 1; i <= n; i++)
        {
            if (!used[i]) continue;
            t = make_hash(vc[i]);
            if (ump.find(t) == ump.end()) ump.insert({t, 0});
            ump[t] += b[i];
        }
        g = 0;
        for (auto f: ump)
        {
            if (!g) g = f.second;
            else g = gcd(g, f.second);
        }
        cout << g << '\n';
    }
    return 0;
}


#include <iostream>
#include <fstream>
#include <algorithm>
#include <iomanip>
#include <random>
#include <ctime>
#include <bitset>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <cstring>
#include <queue>
#include <deque>
#include <list>
#include <stack>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define ld long double
#define ull unsigned long long
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(x) x.begin(), x.end()
#define fast_io cout.tie(0), cin.tie(0), ios_base::sync_with_stdio(0)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using namespace std;
ld eps = (ld)1 / 1e15;
ll inf = 1e18, mod2 = 998244353, mod1 = 1e9 + 7;
ll sqr(ll a) { return a * a; }
ll qb(ll a) { return a * a * a; }
ll binpow(ll a, ll b, ll mod) { return b ? (b % 2 ? (a * (sqr(binpow(a, b / 2, mod)) % mod)) % mod : sqr(binpow(a, b / 2, mod)) % mod) : 1; }
ll binmult(ll a, ll b, ll mod) { return b ? (b % 2 ? (2 * binmult(a, b / 2, mod) + a) % mod : (2 * binmult(a, b / 2, mod)) % mod) : 0; }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
vector<ll> vc[500001];
ll b[500001];

int main()
{
    //freopen("majorhouse.in", "r", stdin), freopen("majorhouse.out", "w", stdout);
    fast_io;
    ll q, n, m, i, x, y, g;
    cin >> q;
    while (q--)
    {
        cin >> n >> m;
        map<vector<ll>, ll> mp;
        for (i = 1; i <= n; i++) cin >> b[i], vc[i].clear();
        for (i = 1; i <= m; i++)
        {
            cin >> x >> y;
            vc[y].pb(x);
        }
        for (i = 1; i <= n; i++)
        {
            if (vc[i].empty()) continue;
            sort(all(vc[i]));
            if (mp.find(vc[i]) == mp.end()) mp.insert({vc[i], 0});
            mp[vc[i]] += b[i];
        }
        g = 0;
        for (auto f: mp)
            g = __gcd(g, f.second);
        cout << g << '\n';
    }
    return 0;
}


#include <iostream>
#include <fstream>
#include <algorithm>
#include <iomanip>
#include <random>
#include <ctime>
#include <bitset>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <cstring>
#include <queue>
#include <deque>
#include <list>
#include <stack>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define ld long double
#define ull unsigned long long
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(x) x.begin(), x.end()
#define fast_io cout.tie(0), cin.tie(0), ios_base::sync_with_stdio(0)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using namespace std;
ld eps = (ld)1 / 1e15;
ll inf = 1e18, mod2 = 998244353, mod1 = 1e9 + 7;
ll sqr(ll a) { return a * a; }
ll qb(ll a) { return a * a * a; }
ll gcd(ll a, ll b) { return !a ? b : gcd(b % a, a); }
ll binpow(ll a, ll b, ll mod) { return b ? (b % 2 ? (a * (sqr(binpow(a, b / 2, mod)) % mod)) % mod : sqr(binpow(a, b / 2, mod)) % mod) : 1; }
ll binmult(ll a, ll b, ll mod) { return b ? (b % 2 ? (2 * binmult(a, b / 2, mod) + a) % mod : (2 * binmult(a, b / 2, mod)) % mod) : 0; }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
vector<ll> vc[500001];
ll b[500001], prm = 500009, st[500001], md = 1e18 + 3;
bool used[500001];
ll make_hash(vector<ll> vc)
{
    ll s = 0;
    for (int i = 0; i < vc.size(); i++)
        s += binmult(vc[i], st[i + 1], md), s %= md;
    return s;
}

int main()
{
    //freopen("majorhouse.in", "r", stdin), freopen("majorhouse.out", "w", stdout);
    fast_io;
    ll q, n, m, i, x, y, g, t;
    unordered_map<ll, ll> ump;
    cin >> q;
    for (i = 2, st[1] = prm; i <= 500000; i++)
        st[i] = binmult(st[i - 1], prm, md), st[i] %= md;
    while (q--)
    {
        cin >> n >> m;
        ump.clear();
        for (i = 1; i <= n; i++) cin >> b[i], vc[i].clear(), used[i] = 0;
        for (i = 1; i <= m; i++)
        {
            cin >> x >> y;
            vc[y].pb(x);
            used[y] = 1;
        }
        for (i = 1; i <= n; i++)
        {
            if (!used[i]) continue;
            t = make_hash(vc[i]);
            if (ump.find(t) == ump.end()) ump.insert({t, 0});
            ump[t] += b[i];
        }
        g = 0;
        for (auto f: ump)
        {
            if (!g) g = f.second;
            else g = gcd(g, f.second);
        }
        cout << g << '\n';
    }
    return 0;
}


#include <iostream>
#include <fstream>
#include <algorithm>
#include <iomanip>
#include <random>
#include <ctime>
#include <bitset>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <cstring>
#include <queue>
#include <deque>
#include <list>
#include <stack>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define ld long double
#define ull unsigned long long
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(x) x.begin(), x.end()
#define fast_io cout.tie(0), cin.tie(0), ios_base::sync_with_stdio(0)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using namespace std;
ld eps = (ld)1 / 1e15;
ll inf = 1e18, mod2 = 998244353, mod1 = 1e9 + 7;
ll sqr(ll a) { return a * a; }
ll qb(ll a) { return a * a * a; }
ll gcd(ll a, ll b) { return !a ? b : gcd(b % a, a); }
ll binpow(ll a, ll b, ll mod) { return b ? (b % 2 ? (a * (sqr(binpow(a, b / 2, mod)) % mod)) % mod : sqr(binpow(a, b / 2, mod)) % mod) : 1; }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
vector<ll> vc[500001];
ll b[500001], prm = 500009;
bool used[500001];
ll make_hash(vector<ll> vc)
{
    ll s = 0;
    for (int i = 0; i < vc.size(); i++)
        s += vc[i] * binpow(prm, i + 1, mod2), s %= mod2;
    return s;
}

int main()
{
    //freopen("majorhouse.in", "r", stdin), freopen("majorhouse.out", "w", stdout);
    fast_io;
    ll q, n, m, i, x, y, g, t;
    unordered_map<ll, ll> ump;
    cin >> q;
    while (q--)
    {
        cin >> n >> m;
        ump.clear();
        for (i = 1; i <= n; i++) cin >> b[i], vc[i].clear(), used[i] = 0;
        for (i = 1; i <= m; i++)
        {
            cin >> x >> y;
            vc[y].pb(x);
            used[y] = 1;
        }
        for (i = 1; i <= n; i++)
        {
            if (!used[i]) continue;
            t = make_hash(vc[i]);
            if (ump.find(t) == ump.end()) ump.insert({t, 0});
            ump[t] += b[i];
        }
        g = 0;
        for (auto f: ump)
        {
            if (!g) g = f.second;
            else g = gcd(g, f.second);
        }
        cout << g << '\n';
    }
    return 0;
}


#include <iostream>
#include <fstream>
#include <algorithm>
#include <iomanip>
#include <random>
#include <ctime>
#include <bitset>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <cstring>
#include <queue>
#include <deque>
#include <list>
#include <stack>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define ld long double
#define ull unsigned long long
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(x) x.begin(), x.end()
#define fast_io cout.tie(0), cin.tie(0), ios_base::sync_with_stdio(0)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using namespace std;
ld eps = (ld)1 / 1e15;
ll inf = 1e18, mod2 = 998244353, mod1 = 1e9 + 7;
ll sqr(ll a) { return a * a; }
ll qb(ll a) { return a * a * a; }
ll gcd(ll a, ll b) { return !a ? b : gcd(b % a, a); }
ll binpow(ll a, ll b, ll mod) { return b ? (b % 2 ? (a * (sqr(binpow(a, b / 2, mod)) % mod)) % mod : sqr(binpow(a, b / 2, mod)) % mod) : 1; }
ll binmult(ll a, ll b, ll mod) { return b ? (b % 2 ? (2 * binmult(a, b / 2, mod) + a) % mod : (2 * binmult(a, b / 2, mod)) % mod) : 0; }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
vector<ll> vc[500001];
ll b[500001];

int main()
{
    //freopen("majorhouse.in", "r", stdin), freopen("majorhouse.out", "w", stdout);
    fast_io;
    ll q, n, m, i, x, y, g;
    map<vector<ll>, ll> mp;
    cin >> q;
    while (q--)
    {
        cin >> n >> m;
        mp.clear();
        for (i = 1; i <= n; i++) cin >> b[i], vc[i].clear();
        for (i = 1; i <= m; i++)
        {
            cin >> x >> y;
            vc[y].pb(x);
        }
        for (i = 1; i <= n; i++)
        {
            if (mp.find(vc[i]) == mp.end()) mp.insert({vc[i], 0});
            mp[vc[i]] += b[i];
        }
        g = 0;
        for (auto f: mp)
        {
            if (f.first.empty()) continue;
            if (!g) g = f.second;
            else g = gcd(g, f.second);
        }
        cout << g << '\n';
    }
    return 0;
}


#include <iostream>
#include <fstream>
#include <algorithm>
#include <iomanip>
#include <random>
#include <ctime>
#include <bitset>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <cstring>
#include <queue>
#include <deque>
#include <list>
#include <stack>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define ld long double
#define ull unsigned long long
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(x) x.begin(), x.end()
#define fast_io cout.tie(0), cin.tie(0), ios_base::sync_with_stdio(0)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using namespace std;
ld eps = (ld)1 / 1e15;
ll inf = 1e18, mod2 = 998244353, mod1 = 1e9 + 7;
ll sqr(ll a) { return a * a; }
ll qb(ll a) { return a * a * a; }
ll binpow(ll a, ll b, ll mod) { return b ? (b % 2 ? (a * (sqr(binpow(a, b / 2, mod)) % mod)) % mod : sqr(binpow(a, b / 2, mod)) % mod) : 1; }
ll binmult(ll a, ll b, ll mod) { return b ? (b % 2 ? (2 * binmult(a, b / 2, mod) + a) % mod : (2 * binmult(a, b / 2, mod)) % mod) : 0; }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
vector<int> vc[500001];
ll b[500001];

int main()
{
    //freopen("majorhouse.in", "r", stdin), freopen("majorhouse.out", "w", stdout);
    fast_io;
    int q, n, m, i, x, y;
    ll g;
    map<vector<int>, ll> mp;
    cin >> q;
    while (q--)
    {
        cin >> n >> m;
        mp.clear();
        for (i = 1; i <= n; i++) cin >> b[i], vc[i].clear();
        for (i = 1; i <= m; i++)
        {
            cin >> x >> y;
            vc[y].pb(x);
        }
        for (i = 1; i <= n; i++)
        {
            if (vc[i].empty()) continue;
            sort(all(vc[i]));
            if (mp.find(vc[i]) == mp.end()) mp.insert({vc[i], 0});
            mp[vc[i]] += b[i];
        }
        g = 0;
        for (auto f: mp)
            g = __gcd(g, f.second);
        cout << g << '\n';
    }
    return 0;
}


// 1322C https://codeforces.com/problemset/problem/1322/C
#include <bits/stdc++.h>
using namespace std;

#define f(i,a,b) for (int i = a; i < b; i++)
#define fr(i,a,b) for (int i = (b)-1; i >= a; i--)
#define IN(i,a,b) (a<=i&&i<=b)
#define mp make_pair
#define mt make_tuple
#define sz(a) a.size()

using ld = long double;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi  = vector<int>;

const int oo = 1e9+7;
const ll loo = 1e18;

ll modpow(ll a, ll b) {
	ll res = 1;
	while (b) {
		if (b&1) res = res*a%oo;
		a = a*a%oo;
		b /=2;
	}
	return res;
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// use uniform_int_distribution<int>(0, i)(rng)

const int N = 5e5, K = 7;
int n,m;
int fid[N][K];
ll a[N];
vi adj[N], rev[N];
stringstream ss;

int solve() {
	cin>>n>>m;
	f(i,0,n) adj[i].clear(), rev[i].clear();

	f(i,0,n) cin>>a[i];
	f(i,0,n) f(j,0,K) fid[i][j] = i;
	f(i,0,n) f(j,0,K) swap(fid[i][j], fid[uniform_int_distribution<int>(0, i)(rng)][j]);

	map<array<ll,K>,ll> fi;
	f(i,0,m) {
		int x,y; cin>>x>>y;
		adj[--x].push_back(--y);
		rev[y].push_back(x);
	}

	ll g = 0;
	f(i,0,n) {
		array<ll,K> ash;
		f(j,0,K) ash[j] = 0;
		bool gd = 0;
		for (int w : rev[i]) {
			gd = 1;
			f(j,0,K) ash[j] ^= fid[w][j]+1;
		}
		if (gd) fi[ash] += a[i];
	}
	for (auto pp : fi)
		g = gcd(g, pp.second);
	ss << g << endl;
	return 0;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int t; cin>>t;
	while (t--) solve();
	cout << ss.str();

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define f(i,a,b) for (int i = a; i < b; i++)

const int N = 5e5, sN = 800;
int n,m;
int a[N];
map<int,int> cnt[N];
vector<int> adj[N];

long long gcd(long long a, long long b) {
	return a?gcd(b%a,a):b;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int t; cin>>t;
	while (t--) {
		cin>>n>>m;
		f(i,0,n) cin>>a[i];
		long long g = 0;
		f(i,0,n) adj[i].clear();
		f(i,0,m) {
			int v,w; cin>>v>>w; v--; w--;
			adj[v].push_back(w);
		}
		f(i,0,n) {
			long long s = 0;
			for (int w : adj[i])
				s += a[w];
			g = gcd(s, g);
		}
		bool con[n];
		f(i,0,n) con[i] = 0;
		f(i,0,n) for (int w : adj[i])
			con[w] = 1;
		long long s = 0;
		f(i,0,n) if (con[i]) s += a[i];
		g = gcd(s, g);

		cout << g << endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define f(i,a,b) for (int i = a; i < b; i++)

const int N = 5e5, sN = 800, mod = 1e9+7, NN = 1e7+1;
int n,m, p = 0;
long long a[N];
map<int,int> cnt[N];
vector<int> adj[N], rev[N];
int primes[N];

long long gcd(long long a, long long b) {
	return a?gcd(b%a,a):b;
}

long long modpow(long long a, long long b) {
	long long res = 1;
	while (b) {
		if (b&1) res = res*a%mod;
		a = a*a%mod;
		b /= 2;
	}
	return res;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	bool sieve[NN];
	f(i,0,NN) sieve[i] = 1;
	f(i,2,NN) if (sieve[i]) {
		primes[p++] = i;
		if (p == N) break;
		for (int j = i+i; j < NN; j += i)
			sieve[j] = 0;
	}

	int t; cin>>t;
	while (t--) {
		cin>>n>>m;
		f(i,0,n) cin>>a[i];
		long long g = 0;
		f(i,0,n) {
			adj[i].clear();
			rev[i].clear();
		}
		f(i,0,m) {
			int v,w; cin>>v>>w; v--; w--;
			adj[v].push_back(w);
			rev[w].push_back(v);
		}

		unordered_map<int,int> ash;

		f(i,0,n) {
			long long s = 0;
			for (int w : adj[i])
				s += a[w];
			g = gcd(s, g);
		}

		f(i,0,n) {
			long long val = 0;
			int cnt = 0;
			for (int w : rev[i])
				val = (val + modpow(primes[cnt++], w)) % mod;
			ash[val] += a[i];
		}

		for (auto pp : ash)
			g = gcd(g, pp.second);

		cout << g << endl;
	}

	return 0;
}


// 1322C https://codeforces.com/problemset/problem/1322/C
#include <bits/stdc++.h>
using namespace std;

#define f(i,a,b) for (int i = a; i < b; i++)
#define fr(i,a,b) for (int i = (b)-1; i >= a; i--)
#define IN(i,a,b) (a<=i&&i<=b)
#define mp make_pair
#define mt make_tuple
#define sz(a) a.size()

using ld = long double;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi  = vector<int>;

const int oo = 1e9+7;
const ll loo = 1e18;

ll modpow(ll a, ll b) {
	ll res = 1;
	while (b) {
		if (b&1) res = res*a%oo;
		a = a*a%oo;
		b /=2;
	}
	return res;
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// use uniform_int_distribution<int>(0, i)(rng)

const int N = 5e5;
int n,m;
int fid[N], a[N];
vi adj[N], rev[N];
stringstream ss;

int solve() {
	cin>>n>>m;
	f(i,0,n) adj[i].clear(), rev[i].clear();

	f(i,0,n) cin>>a[i];
	f(i,0,n) fid[i] = i;
	f(i,0,n) swap(fid[i], fid[uniform_int_distribution<int>(0, i)(rng)]);

	unordered_map<ll,ll> fi;
	f(i,0,m) {
		int x,y; cin>>x>>y;
		adj[--x].push_back(--y);
		rev[y].push_back(x);
	}

	ll g = 0;
	f(i,0,n) {
		ll sum = 0;
		for (int w : adj[i])
			sum += a[w];
		g = gcd(g, sum);

	}
	f(i,0,n) {
		ll ash = 0;
		for (int w : rev[i])
			ash ^= fid[w]+1;
		fi[ash] += a[i];
	}
	for (auto pp : fi)
		g = gcd(g, pp.second);
	ss << g << endl;
	return 0;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int t; cin>>t;
	while (t--) solve();
	cout << ss.str();

	return 0;
}


// 1322C https://codeforces.com/problemset/problem/1322/C
#include <bits/stdc++.h>
using namespace std;

#define f(i,a,b) for (int i = a; i < b; i++)
#define fr(i,a,b) for (int i = (b)-1; i >= a; i--)
#define IN(i,a,b) (a<=i&&i<=b)
#define mp make_pair
#define mt make_tuple
#define sz(a) a.size()

using ld = long double;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi  = vector<int>;

const int oo = 1e9+7;
const ll loo = 1e18;

ll modpow(ll a, ll b) {
	ll res = 1;
	while (b) {
		if (b&1) res = res*a%oo;
		a = a*a%oo;
		b /=2;
	}
	return res;
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// use uniform_int_distribution<int>(0, i)(rng)

const int N = 5e5;
int n,m;
int fid[N];
ll a[N];
vi adj[N], rev[N];
stringstream ss;

int solve() {
	cin>>n>>m;
	f(i,0,n) adj[i].clear(), rev[i].clear();

	f(i,0,n) cin>>a[i];
	f(i,0,n) fid[i] = i;
	f(i,0,n) swap(fid[i], fid[uniform_int_distribution<int>(0, i)(rng)]);

	unordered_map<ll,ll> fi;
	f(i,0,m) {
		int x,y; cin>>x>>y;
		adj[--x].push_back(--y);
		rev[y].push_back(x);
	}

	ll g = 0;
	f(i,0,n) {
		ll sum = 0;
		for (int w : adj[i])
			sum += a[w];
		g = gcd(g, sum);

	}
	f(i,0,n) {
		ll ash = 0;
		for (int w : rev[i])
			ash ^= fid[w]+1;
		if (ash) fi[ash] += a[i];
	}
	for (auto pp : fi)
		g = gcd(g, pp.second);
	ss << g << endl;
	return 0;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int t; cin>>t;
	while (t--) solve();
	cout << ss.str();

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define f(i,a,b) for (int i = a; i < b; i++)

const int N = 5e5, sN = 800, mod = 1e9+7, NN = 1e7+1;
int n,m, p = 0;
long long a[N];
map<int,int> cnt[N];
vector<int> adj[N], rev[N];
int primes[N];

long long gcd(long long a, long long b) {
	return a?gcd(b%a,a):b;
}

long long modpow(long long a, long long b) {
	long long res = 1;
	while (b) {
		if (b&1) res = res*a%mod;
		a = a*a%mod;
		b /= 2;
	}
	return res;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	bool sieve[NN];
	f(i,0,NN) sieve[i] = 1;
	f(i,2,NN) if (sieve[i]) {
		primes[p++] = i;
		if (p == N) break;
		for (int j = i+i; j < NN; j += i)
			sieve[j] = 0;
	}

	int t; cin>>t;
	while (t--) {
		cin>>n>>m;
		f(i,0,n) cin>>a[i];
		long long g = 0;
		f(i,0,n) {
			adj[i].clear();
			rev[i].clear();
		}
		f(i,0,m) {
			int v,w; cin>>v>>w; v--; w--;
			adj[v].push_back(w);
			rev[w].push_back(v);
		}

		unordered_map<int,int> ash;

		f(i,0,n) {
			long long s = 0;
			for (int w : adj[i])
				s += a[w];
			g = gcd(s, g);
		}

		f(i,0,n) {
			long long val = 0;
			int cnt = 0;
			for (int w : rev[i])
				val = val ^= w;
			ash[val] += a[i];
		}

		for (auto pp : ash)
			g = gcd(g, pp.second);
        long long s = 0;
		bool con[n];
		f(i,0,n) con[i] = 0;
		f(i,0,n) for (int w : adj[i])
			con[w] = 1;
		f(i,0,n) if (con[i]) s += a[i];
		g = gcd(s, g);
		cout << g << endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define f(i,a,b) for (int i = a; i < b; i++)

const int N = 5e5, sN = 800, mod = 1e9+7, NN = 1e7+1;
int n,m, p = 0;
long long a[N];
map<int,int> cnt[N];
vector<int> adj[N], rev[N];
int primes[N];

long long gcd(long long a, long long b) {
	return a?gcd(b%a,a):b;
}

long long modpow(long long a, long long b) {
	long long res = 1;
	while (b) {
		if (b&1) res = res*a%mod;
		a = a*a%mod;
		b /= 2;
	}
	return res;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	bool sieve[NN];
	f(i,0,NN) sieve[i] = 1;
	f(i,2,NN) if (sieve[i]) {
		primes[p++] = i;
		if (p == N) break;
		for (int j = i+i; j < NN; j += i)
			sieve[j] = 0;
	}

	int t; cin>>t;
	while (t--) {
		cin>>n>>m;
		f(i,0,n) cin>>a[i];
		long long g = 0;
		f(i,0,n) {
			adj[i].clear();
			rev[i].clear();
		}
		f(i,0,m) {
			int v,w; cin>>v>>w; v--; w--;
			adj[v].push_back(w);
			rev[w].push_back(v);
		}

		unordered_map<int,int> ash;

		f(i,0,n) {
			long long s = 0;
			for (int w : adj[i])
				s += a[w];
			g = gcd(s, g);
		}

		f(i,0,n) {
			long long val = 0;
			int cnt = 0;
			for (int w : rev[i])
				val = val ^= w;
			ash[val] += a[i];
		}

		for (auto pp : ash)
			g = gcd(g, pp.second);

		cout << g << endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define f(i,a,b) for (int i = a; i < b; i++)

const int N = 5e5, sN = 800, mod = 1e9+7, NN = 1e7+1;
int n,m, p = 0;
long long a[N];
map<int,int> cnt[N];
vector<int> adj[N], rev[N];
int primes[N];

long long gcd(long long a, long long b) {
	return a?gcd(b%a,a):b;
}

long long modpow(long long a, long long b) {
	long long res = 1;
	while (b) {
		if (b&1) res = res*a%mod;
		a = a*a%mod;
		b /= 2;
	}
	return res;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	bool sieve[NN];
	f(i,0,NN) sieve[i] = 1;
	f(i,2,NN) if (sieve[i]) {
		primes[p++] = i;
		if (p == N) break;
		for (int j = i+i; j < NN; j += i)
			sieve[j] = 0;
	}

	int t; cin>>t;
	while (t--) {
		cin>>n>>m;
		f(i,0,n) cin>>a[i];
		long long g = 0;
		f(i,0,n) {
			adj[i].clear();
			rev[i].clear();
		}
		f(i,0,m) {
			int v,w; cin>>v>>w; v--; w--;
			adj[v].push_back(w);
			rev[w].push_back(v);
		}

		unordered_map<int,int> ash;

		f(i,0,n) {
			long long s = 0;
			for (int w : adj[i])
				s += a[w];
			g = gcd(s, g);
		}

		f(i,0,n) {
			long long val = 0;
			int cnt = 0;
			for (int w : rev[i])
				val = val ^= w;
			ash[val] += a[i];
		}

		for (auto pp : ash)
			g = gcd(g, pp.second);
        		long long s = 0;
		bool con[n];
		f(i,0,n) con[i] = 0;
		f(i,0,n) for (int w : adj[i])
			con[w] = 1;
		long long s = 0;
		f(i,0,n) if (con[i]) s += a[i];
		g = gcd(s, g);
		cout << g << endl;
	}

	return 0;
}


// 1322C https://codeforces.com/problemset/problem/1322/C
#include <bits/stdc++.h>
using namespace std;

#define f(i,a,b) for (int i = a; i < b; i++)
#define fr(i,a,b) for (int i = (b)-1; i >= a; i--)
#define IN(i,a,b) (a<=i&&i<=b)
#define mp make_pair
#define mt make_tuple
#define sz(a) a.size()

using ld = long double;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi  = vector<int>;

const int oo = 1e9+7;
const ll loo = 1e18;

ll modpow(ll a, ll b) {
	ll res = 1;
	while (b) {
		if (b&1) res = res*a%oo;
		a = a*a%oo;
		b /=2;
	}
	return res;
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// use uniform_int_distribution<int>(0, i)(rng)

const int N = 5e5;
int n,m;
int fid[N], a[N];
vi adj[N], rev[N];
stringstream ss;

int solve() {
	cin>>n>>m;
	f(i,0,n) adj[i].clear(), rev[i].clear();

	f(i,0,n) cin>>a[i];
	f(i,0,n) fid[i] = i;
	f(i,0,n) swap(fid[i], fid[uniform_int_distribution<int>(0, i)(rng)]);

	unordered_map<ll,ll> fi;
	f(i,0,m) {
		int x,y; cin>>x>>y;
		adj[--x].push_back(--y);
		rev[y].push_back(x);
	}

	ll g = 0;
	f(i,0,n) {
		ll sum = 0;
		for (int w : adj[i])
			sum += a[w];
		g = gcd(g, sum);

	}
	f(i,0,n) {
		ll ash = 0;
		for (int w : rev[i])
			ash ^= fid[w]+1;
		if (ash) fi[ash] += a[i];
	}
	for (auto pp : fi)
		g = gcd(g, pp.second);
	ss << g << endl;
	return 0;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int t; cin>>t;
	while (t--) solve();
	cout << ss.str();

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define f(i,a,b) for (int i = a; i < b; i++)

const int N = 5e5, sN = 800, mod = 1e9+9, NN = 1e7+1;
int n,m, p = 0;
long long a[N];
map<int,int> cnt[N];
vector<int> adj[N], rev[N];
int primes[N];

long long gcd(long long a, long long b) {
	return a?gcd(b%a,a):b;
}

long long modpow(long long a, long long b) {
	long long res = 1;
	while (b) {
		if (b&1) res = res*a%mod;
		a = a*a%mod;
		b /= 2;
	}
	return res;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	bool sieve[NN];
	f(i,0,NN) sieve[i] = 1;
	f(i,2,NN) if (sieve[i]) {
		primes[p++] = i;
		if (p == N) break;
		for (int j = i+i; j < NN; j += i)
			sieve[j] = 0;
	}

	int t; cin>>t;
	while (t--) {
		cin>>n>>m;
		f(i,0,n) cin>>a[i];
		long long g = 0;
		f(i,0,n) {
			adj[i].clear();
			rev[i].clear();
		}
		f(i,0,m) {
			int v,w; cin>>v>>w; v--; w--;
			adj[v].push_back(w);
			rev[w].push_back(v);
		}

		unordered_map<int,int> ash;

		f(i,0,n) {
			long long s = 0;
			for (int w : adj[i])
				s += a[w];
			g = gcd(s, g);
		}

		f(i,0,n) {
			long long val = 0;
			int cnt = 0;
			for (int w : rev[i])
				val = (val + modpow(primes[cnt++], w)) % mod;
			ash[val] += a[i];
		}

		for (auto pp : ash)
			g = gcd(g, pp.second);

		cout << g << endl;
	}

	return 0;
}


#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <map>

#define int long long

typedef long long ll;

const int N = 5e5 + 10;
int MOD, BASE;

int n, m;
int c[N];
struct Edge {
  int v, nxt;

  Edge() : v(0), nxt(0) {}
  Edge(int _v, int _nxt) : v(_v), nxt(_nxt) {}
} e[N << 1];
int head[N], edge_cnt;

void AddEdge(int u, int v) {
  e[++edge_cnt] = Edge(v, head[u]);
  head[u] = edge_cnt;
}

#define Rand(l, r) (1ll * rand() * rand() * rand() % (r - l + 1) + l)

void Init() {
  srand(time(NULL));
  MOD = Rand((int)1e8, (int)1e9);
  BASE = Rand((int)1e7, (int)1e8);
}

std::map<int, ll> has;

int Has(int u) {
  int res = 0;
  for (int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].v;
    res = (1ll * res * BASE + v) % MOD;
  }
  return res;
}

ll Gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return Gcd(b, a % b);
}

signed main() {
  Init();
  int T;
  scanf("%lld", &T);
  while (T--) {
    memset(head, 0, sizeof(head));
    edge_cnt = 0;
    scanf("%lld%lld", &n, &m);
    for (int i = 1; i <= n; ++i)
      scanf("%lld", &c[i]);
    for (int i = 1, u, v; i <= m; ++i) {
      scanf("%lld%lld", &u, &v);
      AddEdge(v, u);
    }
    has.clear();
    for (int i = 1; i <= n; ++i)
      if (head[i])
        has[Has(i)] += c[i];
    ll ans = 0;
    for (auto i : has)
      ans = ans ? __gcd(ans, i.second) : i.second;
    printf("%lld\n", ans);
  }
  return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <map>

#define int long long

typedef long long ll;

const int N = 5e5 + 10;
int MOD, BASE;

int n, m;
int c[N];
struct Edge {
  int v, nxt;

  Edge() : v(0), nxt(0) {}
  Edge(int _v, int _nxt) : v(_v), nxt(_nxt) {}
} e[N << 1];
int head[N], edge_cnt;

void AddEdge(int u, int v) {
  e[++edge_cnt] = Edge(v, head[u]);
  head[u] = edge_cnt;
}

#define Rand(l, r) (1ll * rand() * rand() * rand() % (r - l + 1) + l)

void Init() {
  srand(time(NULL));
  MOD = Rand((int)1e8, (int)1e9);
  BASE = Rand((int)1e7, (int)1e8);
}

std::map<int, ll> has;

int Has(int u) {
  int res = 0;
  for (int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].v;
    res = (1ll * res * BASE + v) % MOD;
  }
  return res;
}

ll Gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return Gcd(b, a % b);
}

signed main() {
  Init();
  int T;
  scanf("%lld", &T);
  while (T--) {
    memset(head, 0, sizeof(head));
    edge_cnt = 0;
    scanf("%lld%lld", &n, &m);
    for (int i = 1; i <= n; ++i)
      scanf("%lld", &c[i]);
    for (int i = 1, u, v; i <= m; ++i) {
      scanf("%lld%lld", &u, &v);
      AddEdge(v, u);
    }
    has.clear();
    for (int i = 1; i <= n; ++i)
      if (head[i])
        has[Has(i)] += c[i];
    ll ans = 0;
    for (auto i : has)
      ans = ans ? Gcd(ans, i.second) : i.second;
    printf("%lld\n", ans);
  }
  return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <map>

typedef long long ll;

const int N = 5e5 + 10;
int MOD, BASE;

int n, m;
int c[N];
struct Edge {
  int v, nxt;

  Edge() : v(0), nxt(0) {}
  Edge(int _v, int _nxt) : v(_v), nxt(_nxt) {}
} e[N << 1];
int head[N], edge_cnt;

void AddEdge(int u, int v) {
  e[++edge_cnt] = Edge(v, head[u]);
  head[u] = edge_cnt;
}

#define Rand(l, r) (1ll * rand() * rand() * rand() % (r - l + 1) + l)

void Init() {
  MOD = Rand((int)1e8, (int)1e9);
  BASE = Rand((int)1e6, (int)1e7);
}

std::map<int, ll> has;

int Has(int u) {
  int res = 0;
  for (int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].v;
    res = (1ll * res * BASE + v) % MOD;
  }
  return res;
}

ll Gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return Gcd(b, a % b);
}

int main() {
  Init();
  int T;
  scanf("%d", &T);
  while (T--) {
    memset(head, 0, sizeof(head));
    edge_cnt = 0;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; ++i)
      scanf("%d", &c[i]);
    for (int i = 1, u, v; i <= m; ++i) {
      scanf("%d%d", &u, &v);
      AddEdge(v, u);
    }
    has.clear();
    for (int i = 1; i <= n; ++i)
      if (head[i])
        has[Has(i)] += c[i];
    ll ans = 0;
    for (auto i : has)
      ans = ans ? Gcd(ans, i.second) : i.second;
    printf("%lld\n", ans);
  }
  return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <map>

typedef long long ll;

const int N = 5e5 + 10;
int MOD, BASE;

int n, m;
int c[N];
struct Edge {
  int v, nxt;

  Edge() : v(0), nxt(0) {}
  Edge(int _v, int _nxt) : v(_v), nxt(_nxt) {}
} e[N << 1];
int head[N], edge_cnt;

void AddEdge(int u, int v) {
  e[++edge_cnt] = Edge(v, head[u]);
  head[u] = edge_cnt;
}

#define Rand(l, r) (1ll * rand() * rand() * rand() % (r - l + 1) + l)

void Init() {
  srand(time(NULL));
  MOD = Rand((int)1e8, (int)1e9);
  BASE = Rand((int)1e6, (int)1e7);
}

std::map<int, ll> has;

int Has(int u) {
  int res = 0;
  for (int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].v;
    res = (1ll * res * BASE + v) % MOD;
  }
  return res;
}

ll Gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return Gcd(b, a % b);
}

int main() {
  Init();
  int T;
  scanf("%d", &T);
  while (T--) {
    memset(head, 0, sizeof(head));
    edge_cnt = 0;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; ++i)
      scanf("%d", &c[i]);
    for (int i = 1, u, v; i <= m; ++i) {
      scanf("%d%d", &u, &v);
      AddEdge(v, u);
    }
    has.clear();
    for (int i = 1; i <= n; ++i)
      if (head[i])
        has[Has(i)] += c[i];
    ll ans = 0;
    for (auto i : has)
      ans = ans ? Gcd(ans, i.second) : i.second;
    printf("%lld\n", ans);
  }
  return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <map>

#define int long long

typedef long long ll;

const int N = 5e5 + 10;
int MOD, BASE;

int n, m;
int c[N];
struct Edge {
  int v, nxt;

  Edge() : v(0), nxt(0) {}
  Edge(int _v, int _nxt) : v(_v), nxt(_nxt) {}
} e[N << 1];
int head[N], edge_cnt;

void AddEdge(int u, int v) {
  e[++edge_cnt] = Edge(v, head[u]);
  head[u] = edge_cnt;
}

#define Rand(l, r) (1ll * rand() * rand() * rand() % (r - l + 1) + l)

void Init() {
  srand(time(NULL));
  MOD = Rand((int)1e8, (int)1e9);
  BASE = Rand((int)1e6, (int)1e7);
}

std::map<int, ll> has;

int Has(int u) {
  int res = 0;
  for (int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].v;
    res = (1ll * res * BASE + v) % MOD;
  }
  return res;
}

ll Gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return Gcd(b, a % b);
}

signed main() {
  Init();
  int T;
  scanf("%lld", &T);
  while (T--) {
    memset(head, 0, sizeof(head));
    edge_cnt = 0;
    scanf("%lld%lld", &n, &m);
    for (int i = 1; i <= n; ++i)
      scanf("%lld", &c[i]);
    for (int i = 1, u, v; i <= m; ++i) {
      scanf("%lld%lld", &u, &v);
      AddEdge(v, u);
    }
    has.clear();
    for (int i = 1; i <= n; ++i)
      if (head[i])
        has[Has(i)] += c[i];
    ll ans = 0;
    for (auto i : has)
      ans = ans ? Gcd(ans, i.second) : i.second;
    printf("%lld\n", ans);
  }
  return 0;
}


#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <map>

#define int long long

typedef long long ll;

const int N = 5e5 + 10;
int MOD, BASE;

int n, m;
int c[N];
struct Edge {
  int v, nxt;

  Edge() : v(0), nxt(0) {}
  Edge(int _v, int _nxt) : v(_v), nxt(_nxt) {}
} e[N << 1];
int head[N], edge_cnt;

void AddEdge(int u, int v) {
  e[++edge_cnt] = Edge(v, head[u]);
  head[u] = edge_cnt;
}

#define Rand(l, r) (1ll * rand() * rand() * rand() % (r - l + 1) + l)

void Init() {
  srand(time(NULL));
  MOD = Rand((int)1e8, (int)1e9);
  BASE = Rand((int)1e7, (int)1e8);
}

std::map<int, ll> has;

int Has(int u) {
  int res = 0;
  for (int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].v;
    res = (1ll * res * BASE + v) % MOD;
  }
  return res;
}

ll Gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return Gcd(b, a % b);
}

signed main() {
  Init();
  int T;
  scanf("%lld", &T);
  while (T--) {
    memset(head, 0, sizeof(head));
    edge_cnt = 0;
    scanf("%lld%lld", &n, &m);
    for (int i = 1; i <= n; ++i)
      scanf("%lld", &c[i]);
    for (int i = 1, u, v; i <= m; ++i) {
      scanf("%lld%lld", &u, &v);
      AddEdge(v, u);
    }
    has.clear();
    for (int i = 1; i <= n; ++i)
      if (head[i])
        has[Has(i)] += c[i];
    ll ans = 0;
    for (auto i : has)
      ans = ans ? std::__gcd(ans, i.second) : i.second;
    printf("%lld\n", ans);
  }
  return 0;
}


#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <ctime>
#include <vector>

using namespace std;

typedef long long ll;

const int N = 5e5 + 7;
int n, m, B, P;
ll a[N];
vector<int> e[N];

inline int h(vector<int> p) {
  sort(p.begin(), p.end());
  int ret = 0;
  for (int x : p)
    ret = (1ll * ret * B + x) % P;
  return ret;
}
inline void solve() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i)
    scanf("%lld", &a[i]);
  for (int i = 1; i <= n; i++)
    e[i].clear();
  for (int i = 1, x, y; i <= m; i++)
    scanf("%d%d", &x, &y), e[y].push_back(x);
  map<int, ll> p;
  for (int i = 1; i <= n; i++)
    if (e[i].size())
      p[h(e[i])] += a[i];
  ll d = 0;
  for (auto o : p)
    d = d ? __gcd(d, o.second) : o.second;
  printf("%lld\n", d);
}

#define Rand(l, r) (1ll * rand() * rand() * rand() % (r - l + 1) + l)

int main() {
  srand(time(0));
  P = Rand((int)1e8, (int)1e9);
  B = Rand((int)1e7, (int)1e8);
  int T;
  scanf("%d", &T);
  while (T--)
    solve();
  return 0;
}
// for test


#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
#include <map>

void solve() {
  int n, m;
  std::cin >> n >> m;
  std::vector<long long int> C(n);
  for (int i = 0; i < n; ++i) {
    std::cin >> C[i];
  }
  std::vector<std::vector<int>> E(n);
  for (int i = 0; i < m; ++i) {
    int u, v;
    std::cin >> u >> v;
    E[v-1].push_back(u-1);
  }
  std::map<std::vector<int>, long long int> cs;
  for (int i = 0; i < n; ++i) {
    if (E[i].empty()) {
      continue;
    }
    std::sort(E[i].begin(), E[i].end());
    cs[E[i]] += C[i];
  }
  long long int g = 0;
  for (const auto& it: cs) {
    g = std::gcd(it.second, g);
  }
  std::cout << g << '\n';
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int t;
  std::cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}


#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
#include <map>

void solve() {
  int n, m;
  std::cin >> n >> m;
  std::vector<long long int> C(n);
  for (int i = 0; i < n; ++i) {
    std::cin >> C[i];
  }
  std::vector<std::vector<int>> E(n);
  for (int i = 0; i < m; ++i) {
    int u, v;
    std::cin >> u >> v;
    E[v-1].push_back(u-1);
  }
  std::map<std::vector<int>, long long int> cs;
  for (int i = 0; i < n; ++i) {
    if (E[i].empty()) {
      continue;
    }
    std::sort(E[i].begin(), E[i].end());
    cs[E[i]] += C[i];
  }
  long long int g = 0;
  for (const auto& it: cs) {
    g = std::gcd(it.second, g);
  }
  std::cout << g << '\n';
}

int main() {
  int t;
  std::cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}


#include <algorithm>
#include <iostream>
#include <numeric>
#include <unordered_set>
#include <unordered_map>
#include <vector>

struct Trie {
  std::unordered_map<int, Trie*> s;
  long long int c;

  Trie() : s(), c() {}
};

void insert(Trie* trie, const std::vector<int>& path, long long int c) {
  Trie* t = trie;
  for (const auto e: path) {
    auto s = t->s.find(e);
    if (s == t->s.end()) {
      s = t->s.emplace(std::make_pair(e, new Trie)).first;
    }
    t = s->second;
  }
  t->c += c;
}

void collect(Trie* t, std::vector<long long int>& gs) {
  if (t->c) {
    gs.push_back(t->c);
  }
  for (const auto it: t->s) {
    collect(it.second, gs);
  }
}

void solve() {
  int n, m;
  std::cin >> n >> m;
  std::vector<long long int> C(n);
  for (int i = 0; i < n; ++i) {
    std::cin >> C[i];
  }
  std::vector<std::vector<int>> E(n);
  for (int i = 0; i < m; ++i) {
    int u, v;
    std::cin >> u >> v;
    E[v-1].push_back(u-1);
  }
  Trie *t = new Trie;
  for (int i = 0; i < n; ++i) {
    if (E[i].empty()) {
      continue;
    }
    std::sort(E[i].begin(), E[i].end());
    insert(t, E[i], C[i]);
  }
  std::vector<long long int> cs(n);
  collect(t, cs);
  long long int g = 0;
  for (const auto c: cs) {
    g = std::gcd(c, g);
  }
  std::cout << g << '\n';
}

int main() {
  int t;
  std::cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}


#include <algorithm>
#include <iostream>
#include <numeric>
#include <unordered_map>
#include <vector>

struct Trie {
  std::unordered_map<int, Trie*> s;
  long long int c;

  Trie() : s(), c() {}
};

void insert(Trie* trie, const std::vector<int>& path, long long int c) {
  Trie* t = trie;
  for (const auto e: path) {
    auto s = t->s.find(e);
    if (s == t->s.end()) {
      s = t->s.emplace(std::make_pair(e, new Trie)).first;
    }
    t = s->second;
  }
  t->c += c;
}

long long int collect(Trie* t) {
  long long int g = t->c;
  for (const auto it: t->s) {
    g = std::gcd(g, collect(it.second));
  }
  return g;
}

void solve() {
  int n, m;
  std::cin >> n >> m;
  std::vector<long long int> C(n);
  for (int i = 0; i < n; ++i) {
    std::cin >> C[i];
  }
  std::vector<std::vector<int>> E(n);
  for (int i = 0; i < m; ++i) {
    int u, v;
    std::cin >> u >> v;
    E[v-1].push_back(u-1);
  }
  Trie *t = new Trie;
  for (int i = 0; i < n; ++i) {
    std::sort(E[i].begin(), E[i].end());
    insert(t, E[i], C[i]);
  }
  t->c = 0;
  std::cout << collect(t) << '\n';
}

int main() {
  int t;
  std::cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}


#include <algorithm>
#include <iostream>
#include <numeric>
#include <unordered_map>
#include <vector>

struct Trie {
  std::unordered_map<int, Trie> s;
  long long int c;

  Trie() : s(), c() {}
};

void insert(Trie& trie, const std::vector<int>& path, long long int c) {
  Trie* t = ≜
  for (const auto e: path) {
    auto s = t->s.find(e);
    if (s == t->s.end()) {
      s = t->s.emplace(std::make_pair(e, Trie())).first;
    }
    t = &s->second;
  }
  t->c += c;
}

long long int collect(const Trie& t) {
  long long int g = t.c;
  for (const auto it: t.s) {
    g = std::gcd(g, collect(it.second));
  }
  return g;
}

void solve() {
  int n, m;
  std::cin >> n >> m;
  std::vector<long long int> C(n);
  for (int i = 0; i < n; ++i) {
    std::cin >> C[i];
  }
  std::vector<std::vector<int>> E(n);
  for (int i = 0; i < m; ++i) {
    int u, v;
    std::cin >> u >> v;
    E[v-1].push_back(u-1);
  }
  Trie t;
  for (int i = 0; i < n; ++i) {
    std::sort(E[i].begin(), E[i].end());
    insert(t, E[i], C[i]);
  }
  std::cout << collect(t) << '\n';
}

int main() {
  int t;
  std::cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}