#include <bits/stdc++.h>
using namespace std;
const int maxn = 500005, maxm = 55;
const double pi = acos(-1.0);
const long long mod = 1000000007;
const double eps = 1e-6;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int T, n, m;
long long c[maxn];
vector<int> E[maxn];
long long ha[maxn];
int id[maxn];
bool cmp(int i, int j) {
  int si = E[i].size(), sj = E[j].size();
  if (si != sj) return si < sj;
  return ha[i] < ha[j];
}
int main() {
  int i, j, k;
  scanf("%d", &T);
  for (int ca = 1; ca <= T; ca++) {
    scanf("%d %d", &n, &m);
    for (i = 0; i < n; i++) {
      scanf("%I64d", c + i);
      E[i].clear();
    }
    for (i = 0; i < m; i++) {
      int x, y;
      scanf("%d %d", &x, &y);
      E[y - 1].push_back(x);
    }
    for (i = 0; i < n; i++) {
      id[i] = i;
      ha[i] = 1;
      for (j = 0; j < E[i].size(); j++) ha[i] = ha[i] * E[i][j] % mod;
    }
    sort(id, id + n, cmp);
    long long ans = 0;
    for (i = 0; i < n;) {
      int sz = E[id[i]].size();
      if (sz == 0) {
        ++i;
        continue;
      }
      long long ct = 0;
      for (j = i; j < n && E[id[j]].size() == sz && ha[id[j]] == ha[id[i]]; j++)
        ct += c[id[j]];
      ans = gcd(ans, ct);
      i = j;
    }
    printf("%I64d\n", ans);
  }
  return 0;
}
