#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
int in() {
  int x;
  cin >> x;
  return x;
}
long long lin() {
  long long x;
  cin >> x;
  return x;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline void print(pair<T, T> p) {
  cout << "(" << p.first << "," << p.second << ") ";
}
template <class T>
inline void print(vector<pair<T, T>> v) {
  for (auto e : v) print(e);
  cout << "\n";
}
template <class T>
inline void print(T v) {
  for (auto e : v) cout << e << " ";
  cout << "\n";
}
unsigned xorshift() {
  static unsigned x = 123456789, y = 362436069, z = 521288629, w = 8867512;
  unsigned t = (x ^ (x << 11));
  x = y;
  y = z;
  z = w;
  return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int n) { return uniform_int_distribution<int>(0, INT_MAX)(rng) % n; }
double drnd() { return (double)(rnd(INT_MAX)) / INT_MAX; }
void shuffle(vector<int>& x) {
  int n = x.size();
  for (long long i = n - 1; i >= 1; i--) {
    swap(x[i], x[rnd(i - 1)]);
  }
}
clock_t start;
double time() { return (double)(clock() - start) / CLOCKS_PER_SEC; }
long long M[4] = {1000000000000037, 9007199254740997, 92709568269121,
                  9999999999999937};
long long rh[4][510000];
long long gcd(long long x, long long y) {
  if (y == 0) return x;
  return gcd(y, x % y);
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int T = in();
  while (T--) {
    int n = in(), m = in();
    vector<long long> a;
    for (long long i = 0; i < n; i++) a.push_back(lin());
    vector<long long> p[4];
    long long k[4];
    for (long long i = 0; i < 4; i++) k[i] = rnd(100) + 2;
    long long t[4] = {1, 1, 1, 1};
    for (long long i = 0; i < n + 1; i++) {
      for (long long j = 0; j < 4; j++) p[j].push_back(t[j]);
      for (long long j = 0; j < 4; j++) t[j] = t[j] * k[j] % M[j];
    }
    for (long long i = 0; i < 4; i++)
      for (long long j = 0; j < n + 1; j++) rh[i][j] = 0;
    for (long long i = 0; i < m; i++) {
      int x = in(), y = in();
      for (long long j = 0; j < 4; j++) {
        rh[j][y] = (rh[j][y] + p[j][x]) % M[j];
      }
    }
    map<vector<long long>, long long> mp;
    for (long long i = 1; i <= n; ++i) {
      vector<long long> v;
      for (long long j = 0; j < 4; j++) v.emplace_back(rh[j][i]);
      bool flag = 0;
      for (auto e : v)
        if (e) flag = 1;
      if (!flag) continue;
      mp[v] += a[i - 1];
    }
    long long g = 0;
    for (auto e : mp) {
      g = gcd(g, e.second);
    }
    cout << g << "\n";
  }
}
