#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-7;
const int N = 1000010;
const int M = 200010;
const int P = 1000000007;
const int Inf = 1000000007;
const int Fni = -1000000007;
inline long long qr() {
  long long r = 0, f = 1;
  char c = getchar();
  while (c > '9' || c < '0') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    r = (r << 1) + (r << 3) + c - '0', c = getchar();
  }
  return r * f;
}
template <typename T>
void debug(T x, char c = '\n') {
  cout << x << c;
}
template <typename T>
void debug(T *const tp, int minn, int maxn, char v = ' ', char c = '\n') {
  for (int i = minn; i <= maxn; ++i) debug(tp[i], v);
  cout << c;
}
template <typename T>
inline void chkmin(T &x, T y) {
  x = x > y ? y : x;
}
template <typename T>
inline void chkmax(T &x, T y) {
  x = x < y ? y : x;
}
template <typename T>
inline void add(T &x, long long y, int mod = P) {
  x += y;
  x = x >= mod ? x - mod : x;
}
template <typename T>
inline void dec(T &x, long long y, int mod = P) {
  x -= y;
  x = x < 0 ? x + mod : x;
}
template <typename T>
inline T addn(T x, long long y, int mod = P) {
  x += y;
  return (x = x > mod ? x - mod : x);
}
template <typename T>
inline T decn(T x, long long y, int mod = P) {
  x -= y;
  return (x = x < 0 ? x + mod : x);
}
const int Base = 137;
int T;
int n, m;
unordered_map<int, long long> h;
long long ans;
long long val[N];
long long base[N];
vector<int> e[N];
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long do_hash(int x) {
  long long ret = 0;
  sort(e[x].begin(), e[x].end());
  for (auto k : e[x]) ret = (ret * Base % P + k) % P;
  return ret;
}
int main() {
  cin >> T;
  while (T--) {
    n = qr(), m = qr();
    int x, y;
    ans = 0;
    h.clear();
    for (int i = 1; i <= n; ++i) base[i] = qr(), e[i].clear();
    for (int i = 1; i <= m; ++i) x = qr(), y = qr(), e[y].push_back(x);
    for (int i = 1; i <= n; ++i) h[do_hash(i)] += base[i];
    for (auto k : h)
      if (k.first) ans = gcd(ans, k.second);
    printf("%lld\n", ans);
  }
  return 0;
}
