#include <bits/stdc++.h>
using namespace std;
template <class T>
istream &operator>>(istream &is, vector<T> &a) {
  for (int i = 0; i < a.size(); ++i) is >> a[i];
  return is;
}
int base1 = 1009, base2 = 1063, mod1 = 1000000103, mod2 = 1000000271;
vector<long long> pow1, pow2;
int maxn = 5 * 100000;
void init() {
  pow1.resize(maxn), pow2.resize(maxn);
  long long cur = 1LL;
  for (int i = 0; i < maxn; ++i, cur *= base1, cur %= mod1) pow1[i] = cur;
  cur = 1LL;
  for (int i = 0; i < maxn; ++i, cur *= base2, cur %= mod2) pow2[i] = cur;
}
pair<long long, long long> get_hash(set<int> a) {
  long long ans1 = 1, ans2 = 1;
  for (int i : a) {
    ans1 = (ans1 + pow1[i]) % mod1;
    ans2 = (ans2 + pow2[i]) % mod2;
  }
  return {ans1, ans2};
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int t;
  cin >> t;
  init();
  while (t--) {
    int n, m;
    cin >> n >> m;
    vector<long long> c(n);
    cin >> c;
    vector<set<int>> g(n);
    for (int i = 0; i < m; ++i) {
      int u, v;
      cin >> u >> v;
      --u, --v;
      g[v].insert(u);
    }
    map<pair<long long, long long>, set<int>> map1;
    for (int i = 0; i < n; ++i) map1[get_hash(g[i])].insert(i);
    long long ans = 0;
    for (auto i : map1) {
      long long sum = 0;
      for (int j : i.second)
        if (g[j].size()) sum += c[j];
      ans = gcd(ans, sum);
    }
    cout << ans << "\n";
  }
}
