#include <bits/stdc++.h>
using namespace std;
inline int in() {
  char ch = getchar();
  int f = 1, x = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
  return f * x;
}
const int inf = (1 << 30);
const int mod = 998244353;
const int N = 500010;
long long c[N];
unsigned long long po[N], cc[N], p = 233;
int n, m;
map<unsigned long long, long long> mm;
long long gcd(long long a, long long b) { return (!b) ? a : gcd(b, a % b); }
int main() {
  int T = in();
  po[0] = 1;
  for (int i = 1; i <= 500000; ++i) po[i] = po[i - 1] * p;
  while (T--) {
    mm.clear();
    n = in(), m = in();
    for (int i = 1; i <= n; ++i) scanf("%I64d", &c[i]), cc[i] = 0;
    for (int i = 1; i <= m; ++i) {
      int u = in(), v = in();
      cc[v] += po[u];
    }
    for (int i = 1; i <= n; ++i)
      if (cc[i]) {
        mm[cc[i]] += c[i];
      }
    long long ans = -1;
    for (auto v : mm) {
      if (ans == -1)
        ans = v.second;
      else
        ans = gcd(ans, v.second);
    }
    printf("%I64d\n", ans);
  }
  return 0;
}
