#include <bits/stdc++.h>
using namespace std;
inline int re_adi() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') x = x * 10 + (ch ^ 48), ch = getchar();
  return x * f;
}
inline long long re_adl() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') x = x * 10 + (ch ^ 48), ch = getchar();
  return x * f;
}
int T;
int n, m;
long long c[500010];
set<int> g[500010];
map<set<int>, long long> M;
long long ans;
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
int main() {
  register int i, x, y, sz, j, v;
  T = re_adi();
  while (T--) {
    for (i = 1; i <= n; ++i) g[i].clear();
    M.clear();
    n = re_adi();
    m = re_adi();
    for (i = 1; i <= n; ++i) c[i] = re_adl();
    for (i = 1; i <= m; ++i) x = re_adi(), y = re_adi(), g[y].insert(x);
    for (i = 1; i <= n; ++i)
      if (!g[i].empty()) M[g[i]] += c[i];
    ans = 0;
    map<set<int>, long long>::iterator it;
    for (it = M.begin(); it != M.end(); ++it) ans = gcd(ans, it->second);
    printf("%lld\n", ans);
  }
}
