#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
vector<vector<int> > G;
bool comp(pair<int, int> a, pair<int, int> b) {
  if (a.first != b.first) return a.first < b.first;
  for (int i = 0; i < a.first; ++i) {
    if (G[a.second][i] < G[b.second][i]) return true;
    if (G[a.second][i] > G[b.second][i]) return false;
  }
  return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    G = vector<vector<int> >(n);
    vector<long long> val(n);
    for (int i = 0; i < n; ++i) {
      cin >> val[i];
    }
    for (int i = 0; i < m; ++i) {
      int x, y;
      cin >> x >> y;
      --x;
      --y;
      G[y].push_back(x);
    }
    vector<pair<int, int> > D(n);
    for (int i = 0; i < n; ++i) {
      D[i] = pair<int, int>(G[i].size(), i);
      sort(G[i].begin(), G[i].end());
    }
    sort(D.begin(), D.end(), comp);
    long long res = 0;
    long long sum = 0;
    for (int i = 0; i < n; ++i) {
      if (!D[i].first) continue;
      if (i and G[D[i].second] != G[D[i - 1].second]) {
        res = gcd(res, sum);
        sum = val[D[i].second];
      } else
        sum += val[D[i].second];
    }
    res = gcd(res, sum);
    cout << res << '\n';
  }
}
