#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline T1 max(T1 a, T2 b) {
  return a < b ? b : a;
}
template <typename T1, typename T2>
inline T1 min(T1 a, T2 b) {
  return a < b ? a : b;
}
const char lf = '\n';
namespace ae86 {
const int bufl = 1 << 15;
char buf[bufl], *s = buf, *t = buf;
inline int fetch() {
  if (s == t) {
    t = (s = buf) + fread(buf, 1, bufl, stdin);
    if (s == t) return EOF;
  }
  return *s++;
}
inline long long ty() {
  long long a = 0;
  int b = 1, c = fetch();
  while (!isdigit(c)) b ^= c == '-', c = fetch();
  while (isdigit(c)) a = a * 10 + c - 48, c = fetch();
  return b ? a : -a;
}
}  // namespace ae86
using ae86::ty;
long long gcd(long long a, long long b) {
  while (b) a %= b, swap(a, b);
  return a;
}
const int _ = 500007, mo = 998244853, bas = 18181;
long long powa(long long a, long long t) {
  long long b = 1;
  while (t) {
    if (t & 1) b = b * a % mo;
    a = a * a % mo, t >>= 1;
  }
  return b;
}
int n, m, cnt[_], got[_];
long long val[_] = {0}, ps[_];
void finder() {
  n = ty(), m = ty();
  for (int i = 1; i <= n; i++) val[i] = ty(), cnt[i] = 0, ps[i] = 0, got[i] = 0;
  for (int i = 1, a, b; i <= m; i++)
    a = ty(), b = ty(), ps[b] = (ps[b] + powa(bas, a)) % mo, cnt[b]++;
  map<pair<long long, int>, int> buc;
  for (int i = 1; i <= n; i++) {
    got[i] = 1;
    pair<long long, int> ha = make_pair(ps[i], cnt[i]);
    if (buc.count(ha))
      val[buc[ha]] += val[i], got[i] = 0;
    else
      buc[ha] = i;
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++)
    if (got[i] && cnt[i]) ans = gcd(ans, val[i]);
  cout << ans << lf;
}
int main() {
  ios::sync_with_stdio(0), cout.tie(nullptr);
  int t = ty();
  while (t--) finder();
  return 0;
}
