#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000000;
const long long INF = 1000000000ll;
const long long LLINF = 1000000000000000000ll;
const int MOD = 998244353;
long long c[MAXN];
long long col[MAXN];
vector<int> a[MAXN];
long long gcd(long long x, long long y) {
  if (y == 0) {
    return x;
  } else {
    return gcd(y, x % y);
  }
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
  int t;
  cin >> t;
  mt19937 mt_rand(0);
  for (int tt = 0; tt < (int)(t); tt++) {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < (int)(n); i++) {
      cin >> c[i];
      a[i].clear();
      col[i] = 0;
    }
    for (int i = 0; i < (int)(m); i++) {
      int x, y;
      cin >> x >> y;
      x--;
      y--;
      a[x].push_back(y);
    }
    for (int i = 0; i < (int)(n); i++) {
      int x = mt_rand();
      for (int u : a[i]) {
        col[u] += x;
      }
    }
    unordered_map<long long, long long> s;
    for (int i = 0; i < (int)(n); i++) {
      if (col[i] != 0) {
        s[col[i]] += c[i];
      }
    }
    long long res = 0;
    for (auto p : s) {
      res = gcd(res, p.second);
    }
    cout << res << "\n";
  }
  return 0;
}
