#include <bits/stdc++.h>
int read() {
  static int c, x;
  while ((c = getchar()) < 48) {
  }
  x = c & 15;
  while ((c = getchar()) >= 48) x = x * 10 + (c & 15);
  return x;
}
long long readll() {
  static int c;
  static long long x;
  while ((c = getchar()) < 48) {
  }
  x = c & 15;
  while ((c = getchar()) >= 48) x = x * 10 + (c & 15);
  return x;
}
int ord[500010];
long long a[500010];
std::vector<int> e[500010];
long long gcd(long long x, long long y) {
  while (y) {
    const long long t = x % y;
    x = y;
    y = t;
  }
  return x;
}
inline bool cmp(const int &x, const int &y) {
  return (e[x].size() != e[y].size()) ? (e[x].size() < e[y].size())
                                      : (e[x] < e[y]);
}
int main() {
  for (int T = read(); T--;) {
    const int n = read();
    int m = read();
    for (int i = 1; i <= n; ++i) {
      e[i].clear();
      a[i] = readll();
    }
    while (m--) {
      const int x = read();
      e[read()].push_back(x);
    }
    for (int i = 1; i <= n; ++i) {
      ord[i] = i;
      std::sort(e[i].begin(), e[i].end());
    }
    std::sort(ord + 1, ord + n + 1, cmp);
    long long ans = 0, tot = 0;
    for (int i = 1; i <= n; ++i) {
      if (e[ord[i]].empty()) continue;
      if (e[ord[i]] != e[ord[i - 1]]) {
        ans = gcd(ans, tot);
        tot = 0;
      }
      tot += a[ord[i]];
    }
    ans = gcd(ans, tot);
    printf("%lld\n", ans);
  }
  return 0;
}
