#include <bits/stdc++.h>
using namespace std;
inline int in() {
  int x, y;
  y = scanf("%d", &x);
  return x;
}
const int N = 5e5 + 123;
const long long mod = 1e9 + 9;
const long long p1 = 343;
const long long p2 = 17;
long long a[N];
pair<long long, long long> h[N];
map<pair<long long, long long>, vector<int> > ma;
long long gcd(long long l, long long r) {
  if (r == 0) return l;
  return gcd(r, l % r);
}
long long pow(long long n, long long k) {
  if (k == 0) return 1;
  return pow((n * n) % mod, k / 2) * (k % 2 ? n : 1) % mod;
}
void add(pair<long long, long long> &a, long long d) {
  a.first = (a.first + pow(p1, d)) % mod;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < (n); i++) cin >> a[i];
    while (m--) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      add(h[b], a);
    }
    for (int i = 0; i < (n); i++) {
      ma[h[i]].emplace_back(i);
    }
    long long g = 0;
    for (auto [k, v] : ma) {
      if (k.first == 0) continue;
      long long sum = 0;
      for (auto u : v) sum += a[u];
      g = gcd(g, sum);
    }
    cout << g << endl;
    for (int i = 0; i < (n); i++) h[i] = {0, 0};
    ma.clear();
  }
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}
