#include <iostream>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 10;
int t, n, m, ans, h[maxn], a[maxn], v[maxn];
vector<int> e[maxn];
map<int, ll> mp;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int main() {
ios::sync_with_stdio(false);
cin >> t; srand(20101008);
while (t--) {
cin >> n >> m; ans = 0; mp.clear();
for (int i = 1; i <= n; i++) cin >> a[i], v[i] = rand(), h[i] = 0;
for (int x, y; m--; ) cin >> x >> y, h[y] ^= v[x];
for (int i = 1; i <= n; i++) if (h[i]) mp[h[i]] += a[i];
for (auto [p, x] : mp) ans = gcd(ans, x);
cout << ans << '\n';
}
return 0;
}