#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string my_to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string my_to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string my_to_string(tuple<A, B, C, D> p);
string my_to_string(const string& s) { return '"' + s + '"'; }
string my_to_string(const char* s) { return my_to_string((string)s); }
string my_to_string(bool b) { return (b ? "true" : "false"); }
string my_to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += my_to_string(v[i]);
  }
  res += "}";
  return res;
}
template <size_t N>
string my_to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}
string my_to_string(uint8_t a) {
  ostringstream ss;
  ss << a;
  return ss.str();
}
string my_to_string(uint16_t a) {
  ostringstream ss;
  ss << a;
  return ss.str();
}
string my_to_string(uint32_t a) {
  ostringstream ss;
  ss << a;
  return ss.str();
}
string my_to_string(uint64_t a) {
  ostringstream ss;
  ss << a;
  return ss.str();
}
string my_to_string(int8_t a) {
  ostringstream ss;
  ss << a;
  return ss.str();
}
string my_to_string(int16_t a) {
  ostringstream ss;
  ss << a;
  return ss.str();
}
string my_to_string(int32_t a) {
  ostringstream ss;
  ss << a;
  return ss.str();
}
string my_to_string(int64_t a) {
  ostringstream ss;
  ss << a;
  return ss.str();
}
string my_to_string(float a) {
  ostringstream ss;
  ss << a;
  return ss.str();
}
string my_to_string(double a) {
  ostringstream ss;
  ss << a;
  return ss.str();
}
string my_to_string(long double a) {
  ostringstream ss;
  ss << a;
  return ss.str();
}
string my_to_string(char a) {
  ostringstream ss;
  ss << '\'' << a << '\'';
  return ss.str();
}
template <typename A>
string my_to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto& x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += my_to_string(x);
  }
  res += "}";
  return res;
}
template <typename A, typename B>
string my_to_string(pair<A, B> p) {
  return "(" + my_to_string(p.first) + ", " + my_to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string my_to_string(tuple<A, B, C> p) {
  return "(" + my_to_string(get<0>(p)) + ", " + my_to_string(get<1>(p)) + ", " +
         my_to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string my_to_string(tuple<A, B, C, D> p) {
  return "(" + my_to_string(get<0>(p)) + ", " + my_to_string(get<1>(p)) + ", " +
         my_to_string(get<2>(p)) + ", " + my_to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << my_to_string(H);
  debug_out(T...);
}
long long gcd(long long a, long long b) {
  while (b != 0) {
    a %= b;
    swap(a, b);
  }
  return a;
}
const int MX = 5e5 + 99;
int n, m;
long long c[MX];
vector<int> eL[MX], eR[MX];
unordered_set<int> S[4 * MX];
unordered_set<unordered_set<int>*> have_this[MX];
int cnt_S;
void do_intersection(unordered_set<int>* A, unordered_set<int>* B,
                     unordered_set<int>* C) {
  if (A->size() > B->size()) swap(A, B);
  for (int a : *A) {
    if (B->count(a)) C->insert(a);
  }
  for (int cc : *C) {
    A->erase(cc);
    B->erase(cc);
    have_this[cc].erase(A);
    have_this[cc].erase(B);
    have_this[cc].insert(C);
  }
}
long long solve() {
  for (int i = 0; i < n; i++) {
    for (int j : eL[i]) {
      S[i].insert(j);
      have_this[j].insert(&S[i]);
    }
    42;
  }
  int curr_C_idx = n;
  for (int j = 0; j < n;) {
    if (have_this[j].size() < 2) {
      j++;
      continue;
    }
    auto it = have_this[j].begin();
    unordered_set<int>* A = *it;
    ++it;
    unordered_set<int>* B = *it;
    unordered_set<int>* C = &S[curr_C_idx];
    curr_C_idx++;
    do_intersection(A, B, C);
  }
  long long result = 0LL;
  42;
  for (int i = 0; i < curr_C_idx; i++) {
    long long sum = 0;
    for (int j : S[i]) {
      sum += c[j];
    }
    42;
    42;
    S[i].clear();
    result = gcd(result, sum);
  }
  42;
  for (int j = 0; j < n; j++) have_this[j].clear();
  return result;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int t;
  cin >> t;
  for (int qq = 0; qq < t; qq++) {
    cin >> n >> m;
    for (int i = 0; i < n; i++) cin >> c[i];
    for (int i = 0; i < n; i++) {
      eL[i].clear();
      eR[i].clear();
    }
    for (int i = 0; i < m; i++) {
      int u, v;
      cin >> u >> v;
      u--;
      v--;
      eL[u].push_back(v);
      eR[v].push_back(u);
    }
    long long res = solve();
    cout << res << '\n';
  }
  return 0;
}
