#include <bits/stdc++.h>
#pragma GCC optimize(2)
const int maxn = 5e5 + 5;
int n, m, deg[maxn], rnk[maxn];
std::vector<int> e[maxn], E[maxn];
long long val[maxn];
bool del[maxn];
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
inline bool Comp(const int &Alpha, const int &Beta) {
  return deg[Alpha] < deg[Beta];
}
int main() {
  int cas;
  scanf("%d", &cas);
  while (cas--) {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) scanf("%lld", &val[i]);
    for (int i = 1; i <= n; i++) e[i].clear(), E[i].clear(), del[i] = false;
    for (int i = 1; i <= m; i++) {
      int u, v;
      scanf("%d%d", &u, &v);
      e[u].push_back(v);
      E[v].push_back(u);
    }
    long long answer = 0, sum = 0;
    for (int i = 1; i <= n; i++)
      for (int v : e[i])
        if (++deg[v] == 1) sum += val[v];
    answer = sum;
    for (int i = 1; i <= n; i++) rnk[i] = i;
    std::sort(rnk + 1, rnk + n + 1, Comp);
    for (int i = 1; i <= n; i++) {
      for (int v : E[rnk[i]]) {
        if (del[v]) continue;
        del[v] = 1;
        for (int t : e[v])
          if (--deg[t] == 0) sum -= val[t];
      }
      answer = gcd(answer, sum);
    }
    for (int i = 1; i <= n; i++) {
      sum = 0;
      for (int v : e[i]) sum += val[v];
      answer = gcd(answer, sum);
    }
    printf("%lld\n", answer);
  }
  return 0;
}
