#include <bits/stdc++.h>
using namespace std;
long long gcd(long long x, long long y) { return y == 0 ? x : gcd(y, x % y); }
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
void solve() {
  int n, m;
  scanf("%d%d", &n, &m);
  vector<long long> w(n + 1), c(n + 1), adj(n + 1);
  map<long long, long long> mp;
  for (int i = 1; i <= n; ++i) {
    scanf("%llu", &c[i]);
    w[i] = rng();
  }
  for (int i = 1; i <= m; ++i) {
    static int u, v;
    scanf("%d%d", &u, &v);
    adj[v] ^= w[u];
  }
  for (int i = 1; i <= n; ++i) mp[adj[i]] += c[i];
  long long ans = 0;
  for (auto g : mp)
    if (g.first != 0) ans = gcd(ans, g.second);
  printf("%llu\n", ans);
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) solve();
  return 0;
}
