#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  if (a == 0) return b;
  return gcd(b, a % b);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> g(n, vector<int>());
    vector<long long> c(n);
    for (long long& x : c) cin >> x;
    while (m--) {
      int x, y;
      cin >> x >> y;
      g[--y].push_back(--x);
    }
    map<vector<int>, long long> s;
    for (int v = 0; v < n; v++) {
      if ((int)(g[v]).size() == 0) continue;
      sort((g[v]).begin(), (g[v]).end());
      long long h = 0;
      if ((int)(g[v]).size()) {
        s[g[v]] += c[v];
      }
    }
    long long ans = 0;
    for (auto& e : s) ans = gcd(ans, e.second);
    cout << ans << '\n';
  }
  return 0;
}
