#include <bits/stdc++.h>
using namespace std;
int VECPOW1[500069];
int VECPOW2[500069];
long long arr[500069];
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
pair<int, int> rhash(vector<int>& arr) {
  pair<int, int> val = make_pair(0, 0);
  for (int i = 0; i < arr.size(); i++) {
    val.first = (val.first + 1ll * (arr[i] + 1) * VECPOW1[i]) % 1000002193;
    val.second = (val.second + 1ll * (arr[i] + 1) * VECPOW2[i]) % 1000011769;
  }
  return val;
}
void pre() {
  VECPOW1[0] = 1;
  VECPOW2[0] = 1;
  for (int i = 1; i < 500069; i++) {
    VECPOW1[i] = (1ll * 1000003 * VECPOW1[i - 1]) % 1000002193;
    VECPOW2[i] = (1ll * 1000003 * VECPOW2[i - 1]) % 1000011769;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  pre();
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    vector<int> adj[n];
    for (int i = 0; i < n; i++) cin >> arr[i];
    for (int i = 0; i < m; i++) {
      int u, v;
      cin >> u >> v;
      u--;
      v--;
      adj[v].push_back(u);
    }
    map<pair<int, int>, long long> node;
    for (int i = 0; i < n; i++) {
      if (adj[i].size() == 0) continue;
      sort(adj[i].begin(), adj[i].end());
      node[rhash(adj[i])] += arr[i];
    }
    long long ans = -1;
    for (auto p : node) ans = (ans == -1) ? p.second : gcd(ans, p.second);
    cout << ans << '\n';
  }
}
