#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target( \
    "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("section-anchors")
#pragma GCC optimize("profile-values,profile-reorder-functions,tracer")
#pragma GCC optimize("vpt")
#pragma GCC optimize("rename-registers")
#pragma GCC optimize("move-loop-invariants")
#pragma GCC optimize("unswitch-loops")
#pragma GCC optimize("function-sections")
#pragma GCC optimize("data-sections")
#pragma GCC optimize("branch-target-load-optimize")
#pragma GCC optimize("branch-target-load-optimize2")
#pragma GCC optimize("btr-bb-exclusive")
using namespace std;
const int p = 1e2 + 17;
const int maxn = 5e5 + 10;
long long st[maxn];
const int mod = 1e9 + 7;
int gcd(int a, int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  st[0] = 1;
  for (int l = 1; l < maxn; ++l) {
    st[l] = (st[l - 1] * p) % mod;
  }
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int m;
    cin >> m;
    vector<pair<long long, long long>> f(n);
    for (int i = 0; i < n; ++i) {
      cin >> f[i].second;
    }
    for (int j = 0; j < m; ++j) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      f[b].first -= st[a + 1];
      f[b].first %= mod;
    }
    sort(f.begin(), f.end());
    while (f.size() > 0 && f.back().first == 0) f.pop_back();
    long long ans = 0;
    long long sum = f[0].second;
    for (int i = 1; i < f.size(); ++i) {
      if (f[i].first != f[i - 1].first) {
        ans = gcd(ans, sum);
        sum = f[i].second;
      } else {
        sum += f[i].second;
      }
    }
    ans = gcd(ans, sum);
    cout << ans << "\n";
  }
}
