#include <bits/stdc++.h>
using namespace std;
const long long int mod1 = 1000000007;
const long long int mod2 = 998244353;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long int gcd(long long int a, long long int b) {
  long long int c;
  while (b) {
    c = b;
    b = a % b;
    a = c;
  }
  return a;
}
long long int power1(long long int a, long long int b) {
  if (b == 0)
    return 1;
  else if (b % 2)
    return a * power1(a, b - 1);
  else {
    long long int temp = power1(a, b / 2);
    return temp * temp;
  }
}
long long int power2(long long int a, long long int b, long long int m) {
  a %= m;
  if (b == 0)
    return 1;
  else if (b % 2)
    return (a * power2(a, b - 1, m)) % m;
  else {
    long long int temp = power2(a, b / 2, m);
    return (temp * temp) % m;
  }
}
long long int mod(long long int x, long long int p) { return (x % p + p) % p; }
long long int lcm(long long int x, long long int y) {
  return (x * y) / gcd(x, y);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long long int tt = 1;
  cin >> tt;
  while (tt--) {
    long long int n, m1;
    cin >> n >> m1;
    long long int c[n];
    for (int i = 0; i < n; i++) {
      cin >> c[i];
    }
    vector<long long int> g[n];
    for (int i = 0; i < m1; i++) {
      long long int x, y;
      cin >> x >> y;
      x--;
      y--;
      g[y].push_back(x);
    }
    map<pair<long long int, long long int>, long long int> m;
    for (int i = 0; i < n; i++) {
      vector<long long int> cur = g[i];
      sort(cur.begin(), cur.end());
      long long int st1 = 1, st2 = 1;
      long long int p1 = 12345679, p2 = 87654321;
      long long int su1 = 0, su2 = 0;
      for (auto x : cur) {
        su1 += (st1 * x) % mod1;
        su1 %= mod1;
        su2 += (st2 * x) % mod2;
        su2 %= mod2;
        st1 = (st1 * p1) % mod1;
        st2 = (st2 * p2) % mod2;
      }
      if (cur.size()) {
        m[{su1, su2}] += c[i];
      }
    }
    long long int out = 0;
    for (auto q : m) {
      out = gcd(out, q.second);
    }
    cout << out << endl;
  }
}
