#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c *r) -> decltype(cerr << *r);
template <class c>
char dud(...);
struct muu {
  template <class c>
  muu &operator<<(const c &) {
    return *this;
  }
};
using pii = pair<int, int>;
using ld = long double;
using ll = long long;
const int MAXN = 500007;
vector<int> v[MAXN];
int kolor[MAXN];
long long war[MAXN];
long long nwd(long long a, long long b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int N, M;
    scanf("%d%d", &N, &M);
    int licznik = 1;
    for (int i = 1; i <= N; i++) kolor[i] = 1;
    for (int i = 1; i <= N; i++) scanf("%lld", &war[i]);
    for (int i = 1; i <= M; i++) {
      int a, b;
      scanf("%d%d", &a, &b);
      v[a].push_back(b);
    }
    for (int i = 1; i <= N; i++) {
      map<int, vector<int>> grupki;
      for (auto it : v[i]) grupki[kolor[it]].push_back(it);
      for (auto &gr : grupki) {
        licznik++;
        for (auto it : gr.second) kolor[it] = licznik;
      }
    }
    map<int, long long> grupki;
    for (int i = 1; i <= N; i++)
      if (kolor[i] != 1) grupki[kolor[i]] += war[i];
    long long wyn = 0;
    for (auto it : grupki) wyn = nwd(wyn, it.second);
    printf("%lld\n", wyn);
    for (int i = 1; i <= N; i++) v[i].clear();
  }
  return 0;
}
