#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-') f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = (x << 1) + (x << 3) + ch - '0';
  return x * f;
}
const long long N = 5e5 + 5;
long long a[N];
vector<long long> G[N];
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
signed main() {
  long long T = read();
  while (T--) {
    long long n = read(), m = read();
    for (long long i = 1; i <= n; ++i) a[i] = read();
    for (long long i = 1; i <= n; ++i) G[i].clear();
    for (long long i = 1; i <= m; ++i) {
      long long u = read(), v = read();
      G[v].push_back(u);
    }
    for (long long i = 1; i <= n; ++i) sort(G[i].begin(), G[i].end());
    map<vector<long long>, long long> M;
    long long ans = 0;
    for (long long i = 1; i <= n; ++i)
      if (G[i].size()) M[G[i]] += a[i];
    for (auto i : M) ans = gcd(ans, i.second);
    printf("%lld\n", ans);
  }
  return 0;
}
