#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx")
using namespace std;
long long gcd(long long a, long long b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}
void solve() {
  long long n, m;
  cin >> n >> m;
  vector<vector<long long>> g(n);
  vector<long long> c(n);
  vector<long long> sum(n);
  for (long long i = 0; i < n; ++i) {
    cin >> c[i];
  }
  for (long long i = 0; i < m; ++i) {
    long long u, v;
    cin >> u >> v;
    u--, v--;
    g[v].push_back(u);
  }
  map<vector<long long>, long long> mp;
  for (long long i = 0; i < n; ++i) {
    if (!g[i].empty()) {
      sort(g[i].begin(), g[i].end());
      mp[g[i]] += c[i];
    }
  }
  long long ans = -1;
  for (auto u : mp) {
    if (ans == -1)
      ans = u.second;
    else
      ans = gcd(ans, u.second);
  }
  cout << ans << '\n';
}
signed main() {
  std::ios_base::sync_with_stdio(0), cin.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
