#include <bits/stdc++.h>
using namespace std;
int N, M;
const int Base = 666013;
const int MOD1 = 1000000007;
const int MOD2 = 1000000009;
long long Val[500005];
int Ord[500005], Grade[500005];
pair<int, int> E[500005];
pair<int, int> H[500005];
void Read() {
  scanf("%d%d", &N, &M);
  for (int i = 1; i <= N; i++) {
    scanf("%I64d", &Val[i]);
  }
  for (int i = 1; i <= M; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    Grade[y]++;
    E[i] = make_pair(x, y);
  }
  sort(E + 1, E + M + 1);
}
long long gcd(long long a, long long b) {
  if (a == 0 || b == 0) return max(a, b);
  long long r = a % b;
  while (r) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}
void Add(int& x, int y, int mod) {
  x += y;
  if (x >= mod) x -= mod;
}
bool cmp(int a, int b) { return H[a] < H[b]; }
void Solve() {
  for (int i = 1; i <= M; i++) {
    int x = E[i].first, y = E[i].second;
    H[y].first = (1LL * H[y].first * Base) % MOD1;
    Add(H[y].first, x, MOD1);
    H[y].second = (1LL * H[y].second * Base) % MOD2;
    Add(H[y].second, x, MOD2);
  }
  int cnt = 0;
  for (int i = 1; i <= N; i++) {
    if (Grade[i] > 0) Ord[++cnt] = i;
  }
  sort(Ord + 1, Ord + cnt + 1, cmp);
  long long res = 0, sum = 0;
  for (int i = 1; i <= cnt; i++) {
    if (i == 1 || H[Ord[i]] != H[Ord[i - 1]]) {
      res = gcd(res, sum);
      sum = Val[Ord[i]];
    } else
      sum += Val[Ord[i]];
  }
  res = gcd(res, sum);
  printf("%I64d\n", res);
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    Read();
    Solve();
    for (int i = 1; i <= N; i++) H[i] = make_pair(0, 0), Grade[i] = 0;
  }
  return 0;
}
