#include <bits/stdc++.h>
void rd(int &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
  x *= f;
}
void lrd(long long &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
  x *= f;
}
const int INF = 1e9;
const long long LINF = 1e18;
const int N = 5e5 + 10;
const int mod = 1e9 + 7;
using namespace std;
int T;
int n, m;
map<int, long long> S;
long long c[N];
vector<int> f[N];
long long gcd(long long x, long long y) { return !y ? x : gcd(y, x % y); }
int main() {
  rd(T);
  while (T--) {
    rd(n);
    rd(m);
    S.clear();
    for (int i = 1; i <= n; i++) lrd(c[i]), f[i].clear();
    for (int i = 1; i <= m; i++) {
      int x, y;
      rd(x);
      rd(y);
      f[y].push_back(x);
    }
    for (int i = 1; i <= n; i++) sort(f[i].begin(), f[i].end());
    for (int i = 1; i <= n; i++) {
      if (!f[i].size()) continue;
      int hs = 0;
      for (int j = 0; j < f[i].size(); j++)
        hs = (1ll * hs * (n + 1) + f[i][j]) % mod;
      if (S.find(hs) == S.end())
        S[hs] = c[i];
      else
        S[hs] += c[i];
    }
    map<int, long long>::iterator it;
    long long ans = 0;
    for (it = S.begin(); it != S.end(); it++)
      if (ans)
        ans = gcd(ans, it->second);
      else
        ans = it->second;
    printf("%lld\n", ans);
  }
  return 0;
}
