#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
void solve() {}
signed main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n, e;
    cin >> n >> e;
    long long arr[n];
    for (long long i = 0; i < n; i++) cin >> arr[i];
    vector<vector<long long>> v(n);
    for (long long i = 0; i < e; i++) {
      long long a, b;
      cin >> a >> b;
      v[b - 1].push_back(a - 1);
    }
    map<vector<long long>, long long> m;
    for (long long i = 0; i < n; i++) {
      if (v[i].size()) {
        sort(v[i].begin(), v[i].end());
        m[v[i]] += arr[i];
      }
    }
    long long ans = 0;
    for (auto it : m) {
      ans = gcd(ans, it.second);
    }
    cout << ans << "\n";
  }
  return 0;
}
