#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);
}
long long n, m, t;
long long a[500000];
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> t;
  for (long long ti = 0; ti < t; ti++) {
    map<vector<long long>, long long> tab;
    map<vector<long long>, long long>::iterator iter;
    cin >> n >> m;
    vector<long long> v[n + 1];
    for (long long i = 0; i < n; i++) cin >> a[i];
    for (long long i = 0; i < m; i++) {
      long long a, b;
      cin >> a >> b;
      v[b].push_back(a);
    }
    for (long long i = 0; i < n; i++) sort(v[i + 1].begin(), v[i + 1].end());
    for (long long i = 0; i < n; i++)
      if (!v[i + 1].empty()) tab[v[i + 1]] += a[i];
    long long g;
    iter = tab.begin();
    g = iter->second;
    iter++;
    for (; iter != tab.end(); iter++) g = gcd(g, iter->second);
    cout << g << '\n';
  }
  return 0;
}
