#include <bits/stdc++.h>
using namespace std;
const int N_MAX = 500000;
int t, n, m, atom[N_MAX], s[N_MAX], A;
int lst_done[N_MAX];
vector<int> in_atom[N_MAX];
queue<int> todo;
long long c[N_MAX], sum[N_MAX];
bool covered[N_MAX];
vector<int> G[N_MAX];
long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> t;
  while (t--) {
    cin >> n >> m;
    for (int i = 0; i < n; i++) cin >> c[i];
    for (int i = 0; i < n; i++) {
      covered[i] = false;
      G[i].clear();
    }
    for (int i = 0; i < m; i++) {
      int u, v;
      cin >> u >> v;
      u--;
      v--;
      covered[v] = true;
      G[u].push_back(v);
    }
    for (int i = 0; i < n; i++) lst_done[i] = -1;
    for (int i = 0; i < n; i++) atom[i] = 0;
    s[0] = n;
    A = 1;
    for (int u = 0; u < n; u++) {
      for (int v : G[u]) {
        if (lst_done[atom[v]] != u) {
          todo.push(atom[v]);
          lst_done[atom[v]] = u;
        }
        in_atom[atom[v]].push_back(v);
      }
      while (!todo.empty()) {
        int a = todo.front();
        todo.pop();
        if (s[a] != in_atom[a].size()) {
          s[A] = in_atom[a].size();
          s[a] -= in_atom[a].size();
          for (int v : in_atom[a]) atom[v] = A;
          A++;
        }
        in_atom[a].clear();
      }
    }
    for (int i = 0; i < A; i++) sum[i] = 0;
    for (int i = 0; i < n; i++)
      if (covered[i]) sum[atom[i]] += c[i];
    long long sol = sum[0];
    for (int i = 1; i < A; i++) sol = gcd(max(sol, sum[i]), min(sol, sum[i]));
    cout << sol << "\n";
  }
  return 0;
}
