#include <bits/stdc++.h>
using namespace std;
int t, n, m;
long long c[500100];
vector<int> s[500100];
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
void solve() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%lld", c + i);
  for (int i = 1; i <= n; i++) s[i].clear();
  for (int i = 1, u, v; i <= m; i++) scanf("%d %d", &u, &v), s[v].push_back(u);
  map<long long, long long> mp;
  for (int i = 1; i <= n; i++) {
    if (!s[i].size()) continue;
    sort(s[i].begin(), s[i].end());
    long long h = 0;
    for (int j = 0; j < s[i].size(); j++)
      h = h * 972537172523723513ull + s[i][j];
    mp[h] += c[i];
  }
  long long g = 0;
  for (map<long long, long long>::iterator it = mp.begin(); it != mp.end();
       ++it)
    g = gcd(g, it->second);
  printf("%lld\n", g);
}
int main() {
  for (scanf("%d", &t); t; t--) solve();
}
