#include <bits/stdc++.h>
using namespace std;
int T, N, M, a, b;
long long vals[500000];
pair<vector<int>, int> ids[500000];
vector<long long> nums;
bool same(vector<int>& a, vector<int>& b) {
  if (a.size() != b.size()) return false;
  for (int i = (0); i < (a.size()); i++)
    if (a[i] != b[i]) return false;
  return true;
}
long long gcd(long long a, long long b) {
  while (a != 0 && b != 0) {
    if (a > b)
      a = a % b;
    else
      b = b % a;
  }
  if (a == 0)
    return b;
  else
    return a;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> T;
  while (T--) {
    cin >> N >> M;
    nums.clear();
    for (int i = (0); i < (N); i++) {
      cin >> vals[i];
      ids[i].second = i;
      ids[i].first.clear();
    }
    for (int i = (0); i < (M); i++) {
      cin >> a >> b;
      a--;
      b--;
      ids[b].first.emplace_back(a);
    }
    for (int i = (0); i < (N); i++)
      sort(ids[i].first.begin(), ids[i].first.end());
    sort(ids, ids + N);
    bool fd = 0;
    for (int i = (0); i < (N); i++) {
      if (ids[i].first.empty()) continue;
      if (!fd || !same(ids[i].first, ids[i - 1].first))
        nums.emplace_back(vals[ids[i].second]);
      else
        nums[nums.size() - 1] += vals[ids[i].second];
      fd = 1;
    }
    long long ans = 0;
    for (long long i : nums) ans = gcd(ans, i);
    cout << ans << '\n';
  }
}
