#include <bits/stdc++.h>
using namespace std;
vector<int> equivalence_classes(const vector<vector<int>>& G, int n) {
  vector<int> eq_class(n, 0);
  int c = 1;
  unordered_map<int, int> shift;
  unordered_map<int, int>::iterator it, end = shift.end();
  for (int i = 0; i < n; ++i) {
    for (int v : G[i]) {
      it = shift.find(eq_class[v]);
      if (it == end) {
        shift[eq_class[v]] = c;
        eq_class[v] = c++;
      } else
        eq_class[v] = it->second;
    }
    shift.clear();
  }
  return eq_class;
}
template <class T>
inline bool is_zero(const T& x) {
  return x == T(0);
}
template <class T>
T euclid_modulus_gcd(T x, T y) {
  while (true) {
    if (is_zero(y)) return x;
    x %= y;
    if (is_zero(x)) return y;
    y %= x;
  }
}
int main() {
  const int N = 500000;
  vector<long long> value(N);
  vector<vector<int>> G(N);
  int t;
  scanf("%d", &t);
  while (t--) {
    int n, m, u, v;
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; ++i) scanf("%lld", &value[i]);
    for (int e = 0; e < m; ++e) {
      scanf("%d %d", &u, &v);
      G[u - 1].push_back(v - 1);
    }
    vector<int> eq_class = equivalence_classes(G, n);
    unordered_map<int, long long> sum;
    for (int i = 0; i < n; ++i) sum[eq_class[i]] += value[i];
    sum.erase(0);
    long long gcd = 0;
    for (const auto& p : sum) gcd = euclid_modulus_gcd(gcd, p.second);
    printf("%lld\n", gcd);
    for (int i = 0; i < n; ++i) G[i].clear();
  }
}
