#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);
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    map<vector<int>, long long> ma;
    int n, m, a, b;
    scanf("%d%d", &n, &m);
    vector<long long> values(n);
    vector<vector<int> > adj(n);
    for (int i = 0; i < n; i++) {
      scanf("%lld", &values[i]);
    }
    for (int i = 0; i < m; i++) {
      scanf("%d%d", &a, &b);
      adj[b - 1].push_back(a - 1);
    }
    for (int i = 0; i < n; i++) {
      if (adj[i].empty()) continue;
      sort(adj[i].begin(), adj[i].end());
      ma[adj[i]] += values[i];
    }
    long long ans = 0;
    map<vector<int>, long long>::iterator it;
    for (it = ma.begin(); it != ma.end(); ++it) {
      ans = gcd(ans, it->second);
    }
    printf("%lld\n", ans);
  }
}
