#include <bits/stdc++.h>
using namespace std;
const int maxn = 500000 + 10;
long long c[maxn], sum[maxn];
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
vector<int> son[maxn];
int fa[maxn];
int ffind(int x) { return fa[x] == x ? fa[x] : fa[x] = ffind(fa[x]); }
const int mod = 1000000000 + 7;
long long h[maxn];
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n, m;
    scanf("%d %d", &n, &m);
    map<long long, int> mp;
    for (int i = 1; i <= n; i++) {
      son[i].clear();
      h[i] = 0;
      fa[i] = i;
    }
    memset(sum, 0, (n + 5) * sizeof(long long));
    for (int i = 1; i <= n; i++) {
      scanf("%lld", c + i);
    }
    for (int i = 1; i <= m; i++) {
      int u, v;
      scanf("%d %d", &u, &v);
      son[v].push_back(u);
    }
    for (int i = 1; i <= n; i++) {
      sort(son[i].begin(), son[i].end());
      for (int j = 0; j < (int)son[i].size(); j++) {
        h[i] = (h[i] * 233333 + son[i][j]) % mod;
      }
      if (mp.count(h[i])) {
        fa[ffind(mp[h[i]])] = i;
      }
      mp[h[i]] = i;
    }
    long long ans = 0;
    for (int i = 1; i <= n; i++) {
      sum[ffind(i)] += c[i];
    }
    for (int i = 1; i <= n; i++) {
      if (ffind(i) == i && son[i].size()) {
        ans = gcd(ans, sum[i]);
      }
    }
    printf("%lld\n", ans);
  }
  getchar();
  getchar();
  return 0;
}
