#include <bits/stdc++.h>
using namespace std;
long long const mod = 1e9 + 9;
long long val[5000001];
std::vector<long long> adj[5000001], adjr[5000001];
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    for (long long i = 1; i <= n; ++i) {
      cin >> val[i];
      adjr[i].clear();
    }
    for (long long i = 0; i < m; ++i) {
      long long x, y;
      cin >> x >> y;
      adjr[y].push_back(x);
    }
    unordered_map<int32_t, long long> hasher;
    long long prime = 999983;
    for (long long i = 1; i <= n; ++i) {
      long long hash = 0;
      long long powr = prime;
      if (adjr[i].size() > 0) {
        sort(adjr[i].begin(), adjr[i].end());
        for (auto &j : adjr[i]) {
          hash += (j * powr) % mod;
          powr = (powr * prime) % mod;
        }
        hasher[hash] += val[i];
      }
    }
    long long ans = 0;
    for (auto &i : hasher) {
      ans = gcd(ans, i.second);
    }
    cout << ans << "\n";
  }
}
