#include <bits/stdc++.h>
using namespace std;
struct P {
  int x, y;
  bool operator<(const P &a) const { return y < a.y; };
};
int a, b, c, d, i, k, n, m, e, dx[10] = {1, 0, -1, 0, 1, 1, -1, -1},
                               dy[10] = {0, 1, 0, -1, 1, -1, 1, -1};
int o[500101];
long long l[500101];
long long j[500101];
long long x, y, z, mod = 1000000009;
char r[1];
P u[1];
queue<int> q;
stack<int> s;
vector<int> v[500101];
bool as(P a, P b) { return a.x > b.x; }
long long gcd(long long x, long long y) {
  if (y == 0) return x;
  return gcd(y, x % y);
}
int main() {
  scanf("%d", &e);
  for (int tt = 1; tt <= e; tt++) {
    map<vector<int>, long long> p;
    scanf("%d %d", &a, &b);
    for (int t = 1; t <= a; t++) scanf("%lld", &l[t]);
    for (int t = 1; t <= b; t++) {
      scanf("%d %d", &n, &m);
      v[m].push_back(n);
    }
    for (int t = 1; t <= a; t++) {
      vector<int> v1;
      sort(v[t].begin(), v[t].end());
      for (int h = 0; h < v[t].size(); h++) v1.push_back(v[t][h]);
      p[v1] += l[t];
    }
    x = 0;
    for (int t = 1; t <= a; t++) {
      vector<int> v1;
      int w = 0;
      for (int h = 0; h < v[t].size(); h++) {
        v1.push_back(v[t][h]);
        w = 1;
      }
      if (w) x = gcd(x, p[v1]);
    }
    printf("%lld\n", x);
    for (int t = 1; t <= a; t++) v[t].clear();
  }
}
