#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x *= f;
}
inline long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
const int N = 500050, E = 500050;
const int v1 = 23335, p1 = 1e9 + 7, v2 = 239876, p2 = 1e9 + 9;
const int v3 = 1234567;
int p3;
int pw1[N], pw2[N], pw3[N];
int n, m;
long long c[N];
vector<int> G[N], G2[N];
long long sum[N];
struct Hash {
  int h1, h2, h3, len;
  Hash(int hh1 = -1, int hh2 = -1, int hh3 = -1, int ll = -1) {
    h1 = hh1, h2 = hh2, h3 = hh3, ll = len;
  }
  inline bool operator<(Hash w) const {
    if (len != w.len) return len < w.len;
    if (h1 != w.h1) return h1 < w.h1;
    if (h2 != w.h2) return h2 < w.h2;
    return h3 < w.h3;
  }
  inline bool operator==(Hash w) const {
    if (len != w.len) return 0;
    if (h1 != w.h1) return 0;
    if (h2 != w.h2) return 0;
    return h3 == w.h3;
  }
};
int fa[N];
map<Hash, int> mp;
Hash data[N];
inline void calcH(int x) {
  if (!G2[x].size()) return;
  long long H1, H2, H3;
  int i, l;
  l = G2[x].size();
  sort(G2[x].begin(), G2[x].end());
  H1 = H2 = H3 = 0;
  for (i = 0; i < l; ++i) {
    H1 = (H1 * v1 + G2[x][i]) % p1;
    H2 = (H2 * v2 + G2[x][i]) % p2;
    H3 = (H3 * v3 + G2[x][i]) % p3;
  }
  data[x] = Hash(H1, H2, H3, l);
  if (mp.count(data[x])) {
    int pp = mp[data[x]];
    sum[fa[pp]] += c[x];
    fa[x] = fa[pp];
  } else {
    fa[x] = x;
    sum[x] = c[x];
    mp[data[x]] = x;
  }
}
long long v0[N], ans;
inline void solve() {
  int i, x, y;
  read(n), read(m);
  mp.clear();
  for (i = 1; i <= n; ++i)
    fa[i] = -1, G[i].clear(), G2[i].clear(), read(c[i]), sum[i] = v0[i] = 0;
  while (m--)
    read(x), read(y), G[x].push_back(y), v0[x] += c[y], G2[y].push_back(x);
  for (i = 1; i <= n; ++i) calcH(i);
  ans = 0;
  for (i = 1; i <= n; ++i) ans = gcd(ans, v0[i]);
  if (ans == 0) {
    cout << 0 << endl;
    return;
  }
  for (i = 1; i <= n; ++i) ans = gcd(ans, sum[i]);
  cout << ans << endl;
}
int main() {
  srand(time(NULL));
  p3 = (1ll * rand() * rand()) % (p2 - v3) + v3;
  pw1[0] = pw2[0] = pw3[0] = 1;
  for (int i = 1; i <= 500005; ++i) {
    pw1[i] = (long long)pw1[i - 1] * v1 % p1;
    pw2[i] = (long long)pw2[i - 1] * v2 % p2;
    pw3[i] = (long long)pw3[i - 1] * v3 % p3;
  }
  int T;
  read(T);
  while (T--) solve();
  return 0;
}
