#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
long long inf = 1LL << 50;
const int N = 500000;
long long mod1 = 1000000009;
long long mod2 = 1000000007;
int prime = 101;
long long power(long long x, long long y, long long p) {
  long long res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long inv1 = power(prime, mod1 - 2, mod1);
long long inv2 = power(prime, mod2 - 2, mod2);
long long getHash(set<long long>& str) {
  long long p1 = prime;
  long long ret = *(str.begin());
  for (int i : str) {
    ret = (ret + p1 * i) % mod1;
    p1 = (p1 * prime) % mod1;
  }
  return ret;
}
pair<long long, long long> doubleHash(const set<long long>& str) {
  long long p1 = 1;
  long long p2 = 1;
  pair<long long, long long> p = make_pair(*(str.begin()), *(str.begin()));
  for (int i : str) {
    p.first = (p.first + p1 * i) % mod1;
    p.second = (p.second + p2 * i) % mod2;
    p1 = (p1 * prime) % mod1;
    p2 = (p2 * prime) % mod2;
  }
  return p;
}
long long gcd(long long a, long long b) {
  while (b > 0) {
    long long temp = b;
    b = a % b;
    a = temp;
  }
  return a;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin.exceptions(cin.failbit);
  int t;
  cin >> t;
  for (int z = 0; z < t; z++) {
    int n, m;
    cin >> n >> m;
    vector<long long> vals(n);
    vector<vector<int>> lefts(n);
    vector<set<long long>> rights(n);
    map<pair<long long, long long>, long long> hashSums;
    for (int i = 0; i < n; i++) {
      cin >> vals[i];
    }
    long long ans = 0;
    for (int i = 0; i < m; i++) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      lefts[a].push_back(b);
      rights[b].insert(a);
    }
    for (int i = 0; i < n; i++) {
      if (!rights[i].empty()) hashSums[doubleHash(rights[i])] += vals[i];
    }
    for (auto a : hashSums) {
      ans = gcd(a.second, ans);
    }
    cout << ans << '\n';
  }
}
