#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 7;
int t, n, m;
long long c[N], h[N];
unsigned long long g[N];
struct node {
  unsigned long long a;
  long long x;
} e[N];
inline unsigned long long rnd() {
  unsigned long long u = 1;
  u = u * rand() * rand() * rand() * rand();
  return u;
}
inline long long gcd(long long a, long long b) {
  if (!b) return a;
  return gcd(b, a % b);
}
inline bool cmp(node a, node b) { return a.a < b.a; }
int main() {
  cin >> t;
  while (t--) {
    scanf("%d%d", &n, &m);
    int a, b;
    long long sum = 0;
    for (int i = 1; i <= n; i++)
      scanf("%lld", &c[i]), h[i] = 0, e[i].x = c[i], e[i].a = 0;
    for (int i = 1; i <= n; i++) g[i] = rnd();
    for (int i = 1; i <= m; i++) {
      scanf("%d%d", &a, &b);
      h[a] += c[b];
      e[b].a ^= g[a];
    }
    for (int i = 1; i <= n; i++) sum = gcd(sum, h[i]);
    sort(e + 1, e + n + 1, cmp);
    for (int i = 1; i <= n; i++) {
      long long q = e[i].x;
      int x = i;
      while (e[x + 1].a == e[i].a && x + 1 <= n) q += e[x + 1].x, x++;
      if (e[i].a > 0) sum = gcd(sum, q);
      i = x;
    }
    printf("%lld\n", sum);
  }
  return 0;
}
