#include <bits/stdc++.h>
using namespace std;
const int MaxN = 500001;
const int q = 27;
const int p = 27644437;
long long v[MaxN];
vector<int> es[MaxN];
unordered_map<int, int> ids;
unordered_map<int, long long> h;
long long gcd(long long x, long long y) {
  long long t = x % y;
  while (t != 0) {
    x = y;
    y = t;
    t = x % y;
  }
  return y;
}
int hash_value(const vector<int>& edge, int id) {
  int res = 0;
  for (int i : edge) {
    res = (res * q + i) % p;
  }
  while (true) {
    if (ids.find(res) == ids.end()) {
      ids[res] = id;
      h[res] = 0;
      return res;
    } else {
      int t = ids[res];
      bool flag = true;
      if (es[t].size() != edge.size()) {
        flag = false;
      } else {
        for (int j = 0; j < edge.size(); ++j) {
          if (es[t][j] != edge[j]) {
            flag = false;
            break;
          }
        }
      }
      if (flag) {
        return res;
      }
    }
    res += 1;
  }
}
void solve() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i) {
    scanf("%lld", &v[i]);
    es[i].clear();
  }
  ids.clear();
  h.clear();
  int x, y;
  for (int i = 0; i < m; ++i) {
    scanf("%d %d", &x, &y);
    es[y - 1].push_back(x);
  }
  for (int i = 0; i < n; ++i) {
    if (es[i].size() == 0) {
      continue;
    }
    sort(es[i].begin(), es[i].end());
    int hv = hash_value(es[i], i);
    h[hv] += v[i];
  }
  long long ans = 0;
  for (const auto& it : h) {
    ans = gcd(ans, it.second);
  }
  printf("%lld\n", ans);
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    solve();
  }
  return 0;
}
