#include <bits/stdc++.h>
using namespace std;
int T, n, m;
long long ans, c[500005];
set<int> g[500005];
map<set<int>, long long> mp;
long long gcd(long long x, long long y) {
  if (!y) return x;
  return gcd(y, x % y);
}
int main() {
  for (scanf("%d", &T); T--;) {
    mp.clear();
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) scanf("%lld", &c[i]);
    for (int i = 1; i <= n; i++) g[i].clear();
    for (int u, v; m--;) {
      scanf("%d%d", &u, &v);
      g[v].insert(u);
    }
    for (int i = 1; i <= n; i++)
      if (!g[i].empty()) mp[g[i]] += c[i];
    ans = 0;
    for (auto i : mp)
      if (ans)
        ans = gcd(ans, i.second);
      else
        ans = i.second;
    printf("%lld\n", ans);
  }
}
