#include <bits/stdc++.h>
using namespace std;
const int N = 500000 + 10;
const int MOD = 1000000007;
long long c[N];
vector<int> G[N];
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  long long res;
  int n, m, t, val, u, v, s, i, j, k;
  scanf("%d", &t);
  while (t--) {
    map<int, vector<int> > H;
    scanf("%d %d", &n, &m);
    for (i = 1; i <= n; i++) scanf("%lld", &c[i]);
    for (i = 0; i < m; i++) {
      scanf("%d %d", &u, &v);
      G[v].push_back(u);
    }
    for (i = 1; i <= n; i++) sort(G[i].begin(), G[i].end());
    for (i = 1; i <= n; i++) {
      if (G[i].size() == 0) {
        c[i] = 0;
        continue;
      }
      val = 0;
      for (j = 0; j < G[i].size(); j++)
        val = (1LL * val * n % MOD + G[i][j] - 1) % MOD;
      if (H.find(val) != H.end()) {
        vector<int> tmp = H[val];
        for (s = 0; s < tmp.size(); s++) {
          k = tmp[s];
          if (G[k].size() == G[i].size()) {
            for (j = 0; j < G[k].size(); j++)
              if (G[k][j] != G[i][j]) break;
            if (j == G[k].size()) {
              c[k] += c[i];
              c[i] = 0;
              break;
            }
          }
        }
        if (s == tmp.size()) H[val].push_back(i);
      } else
        H[val].push_back(i);
    }
    res = 0;
    for (i = 1; i <= n; i++) res = gcd(res, c[i]);
    cout << res << endl;
    for (i = 1; i <= n; i++) G[i].clear();
    H.clear();
  }
  return 0;
}
