#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int MAXN = 500010;
LL c[MAXN];
vector<int> x[MAXN];
vector<int> z[MAXN];
LL Gcd(LL a, LL b) {
  while (b != 0) {
    LL t = a % b;
    a = b;
    b = t;
  }
  return a;
}
void Update(LL& ans, LL v) {
  if (ans == 0) {
    ans = v;
  } else {
    ans = Gcd(ans, v);
  }
}
int main() {
  int times;
  cin >> times;
  while (times--) {
    int n, m;
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; ++i) {
      scanf("%lld", &c[i]);
      x[i].clear();
      z[i].clear();
    }
    for (int i = 0; i < m; ++i) {
      int a, b;
      scanf("%d %d", &a, &b);
      x[b].push_back(a);
    }
    for (int i = 1; i <= n; ++i) {
      if (x[i].empty()) continue;
      sort(x[i].begin(), x[i].end());
      z[x[i].size()].push_back(i);
    }
    LL ans = 0;
    for (int i = 1; i <= n; ++i) {
      if (z[i].empty()) continue;
      if (z[i].size() == 1) {
        Update(ans, c[z[i][0]]);
        continue;
      }
      map<vector<int>, LL> h;
      for (int j : z[i]) h[x[j]] += c[j];
      for (auto& [key, value] : h) {
        Update(ans, value);
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
