#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e9;

long long c[1000001];

vector <int> g[1000001];

long long gcd(long long a, long long b)
{
    return b ? gcd(b, a % b) : a;
}

long long solve()
{
    map <vector <int>, long long> mp;
    int n, m, sum = 0;
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
    {
        cin >> c[i];
    }
    for(int i = 0; i < m; i++)
    {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y + n);
        g[y + n].push_back(x);
    }
    for(int i = n + 1; i <= 2 * n; i++)
    {
        sort(g[i].begin(), g[i].end());
    }
    long long ans = 0;
    for(int i = 1; i <= n; i++)
    {
        long long curr_sum = 0;
        for(auto u : g[i])
        {
            curr_sum += c[u - n];
        }
        //cout << curr_sum << endl;
        //ans = gcd(ans, curr_sum);
    }
    for(int i = n + 1; i <= 2 * n; i++)
    {
        if(g[i].size() == 0)
            continue;
        mp[g[i]] += c[i - n];
    }
    for(auto i : mp)
    {
//        for(int j = 0; j < i.first.size(); j++)
//        {
//            cout << i.first[j] << " ";
//        }
//        cout<< " -> " << i.second << endl;
        ans = gcd(ans, i.second);
    }
    for(int i = 1; i <= 2 * n; i++)
    {
        g[i].clear();
    }
    return ans;
}

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        cout << solve() << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e9;

long long c[500001];

vector <int> g[1001];

long long gcd(long long a, long long b)
{
    return b ? gcd(b, a % b) : a;
}

long long solve()
{
    map <vector <int>, long long> mp;
    int n, m, sum = 0;
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
    {
        cin >> c[i];
    }
    for(int i = 0; i < m; i++)
    {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y + n);
        g[y + n].push_back(x);
    }
    for(int i = n + 1; i <= 2 * n; i++)
    {
        sort(g[i].begin(), g[i].end());
    }
    long long ans = 0;
    for(int i = n + 1; i <= 2 * n; i++)
    {
        if(g[i].size() == 0)
            continue;
        long long curr_sum = 0;
        for(auto u : g[i])
        {
            curr_sum += c[u - n];
        }
        //cout << curr_sum << endl;
        ans = gcd(ans, curr_sum);
    }
    for(int i = n + 1; i <= 2 * n; i++)
    {
        mp[g[i]] += c[i - n];
    }
    for(auto i : mp)
    {
//        for(int j = 0; j < i.first.size(); j++)
//        {
//            cout << i.first[j] << " ";
//        }
//        cout<< " -> " << i.second << endl;
        ans = gcd(ans, i.second);
    }
    for(int i = 1; i <= 2 * n; i++)
    {
        g[i].clear();
    }
    return ans;
}

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        cout << solve() << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e9;

long long c[500001];

vector <int> g[1001];

long long gcd(long long a, long long b)
{
    return b ? gcd(b, a % b) : a;
}

long long solve()
{
    map <vector <int>, long long> mp;
    int n, m, sum = 0;
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
    {
        cin >> c[i];
    }
    for(int i = 0; i < m; i++)
    {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y + n);
        g[y + n].push_back(x);
    }
    for(int i = n + 1; i <= 2 * n; i++)
    {
        sort(g[i].begin(), g[i].end());
    }
    long long ans = 0;
    for(int i = 1; i <= n; i++)
    {
        long long curr_sum = 0;
        for(auto u : g[i])
        {
            curr_sum += c[u - n];
        }
        //cout << curr_sum << endl;
        //ans = gcd(ans, curr_sum);
    }
    for(int i = n + 1; i <= 2 * n; i++)
    {
        if(g[i].size() == 0)
            continue;
        mp[g[i]] += c[i - n];
    }
    for(auto i : mp)
    {
//        for(int j = 0; j < i.first.size(); j++)
//        {
//            cout << i.first[j] << " ";
//        }
//        cout<< " -> " << i.second << endl;
        ans = gcd(ans, i.second);
    }
    for(int i = 1; i <= 2 * n; i++)
    {
        g[i].clear();
    }
    return ans;
}

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        cout << solve() << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#pragma GCC optimize("Ofast")

const int MAX = 1e9;

long long gcd(long long a, long long b)
{
    return b ? gcd(b, a % b) : a;
}

long long solve()
{
    map <vector <int>, long long> mp;
    vector <vector <int> > g;
    int n, m, sum = 0;
    cin >> n >> m;
    long long c[n + 1];
    g.resize(n + 1);
    for(int i = 1; i <= n; i++)
    {
        cin >> c[i];
    }
    for(int i = 0; i < m; i++)
    {
        int x, y;
        cin >> x >> y;
        g[y].push_back(x);
    }
    long long ans = 0;
    for(int i = 1; i <= n; i++)
    {
        if(g[i].size() == 0)
            continue;
        sort(g[i].begin(), g[i].end());
        mp[g[i]] += c[i];
    }
    for(const auto& i : mp)
    {
//        for(int j = 0; j < i.first.size(); j++)
//        {
//            cout << i.first[j] << " ";
//        }
//        cout<< " -> " << i.second << endl;
        ans = gcd(ans, i.second);
    }
    printf("%lld\n", ans);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e9;

long long c[1000001];

vector <int> g[1001];

long long gcd(long long a, long long b)
{
    return b ? gcd(b, a % b) : a;
}

long long solve()
{
    map <vector <int>, long long> mp;
    int n, m, sum = 0;
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
    {
        cin >> c[i];
    }
    for(int i = 0; i < m; i++)
    {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y + n);
        g[y + n].push_back(x);
    }
    for(int i = n + 1; i <= 2 * n; i++)
    {
        sort(g[i].begin(), g[i].end());
    }
    long long ans = 0;
    for(int i = 1; i <= n; i++)
    {
        long long curr_sum = 0;
        for(auto u : g[i])
        {
            curr_sum += c[u - n];
        }
        //cout << curr_sum << endl;
        //ans = gcd(ans, curr_sum);
    }
    for(int i = n + 1; i <= 2 * n; i++)
    {
        if(g[i].size() == 0)
            continue;
        mp[g[i]] += c[i - n];
    }
    for(auto i : mp)
    {
//        for(int j = 0; j < i.first.size(); j++)
//        {
//            cout << i.first[j] << " ";
//        }
//        cout<< " -> " << i.second << endl;
        ans = gcd(ans, i.second);
    }
    for(int i = 1; i <= 2 * n; i++)
    {
        g[i].clear();
    }
    return ans;
}

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        cout << solve() << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e9;

long long c[1000001];

vector <int> g[1000001];

long long gcd(long long a, long long b)
{
    return b ? gcd(b, a % b) : a;
}

long long solve()
{
    map <vector <int>, long long> mp;
    int n, m, sum = 0;
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
    {
        cin >> c[i];
    }
    for(int i = 0; i < m; i++)
    {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y + n);
        g[y + n].push_back(x);
    }
    long long ans = 0;
    for(int i = n + 1; i <= 2 * n; i++)
    {
        if(g[i].size() == 0)
            continue;
        for(int i = n + 1; i <= 2 * n; i++)
        {
            sort(g[i].begin(), g[i].end());
        }
        mp[g[i]] += c[i - n];
    }
    for(auto i : mp)
    {
//        for(int j = 0; j < i.first.size(); j++)
//        {
//            cout << i.first[j] << " ";
//        }
//        cout<< " -> " << i.second << endl;
        ans = gcd(ans, i.second);
    }
    for(int i = 1; i <= 2 * n; i++)
    {
        g[i].clear();
    }
    return ans;
}

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        cout << solve() << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e9;

long long c[1000001];

vector <int> g[500001];

long long gcd(long long a, long long b)
{
    return b ? gcd(b, a % b) : a;
}

long long solve()
{
    map <vector <int>, long long> mp;
    int n, m, sum = 0;
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
    {
        cin >> c[i];
    }
    for(int i = 0; i < m; i++)
    {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y + n);
        g[y + n].push_back(x);
    }
    for(int i = n + 1; i <= 2 * n; i++)
    {
        sort(g[i].begin(), g[i].end());
    }
    long long ans = 0;
    for(int i = 1; i <= n; i++)
    {
        long long curr_sum = 0;
        for(auto u : g[i])
        {
            curr_sum += c[u - n];
        }
        //cout << curr_sum << endl;
        //ans = gcd(ans, curr_sum);
    }
    for(int i = n + 1; i <= 2 * n; i++)
    {
        if(g[i].size() == 0)
            continue;
        mp[g[i]] += c[i - n];
    }
    for(auto i : mp)
    {
//        for(int j = 0; j < i.first.size(); j++)
//        {
//            cout << i.first[j] << " ";
//        }
//        cout<< " -> " << i.second << endl;
        ans = gcd(ans, i.second);
    }
    for(int i = 1; i <= 2 * n; i++)
    {
        g[i].clear();
    }
    return ans;
}

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        cout << solve() << endl;
    }
    return 0;
}


 #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep2(i,a,b) for(ll i=a;i<=b;++i)
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep3(i,a,b) for(ll i=a;i>=b;i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define vec vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define vec2(a,b) vector<vec>(a,vec(b))
#define vec2ll(a,b) vector<vecll>(a,vecll(b))
#define vec3(a,b,c) vector<vector<vec>>(a,vec2(b,c))
#define vec3ll(a,b,c) vector<vector<vecll>>(a,vec2ll(b,c))
#define fi first
#define se second
#define endl "\n"
#define all(c) begin(c),end(c)
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))
#define MM " "
using namespace std;
int in() {int x;cin>>x;return x;}
ll lin() {ll x;cin>>x;return x;}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T> inline void print(pair<T,T> p){cout<<"("<<p.first<<","<<p.second<<") ";}
template<class T> inline void print(vector<pair<T,T>> v){for(auto e:v)print(e); cout<<endl;}
template<class T> inline void print(T v){for(auto e:v)cout<<e<<" ";cout<<endl;}

unsigned xorshift()
{
    static unsigned x=123456789, y=362436069, z=521288629, w=8867512;
    unsigned t=(x^(x<<11));
    x=y; y=z; z=w;
    return w=(w^(w>>19))^(t^(t>>8));
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int n){
    return uniform_int_distribution<int>(0, INT_MAX)(rng)%n;
}
double drnd(){
    return (double)(rnd(INT_MAX))/INT_MAX;
}
void shuffle(vec &x){
    int n=x.size();
    rep3(i,n-1,1){
        swap(x[i],x[rnd(i-1)]);
    }
}
clock_t start;
double time(){
    return (double)(clock()-start)/CLOCKS_PER_SEC;
}
ll M[4]={1000000000000037,9007199254740997,92709568269121 ,9999999999999937};

ll rh[4][510000];

ll gcd(ll x,ll y){
    if(y==0)return x;
    return gcd(y,x%y);
}
main(){
    ios
    int T=in();
    while(T--){
        int n=in(),m=in();
        vecll a;
        rep(i,n)a.pb(lin());
        vecll p[4];
        ll k[4];
        rep(i,4)k[i]=rnd(100)+2;
        ll t[4]={1,1,1,1};
        rep(i,n+1){
            rep(j,4)p[j].pb(t[j]);
            rep(j,4)t[j]=t[j]*k[j]%M[j];
        }
        rep(i,4)rep(j,n+1)rh[i][j]=0;
        rep(i,m){
            int x=in(),y=in();
            rep(j,4){
                rh[j][y]=(rh[j][y]+p[j][x])%M[j];
            }
        }
        map<vecll,ll> mp;
        rep2(i,1,n){
            vecll v;
            rep(j,4) v.eb(rh[j][i]);
            bool flag=0;
            for(auto e:v)if(e)flag=1;
            if(!flag)continue;
            mp[v]+=a[i-1];
        }
        ll g=0;
        for(auto e:mp){
            g=gcd(g,e.second);
        }
        cout<<g<<endl;
    }

}


 #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep2(i,a,b) for(ll i=a;i<=b;++i)
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep3(i,a,b) for(ll i=a;i>=b;i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define vec vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define vec2(a,b) vector<vec>(a,vec(b))
#define vec2ll(a,b) vector<vecll>(a,vecll(b))
#define vec3(a,b,c) vector<vector<vec>>(a,vec2(b,c))
#define vec3ll(a,b,c) vector<vector<vecll>>(a,vec2ll(b,c))
#define fi first
#define se second
#define endl "\n"
#define all(c) begin(c),end(c)
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))
#define MM " "
using namespace std;
int in() {int x;cin>>x;return x;}
ll lin() {ll x;cin>>x;return x;}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T> inline void print(pair<T,T> p){cout<<"("<<p.first<<","<<p.second<<") ";}
template<class T> inline void print(vector<pair<T,T>> v){for(auto e:v)print(e); cout<<endl;}
template<class T> inline void print(T v){for(auto e:v)cout<<e<<" ";cout<<endl;}

unsigned xorshift()
{
    static unsigned x=123456789, y=362436069, z=521288629, w=8867512;
    unsigned t=(x^(x<<11));
    x=y; y=z; z=w;
    return w=(w^(w>>19))^(t^(t>>8));
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int n){
    return uniform_int_distribution<int>(0, INT_MAX)(rng)%n;
}
double drnd(){
    return (double)(rnd(INT_MAX))/INT_MAX;
}
void shuffle(vec &x){
    int n=x.size();
    rep3(i,n-1,1){
        swap(x[i],x[rnd(i-1)]);
    }
}
clock_t start;
double time(){
    return (double)(clock()-start)/CLOCKS_PER_SEC;
}
ll M[4]={1000000000000037,9007199254740997,92709568269121 ,999999999999937};

ll rh[4][510000];

ll gcd(ll x,ll y){
    if(y==0)return x;
    return gcd(y,x%y);
}
main(){
    ios
    int T=in();
    while(T--){
        int n=in(),m=in();
        vecll a;
        rep(i,n)a.pb(lin());
        vecll p[4];
        ll k[4];
        rep(i,4)k[i]=rnd(1000)+2;
        ll t[4]={1,1,1,1};
        rep(i,n+1){
            rep(j,4)p[j].pb(t[j]);
            rep(j,4)t[j]=t[j]*k[j]%M[j];
        }
        rep(i,4)rep(j,n+1)rh[i][j]=0;
        rep(i,m){
            int x=in(),y=in();
            rep(j,4){
                rh[j][y]=(rh[j][y]+p[j][x])%M[j];
            }
        }
        map<vecll,ll> mp;
        rep2(i,1,n){
            vecll v;
            rep(j,4) v.eb(rh[j][i]);
            bool flag=0;
            for(auto e:v)if(e)flag=1;
            if(!flag)continue;
            mp[v]+=a[i-1];
        }
        ll g=0;
        for(auto e:mp){
            g=gcd(g,e.second);
        }
        cout<<g<<endl;
    }

}


// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep2(i,a,b) for(ll i=a;i<=b;++i)
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep3(i,a,b) for(ll i=a;i>=b;i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define vec vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define vec2(a,b) vector<vec>(a,vec(b))
#define vec2ll(a,b) vector<vecll>(a,vecll(b))
#define vec3(a,b,c) vector<vector<vec>>(a,vec2(b,c))
#define vec3ll(a,b,c) vector<vector<vecll>>(a,vec2ll(b,c))
#define fi first
#define se second
#define endl "\n"
#define all(c) begin(c),end(c)
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))
#define MM " "
using namespace std;
int in() {int x;cin>>x;return x;}
ll lin() {ll x;cin>>x;return x;}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T> inline void print(pair<T,T> p){cout<<"("<<p.first<<","<<p.second<<") ";}
template<class T> inline void print(vector<pair<T,T>> v){for(auto e:v)print(e); cout<<endl;}
template<class T> inline void print(T v){for(auto e:v)cout<<e<<" ";cout<<endl;}

unsigned xorshift()
{
    static unsigned x=123456789, y=362436069, z=521288629, w=8867512;
    unsigned t=(x^(x<<11));
    x=y; y=z; z=w;
    return w=(w^(w>>19))^(t^(t>>8));
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int n){
    return xorshift()%(n);
}
double drnd(){
    return (double)(rnd(INT_MAX))/INT_MAX;
}
void shuffle(vec &x){
    int n=x.size();
    rep3(i,n-1,1){
        swap(x[i],x[rnd(i-1)]);
    }
}
clock_t start;
double time(){
    return (double)(clock()-start)/CLOCKS_PER_SEC;
}
ll M[4]={1000000000000037,9007199254740997 , 92709568269121 ,9999999999999937};

ll rh[4][510000];

ll gcd(ll x,ll y){
    if(y==0)return x;
    return gcd(y,x%y);
}
main(){
    int t=in();
    while(t--){
        int n=in(),m=in();
        vecll a;
        rep(i,n)a.pb(lin());
        vecll p[4];
        ll k[4];
        rep(i,4)k[i]=rnd(M[i]-3);
        ll t[4]={1,1,1,1};
        rep(i,n+1){
            rep(j,4)p[j].pb(t[j]);
            rep(j,4)t[j]=t[j]*k[i]%M[i];
        }
        rep(i,4)rep(j,n+1)rh[i][j]=0;
        rep(i,m){
            int x=in(),y=in();
            rep(j,4){
                rh[j][y]=(rh[j][y]+p[j][x])%M[j];
            }
        }
        map<vecll,ll> mp;
        rep2(i,1,n){
            vecll v;
            rep(j,4) v.eb(rh[j][i]);
            bool flag=0;
            for(auto e:v)if(e)flag=1;
            if(!flag)continue;
            mp[v]+=a[i-1];
        }
        ll g=0;
        for(auto e:mp){
            g=gcd(g,e.second);
        }
        cout<<g<<endl;
    }

}


// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep2(i,a,b) for(ll i=a;i<=b;++i)
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep3(i,a,b) for(ll i=a;i>=b;i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define vec vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define vec2(a,b) vector<vec>(a,vec(b))
#define vec2ll(a,b) vector<vecll>(a,vecll(b))
#define vec3(a,b,c) vector<vector<vec>>(a,vec2(b,c))
#define vec3ll(a,b,c) vector<vector<vecll>>(a,vec2ll(b,c))
#define fi first
#define se second
#define endl "\n"
#define all(c) begin(c),end(c)
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))
#define MM " "
using namespace std;
int in() {int x;cin>>x;return x;}
ll lin() {ll x;cin>>x;return x;}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T> inline void print(pair<T,T> p){cout<<"("<<p.first<<","<<p.second<<") ";}
template<class T> inline void print(vector<pair<T,T>> v){for(auto e:v)print(e); cout<<endl;}
template<class T> inline void print(T v){for(auto e:v)cout<<e<<" ";cout<<endl;}

unsigned xorshift()
{
    static unsigned x=123456789, y=362436069, z=521288629, w=8867512;
    unsigned t=(x^(x<<11));
    x=y; y=z; z=w;
    return w=(w^(w>>19))^(t^(t>>8));
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int n){
    return uniform_int_distribution<int>(0, INT_MAX)(rng)%n;
}
double drnd(){
    return (double)(rnd(INT_MAX))/INT_MAX;
}
void shuffle(vec &x){
    int n=x.size();
    rep3(i,n-1,1){
        swap(x[i],x[rnd(i-1)]);
    }
}
clock_t start;
double time(){
    return (double)(clock()-start)/CLOCKS_PER_SEC;
}
ll M[4]={1000000000000037,9007199254740997,92709568269121 ,9999999999999937};

ll rh[4][510000];

ll gcd(ll x,ll y){
    if(y==0)return x;
    return gcd(y,x%y);
}
main(){
    int t=in();
    while(t--){
        int n=in(),m=in();
        vecll a;
        rep(i,n)a.pb(lin());
        vecll p[4];
        ll k[4];
        rep(i,4)k[i]=rnd(INT_MAX);
        ll t[4]={1,1,1,1};
        rep(i,n+1){
            rep(j,4)p[j].pb(t[j]);
            rep(j,4)t[j]=t[j]*k[j]%M[j];
        }
        rep(i,4)rep(j,n+1)rh[i][j]=0;
        rep(i,m){
            int x=in(),y=in();
            rep(j,4){
                rh[j][y]=(rh[j][y]+p[j][x])%M[j];
            }
        }
        map<vecll,ll> mp;
        rep2(i,1,n){
            vecll v;
            rep(j,4) v.eb(rh[j][i]);
            bool flag=0;
            for(auto e:v)if(e)flag=1;
            if(!flag)continue;
            mp[v]+=a[i-1];
        }
        ll g=0;
        for(auto e:mp){
            g=gcd(g,e.second);
        }
        cout<<g<<endl;
    }

}


// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep2(i,a,b) for(ll i=a;i<=b;++i)
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep3(i,a,b) for(ll i=a;i>=b;i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define vec vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define vec2(a,b) vector<vec>(a,vec(b))
#define vec2ll(a,b) vector<vecll>(a,vecll(b))
#define vec3(a,b,c) vector<vector<vec>>(a,vec2(b,c))
#define vec3ll(a,b,c) vector<vector<vecll>>(a,vec2ll(b,c))
#define fi first
#define se second
#define endl "\n"
#define all(c) begin(c),end(c)
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))
#define MM " "
using namespace std;
int in() {int x;cin>>x;return x;}
ll lin() {ll x;cin>>x;return x;}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T> inline void print(pair<T,T> p){cout<<"("<<p.first<<","<<p.second<<") ";}
template<class T> inline void print(vector<pair<T,T>> v){for(auto e:v)print(e); cout<<endl;}
template<class T> inline void print(T v){for(auto e:v)cout<<e<<" ";cout<<endl;}

unsigned xorshift()
{
    static unsigned x=123456789, y=362436069, z=521288629, w=8867512;
    unsigned t=(x^(x<<11));
    x=y; y=z; z=w;
    return w=(w^(w>>19))^(t^(t>>8));
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int n){
    return uniform_int_distribution<int>(0, INT_MAX)(rng)%n;
}
double drnd(){
    return (double)(rnd(INT_MAX))/INT_MAX;
}
void shuffle(vec &x){
    int n=x.size();
    rep3(i,n-1,1){
        swap(x[i],x[rnd(i-1)]);
    }
}
clock_t start;
double time(){
    return (double)(clock()-start)/CLOCKS_PER_SEC;
}
ll M[4]={1000000000000037,9007199254740997,92709568269121 ,9999999999999937};

ll rh[4][510000];

ll gcd(ll x,ll y){
    if(y==0)return x;
    return gcd(y,x%y);
}
main(){
    int t=in();
    while(t--){
        int n=in(),m=in();
        vecll a;
        rep(i,n)a.pb(lin());
        vecll p[4];
        ll k[4];
        rep(i,4)k[i]=rnd(INT_MAX);
        ll t[4]={1,1,1,1};
        rep(i,n+1){
            rep(j,2)p[j].pb(t[j]);
            rep(j,2)t[j]=t[j]*k[j]%M[j];
        }
        rep(i,2)rep(j,n+1)rh[i][j]=0;
        rep(i,m){
            int x=in(),y=in();
            rep(j,2){
                rh[j][y]=(rh[j][y]+p[j][x])%M[j];
            }
        }
        map<pll,ll> mp;
        rep2(i,1,n){
            pll p={rh[0][i],rh[1][i]};
            bool flag=0;
            if(p!=pll{0ll,0ll})flag=1;
            if(!flag)continue;
            mp[p]+=a[i-1];
        }
        ll g=0;
        for(auto e:mp){
            g=gcd(g,e.second);
        }
        cout<<g<<endl;
    }

}


// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep2(i,a,b) for(ll i=a;i<=b;++i)
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep3(i,a,b) for(ll i=a;i>=b;i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define vec vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define vec2(a,b) vector<vec>(a,vec(b))
#define vec2ll(a,b) vector<vecll>(a,vecll(b))
#define vec3(a,b,c) vector<vector<vec>>(a,vec2(b,c))
#define vec3ll(a,b,c) vector<vector<vecll>>(a,vec2ll(b,c))
#define fi first
#define se second
#define endl "\n"
#define all(c) begin(c),end(c)
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))
#define MM " "
using namespace std;
int in() {int x;cin>>x;return x;}
ll lin() {ll x;cin>>x;return x;}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T> inline void print(pair<T,T> p){cout<<"("<<p.first<<","<<p.second<<") ";}
template<class T> inline void print(vector<pair<T,T>> v){for(auto e:v)print(e); cout<<endl;}
template<class T> inline void print(T v){for(auto e:v)cout<<e<<" ";cout<<endl;}

unsigned xorshift()
{
    static unsigned x=123456789, y=362436069, z=521288629, w=8867512;
    unsigned t=(x^(x<<11));
    x=y; y=z; z=w;
    return w=(w^(w>>19))^(t^(t>>8));
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int n){
    return uniform_int_distribution<int>(0, INT_MAX)(rng)%n;
}
double drnd(){
    return (double)(rnd(INT_MAX))/INT_MAX;
}
void shuffle(vec &x){
    int n=x.size();
    rep3(i,n-1,1){
        swap(x[i],x[rnd(i-1)]);
    }
}
clock_t start;
double time(){
    return (double)(clock()-start)/CLOCKS_PER_SEC;
}
ll M[4]={1000000000000037,9007199254740997,92709568269121 ,9999999999999937};

ll rh[4][510000];

ll gcd(ll x,ll y){
    if(y==0)return x;
    return gcd(y,x%y);
}
main(){
    int t=in();
    while(t--){
        int n=in(),m=in();
        vecll a;
        rep(i,n)a.pb(lin());
        vecll p[4];
        ll k[4];
        rep(i,4)k[i]=rnd(INT_MAX);
        ll t[4]={1,1,1,1};
        rep(i,n+1){
            rep(j,2)p[j].pb(t[j]);
            rep(j,2)t[j]=t[j]*k[j]%M[j];
        }
        rep(i,2)rep(j,n+1)rh[i][j]=0;
        rep(i,m){
            int x=in(),y=in();
            rep(j,2){
                rh[j][y]=(rh[j][y]+p[j][x])%M[j];
            }
        }
        map<pll,ll> mp;
        rep2(i,1,n){
            pll p={rh[0][i],rh[1][i]};
            rep(j,4) v.eb(rh[j][i]);
            bool flag=0;
            if(p!={0ll,0ll})flag=1;
            if(!flag)continue;
            mp[p]+=a[i-1];
        }
        ll g=0;
        for(auto e:mp){
            g=gcd(g,e.second);
        }
        cout<<g<<endl;
    }

}


 #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep2(i,a,b) for(ll i=a;i<=b;++i)
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep3(i,a,b) for(ll i=a;i>=b;i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define vec vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define vec2(a,b) vector<vec>(a,vec(b))
#define vec2ll(a,b) vector<vecll>(a,vecll(b))
#define vec3(a,b,c) vector<vector<vec>>(a,vec2(b,c))
#define vec3ll(a,b,c) vector<vector<vecll>>(a,vec2ll(b,c))
#define fi first
#define se second
#define endl "\n"
#define all(c) begin(c),end(c)
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))
#define MM " "
using namespace std;
int in() {int x;cin>>x;return x;}
ll lin() {ll x;cin>>x;return x;}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T> inline void print(pair<T,T> p){cout<<"("<<p.first<<","<<p.second<<") ";}
template<class T> inline void print(vector<pair<T,T>> v){for(auto e:v)print(e); cout<<endl;}
template<class T> inline void print(T v){for(auto e:v)cout<<e<<" ";cout<<endl;}

unsigned xorshift()
{
    static unsigned x=123456789, y=362436069, z=521288629, w=8867512;
    unsigned t=(x^(x<<11));
    x=y; y=z; z=w;
    return w=(w^(w>>19))^(t^(t>>8));
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int n){
    return uniform_int_distribution<int>(0, INT_MAX)(rng)%n;
}
double drnd(){
    return (double)(rnd(INT_MAX))/INT_MAX;
}
void shuffle(vec &x){
    int n=x.size();
    rep3(i,n-1,1){
        swap(x[i],x[rnd(i-1)]);
    }
}
clock_t start;
double time(){
    return (double)(clock()-start)/CLOCKS_PER_SEC;
}
ll M[4]={1000000000000037,9007199254740997,92709568269121 ,9999999999999937};

ll rh[4][510000];

ll gcd(ll x,ll y){
    if(y==0)return x;
    return gcd(y,x%y);
}
main(){
    ios
    int T=in();
    while(T--){
        int n=in(),m=in();
        vecll a;
        rep(i,n)a.pb(lin());
        vecll p[4];
        ll k[4];
        rep(i,4)k[i]=rnd(1000)+2;
        ll t[4]={1,1,1,1};
        rep(i,n+1){
            rep(j,4)p[j].pb(t[j]);
            rep(j,4)t[j]=t[j]*k[j]%M[j];
        }
        rep(i,4)rep(j,n+1)rh[i][j]=0;
        rep(i,m){
            int x=in(),y=in();
            rep(j,4){
                rh[j][y]=(rh[j][y]+p[j][x])%M[j];
            }
        }
        map<vecll,ll> mp;
        rep2(i,1,n){
            vecll v;
            rep(j,4) v.eb(rh[j][i]);
            bool flag=0;
            for(auto e:v)if(e)flag=1;
            if(!flag)continue;
            mp[v]+=a[i-1];
        }
        ll g=0;
        for(auto e:mp){
            g=gcd(g,e.second);
        }
        cout<<g<<endl;
    }

}


// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep2(i,a,b) for(ll i=a;i<=b;++i)
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep3(i,a,b) for(ll i=a;i>=b;i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define vec vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define vec2(a,b) vector<vec>(a,vec(b))
#define vec2ll(a,b) vector<vecll>(a,vecll(b))
#define vec3(a,b,c) vector<vector<vec>>(a,vec2(b,c))
#define vec3ll(a,b,c) vector<vector<vecll>>(a,vec2ll(b,c))
#define fi first
#define se second
#define endl "\n"
#define all(c) begin(c),end(c)
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))
#define MM " "
using namespace std;
int in() {int x;cin>>x;return x;}
ll lin() {ll x;cin>>x;return x;}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T> inline void print(pair<T,T> p){cout<<"("<<p.first<<","<<p.second<<") ";}
template<class T> inline void print(vector<pair<T,T>> v){for(auto e:v)print(e); cout<<endl;}
template<class T> inline void print(T v){for(auto e:v)cout<<e<<" ";cout<<endl;}

unsigned xorshift()
{
    static unsigned x=123456789, y=362436069, z=521288629, w=8867512;
    unsigned t=(x^(x<<11));
    x=y; y=z; z=w;
    return w=(w^(w>>19))^(t^(t>>8));
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int n){
    return xorshift()%(n);
}
double drnd(){
    return (double)(rnd(INT_MAX))/INT_MAX;
}
void shuffle(vec &x){
    int n=x.size();
    rep3(i,n-1,1){
        swap(x[i],x[rnd(i-1)]);
    }
}
clock_t start;
double time(){
    return (double)(clock()-start)/CLOCKS_PER_SEC;
}
ll M[4]={1000000000000037,9007199254740997 , 92709568269121 ,9999999999999937};

ll rh[4][510000];

ll gcd(ll x,ll y){
    if(y==0)return x;
    return gcd(y,x%y);
}
main(){
    int t=in();
    while(t--){
        int n=in(),m=in();
        vecll a;
        rep(i,n)a.pb(lin());
        vecll p[4];
        ll k[4];
        rep(i,4)k[i]=rnd(M[i]-3);
        ll t[4]={1,1,1,1};
        rep(i,n+1){
            rep(j,4)p[j].pb(t[j]);
            rep(j,4)t[j]=t[j]*k[i]%M[i];
        }
        rep(i,4)rep(j,n+1)rh[i][j]=0;
        rep(i,m){
            int x=in(),y=in();
            rep(j,4){
                rh[j][y]=(rh[j][y]+p[j][x])%M[j];
            }
        }
        map<vecll,ll> mp;
        rep2(i,1,n){
            vecll v;
            rep(j,4) v.eb(rh[j][i]);
            mp[v]+=a[i-1];
        }
        ll g=0;
        for(auto e:mp){
            g=gcd(g,e.second);
        }
        cout<<g<<endl;
    }

}


 #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep2(i,a,b) for(ll i=a;i<=b;++i)
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep3(i,a,b) for(ll i=a;i>=b;i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define vec vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define vec2(a,b) vector<vec>(a,vec(b))
#define vec2ll(a,b) vector<vecll>(a,vecll(b))
#define vec3(a,b,c) vector<vector<vec>>(a,vec2(b,c))
#define vec3ll(a,b,c) vector<vector<vecll>>(a,vec2ll(b,c))
#define fi first
#define se second
#define endl "\n"
#define all(c) begin(c),end(c)
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))
#define MM " "
using namespace std;
int in() {int x;cin>>x;return x;}
ll lin() {ll x;cin>>x;return x;}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T> inline void print(pair<T,T> p){cout<<"("<<p.first<<","<<p.second<<") ";}
template<class T> inline void print(vector<pair<T,T>> v){for(auto e:v)print(e); cout<<endl;}
template<class T> inline void print(T v){for(auto e:v)cout<<e<<" ";cout<<endl;}

unsigned xorshift()
{
    static unsigned x=123456789, y=362436069, z=521288629, w=8867512;
    unsigned t=(x^(x<<11));
    x=y; y=z; z=w;
    return w=(w^(w>>19))^(t^(t>>8));
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int n){
    return uniform_int_distribution<int>(0, INT_MAX)(rng)%n;
}
double drnd(){
    return (double)(rnd(INT_MAX))/INT_MAX;
}
void shuffle(vec &x){
    int n=x.size();
    rep3(i,n-1,1){
        swap(x[i],x[rnd(i-1)]);
    }
}
clock_t start;
double time(){
    return (double)(clock()-start)/CLOCKS_PER_SEC;
}
ll M[4]={1000000000000037,9007199254740997,92709568269121 ,9999999999999937};

ll rh[4][510000];

ll gcd(ll x,ll y){
    if(y==0)return x;
    return gcd(y,x%y);
}
main(){
    ios
    int t=in();
    while(t--){
        int n=in(),m=in();
        vecll a;
        rep(i,n)a.pb(lin());
        vecll p[4];
        ll k[4];
        rep(i,4)k[i]=rnd(INT_MAX);
        ll t[4]={1,1,1,1};
        rep(i,n+1){
            rep(j,4)p[j].pb(t[j]);
            rep(j,4)t[j]=t[j]*k[j]%M[j];
        }
        rep(i,4)rep(j,n+1)rh[i][j]=0;
        rep(i,m){
            int x=in(),y=in();
            rep(j,4){
                rh[j][y]=(rh[j][y]+p[j][x])%M[j];
            }
        }
        map<vecll,ll> mp;
        rep2(i,1,n){
            vecll v;
            rep(j,4) v.eb(rh[j][i]);
            bool flag=0;
            for(auto e:v)if(e)flag=1;
            if(!flag)continue;
            mp[v]+=a[i-1];
        }
        ll g=0;
        for(auto e:mp){
            g=gcd(g,e.second);
        }
        cout<<g<<endl;
    }

}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep2(i,a,b) for(ll i=a;i<=b;++i)
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep3(i,a,b) for(ll i=a;i>=b;i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define vec vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define vec2(a,b) vector<vec>(a,vec(b))
#define vec2ll(a,b) vector<vecll>(a,vecll(b))
#define vec3(a,b,c) vector<vector<vec>>(a,vec2(b,c))
#define vec3ll(a,b,c) vector<vector<vecll>>(a,vec2ll(b,c))
#define fi first
#define se second
#define endl "\n"
#define all(c) begin(c),end(c)
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))
#define MM " "
using namespace std;
int in() {int x;cin>>x;return x;}
ll lin() {ll x;cin>>x;return x;}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T> inline void print(pair<T,T> p){cout<<"("<<p.first<<","<<p.second<<") ";}
template<class T> inline void print(vector<pair<T,T>> v){for(auto e:v)print(e); cout<<endl;}
template<class T> inline void print(T v){for(auto e:v)cout<<e<<" ";cout<<endl;}

unsigned xorshift()
{
    static unsigned x=123456789, y=362436069, z=521288629, w=8867512;
    unsigned t=(x^(x<<11));
    x=y; y=z; z=w;
    return w=(w^(w>>19))^(t^(t>>8));
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int n){
    return uniform_int_distribution<int>(0, INT_MAX)(rng)%n;
}
double drnd(){
    return (double)(rnd(INT_MAX))/INT_MAX;
}
void shuffle(vec &x){
    int n=x.size();
    rep3(i,n-1,1){
        swap(x[i],x[rnd(i-1)]);
    }
}
clock_t start;
double time(){
    return (double)(clock()-start)/CLOCKS_PER_SEC;
}
ll M[4]={1000000000000037,9007199254740997,92709568269121 ,9999999999999937};

ll rh[4][510000];

ll gcd(ll x,ll y){
    if(y==0)return x;
    return gcd(y,x%y);
}
main(){
    ios
    int t=in();
    while(t--){
        int n=in(),m=in();
        vecll a;
        rep(i,n)a.pb(lin());
        vecll p[4];
        ll k[4];
        rep(i,4)k[i]=rnd(INT_MAX);
        ll t[4]={1,1,1,1};
        rep(i,n+1){
            rep(j,2)p[j].pb(t[j]);
            rep(j,2)t[j]=t[j]*k[j]%M[j];
        }
        rep(i,2)rep(j,n+1)rh[i][j]=0;
        rep(i,m){
            int x=in(),y=in();
            rep(j,2){
                rh[j][y]=(rh[j][y]+p[j][x])%M[j];
            }
        }
        map<pll,ll> mp;
        rep2(i,1,n){
            pll p={rh[0][i],rh[1][i]};
            bool flag=0;
            if(p!=pll{0ll,0ll})flag=1;
            if(!flag)continue;
            mp[p]+=a[i-1];
        }
        ll g=0;
        for(auto e:mp){
            g=gcd(g,e.second);
        }
        cout<<g<<endl;
    }

}


 #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep2(i,a,b) for(ll i=a;i<=b;++i)
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep3(i,a,b) for(ll i=a;i>=b;i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define vec vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define vec2(a,b) vector<vec>(a,vec(b))
#define vec2ll(a,b) vector<vecll>(a,vecll(b))
#define vec3(a,b,c) vector<vector<vec>>(a,vec2(b,c))
#define vec3ll(a,b,c) vector<vector<vecll>>(a,vec2ll(b,c))
#define fi first
#define se second
#define endl "\n"
#define all(c) begin(c),end(c)
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))
#define MM " "
using namespace std;
int in() {int x;cin>>x;return x;}
ll lin() {ll x;cin>>x;return x;}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T> inline void print(pair<T,T> p){cout<<"("<<p.first<<","<<p.second<<") ";}
template<class T> inline void print(vector<pair<T,T>> v){for(auto e:v)print(e); cout<<endl;}
template<class T> inline void print(T v){for(auto e:v)cout<<e<<" ";cout<<endl;}

unsigned xorshift()
{
    static unsigned x=123456789, y=362436069, z=521288629, w=8867512;
    unsigned t=(x^(x<<11));
    x=y; y=z; z=w;
    return w=(w^(w>>19))^(t^(t>>8));
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int n){
    return uniform_int_distribution<int>(0, INT_MAX)(rng)%n;
}
double drnd(){
    return (double)(rnd(INT_MAX))/INT_MAX;
}
void shuffle(vec &x){
    int n=x.size();
    rep3(i,n-1,1){
        swap(x[i],x[rnd(i-1)]);
    }
}
clock_t start;
double time(){
    return (double)(clock()-start)/CLOCKS_PER_SEC;
}
ll M[4]={1000000000000037,9007199254740997,92709568269121 ,9999999999999937};

ll rh[4][510000];

ll gcd(ll x,ll y){
    if(y==0)return x;
    return gcd(y,x%y);
}
main(){
    ios
    int t=in();
    while(t--){
        int n=in(),m=in();
        vecll a;
        rep(i,n)a.pb(lin());
        vecll p[4];
        ll k[4];
        rep(i,4)k[i]=rnd(1000)+2;
        ll t[4]={1,1,1,1};
        rep(i,n+1){
            rep(j,4)p[j].pb(t[j]);
            rep(j,4)t[j]=t[j]*k[j]%M[j];
        }
        rep(i,4)rep(j,n+1)rh[i][j]=0;
        rep(i,m){
            int x=in(),y=in();
            rep(j,4){
                rh[j][y]=(rh[j][y]+p[j][x])%M[j];
            }
        }
        map<vecll,ll> mp;
        rep2(i,1,n){
            vecll v;
            rep(j,4) v.eb(rh[j][i]);
            bool flag=0;
            for(auto e:v)if(e)flag=1;
            if(!flag)continue;
            mp[v]+=a[i-1];
        }
        ll g=0;
        for(auto e:mp){
            g=gcd(g,e.second);
        }
        cout<<g<<endl;
    }

}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll amt[500005];
ll c[500005];
int blk[500005];
vector<int> adj[500005],radj[500005];
map<vector<int>,ll> vmap;

ll mygcd(ll a, ll b){
    if(!b)
        return a;
    return mygcd(b,a%b);
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n,m;
        vmap.clear();
        scanf("%d%d",&n,&m);
        for(int i = 1; i<=n; i++){
            scanf("%lld",&c[i]);
            amt[i] = 0;
            adj[i].clear();
            radj[i].clear();
            blk[i] = 0;
        }
        for(int i = 0; i<m; i++){
            int u,v;
            scanf("%d%d",&u,&v);
            adj[u].push_back(v);
            radj[v].push_back(u);
            amt[u]+=c[v];
        }
        ll g = 0;
        for(int i = 1; i<=n; i++){
            if(radj[i].empty())
                continue;
            sort(radj[i].begin(),radj[i].end());
            vmap[radj[i]]+=c[i];
            g+=c[i];
        }
        for(auto it:vmap)
            g = mygcd(g,it.second);
        printf("%lld\n",g);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll amt[500005];
ll c[500005];
int blk[500005];
vector<int> adj[500005],radj[500005];

ll mygcd(ll a, ll b){
    if(!b)
        return a;
    return mygcd(b,a%b);
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i = 1; i<=n; i++){
            scanf("%lld",&c[i]);
            amt[i] = 0;
            adj[i].clear();
            radj[i].clear();
            blk[i] = 0;
        }
        for(int i = 0; i<m; i++){
            int u,v;
            scanf("%d%d",&u,&v);
            adj[u].push_back(v);
            radj[v].push_back(u);
            amt[u]+=c[v];
        }
        ll g = 0;
        for(int i = 1; i<=n; i++)
            g=mygcd(g,amt[i]);
        for(int i = 1; i<=n; i++){
            g = mygcd(g,amt[i]);
            for(auto it:adj[i]){
                if(!blk[it]){
                    blk[it] = 1;
                    for(auto it2:radj[it])
                        amt[it2]-=c[it];
                }
            }
        }
        printf("%lld\n",g);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll amt[500005];
ll c[500005];
int blk[500005];
vector<int> adj[500005],radj[500005];

ll mygcd(ll a, ll b){
    if(!b)
        return a;
    return mygcd(b,a%b);
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i = 1; i<=n; i++){
            scanf("%lld",&c[i]);
            amt[i] = 0;
            adj[i].clear();
            radj[i].clear();
            blk[i] = 0;
        }
        ll g = 0;
        for(int i = 0; i<m; i++){
            int u,v;
            scanf("%d%d",&u,&v);
            adj[u].push_back(v);
            radj[v].push_back(u);
            amt[u]+=c[v];
        }
        for(int i = 1; i<=n; i++){
            if(!radj[i].empty())
                g+=c[i];
        }
        for(int i = 1; i<=n; i++)
            g=mygcd(g,amt[i]);
        for(int i = 1; i<=n; i++){
            g = mygcd(g,amt[i]);
            for(auto it:adj[i]){
                if(!blk[it]){
                    blk[it] = 1;
                    for(auto it2:radj[it])
                        amt[it2]-=c[it];
                }
            }
        }
        printf("%lld\n",g);
    }
    return 0;
}


// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx,avx2,fma")

#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <chrono>
#include <vector>
#include <map>
#include <random>
#include <set>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <stdio.h>
#include <queue>
#include <bitset>
#include <cstdlib>
#include <deque>
#include <cassert>
#include <stack>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;

#define mp make_pair
#define f first
#define se second
#define pb push_back
#define ppb pop_back
#define ll long long
#define ull unsigned long long
#define cntbit(x) __builtin_popcount(x)
#define endl '\n'
#define uset unordered_set
#define umap unordered_map
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define ld long double
#define pll pair<long long, long long>
// #define int long long

const int inf = 2e9;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// uniform_int_distribution <int> range(0, inf);

template <typename T> void Min(T &a, T b) {
    a = min(a, b);
}

template <typename T> void Max(T &a, T b) {
    a = max(a, b);
}

ostream &operator << (ostream &cout, vector <int> &a) {
	cout << "{";
	for (auto & it : a) cout << it << ", ";
	cout << "}";
	return cout;
}

ostream &operator << (ostream &cout, set <int> &a) {
	cout << "{";
	for (auto & it : a) cout << it << ", ";
	cout << "}";
	return cout;
}

const int mod[2] = {1000000007, 1000000009};
const int pr[2] = {9999901, 10000019};
const int N = 5e5 + 15;
int n, m;
ll a[N];
vector <int> gr[N];

int p[N], sz[N];

map <vector <int>, int> ind;

ll val[N];

int st[2][N];

inline void add(int &a, int b, int mod) {
	a += b;
	if(a >= mod)
		a -= mod;
}

inline int mt(int a, int b, int mod) {
	return (ll)a * b % mod;
}

int find(int v) {
	if(v == p[v])
		return v;
	return p[v] = find(p[v]);
}

inline void unio(int a, int b) {
	a = find(a);
	b = find(b);
	if(a != b) {
		if(sz[a] < sz[b])
			swap(a, b);
		p[b] = a;
		sz[a] += sz[b];
		val[a] += val[b];
	}
}

inline void input() {
	cin >> n >> m;
	for(int i = 1; i <= n; ++i)
		cin >> a[i];
	for(int i = 1; i <= n; ++i)
		gr[i].clear(), p[i] = i, sz[i] = 1, val[i] = a[i];
	for(int i = 1; i <= m; ++i) {
		int u, v;
		cin >> u >> v;
		gr[v].pb(u);
	}
}

inline void solve() {
	for(int i = 1; i <= n; ++i) {
		if(gr[i].empty())
			continue;
		// int h[2] = {0, 0};
		// for(int j = 0; j < 2; ++j)
		// 	for(int to : gr[i])
		// 		add(h[j], st[j][to], mod[j]);
		sort(gr[i].begin(), gr[i].end());
		if(ind.count(gr[i]))
			unio(i, ind[gr[i]]);
		ind[gr[i]] = i;
	}
	ll g = 0;
	for(int i = 1; i <= n; ++i) {
		if(gr[i].empty())
			continue;
		g = __gcd(g, val[find(i)]);
	}
	cout << g << endl;
}

main() {
	for(int i = 0; i < 2; ++i) {
		st[i][0] = 1;
		for(int j = 1; j < N; ++j)
			st[i][j] = mt(st[i][j-1], pr[i], mod[i]);
	}
	ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);
	int t;
	cin >> t;
	while(t--) {
		input();
		solve();
	}
	return 0;
}


// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx,avx2,fma")

#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <chrono>
#include <vector>
#include <map>
#include <random>
#include <set>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <stdio.h>
#include <queue>
#include <bitset>
#include <cstdlib>
#include <deque>
#include <cassert>
#include <stack>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;

#define mp make_pair
#define f first
#define se second
#define pb push_back
#define ppb pop_back
#define ll long long
#define ull unsigned long long
#define cntbit(x) __builtin_popcount(x)
#define endl '\n'
#define uset unordered_set
#define umap unordered_map
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define ld long double
#define pll pair<long long, long long>
// #define int long long

const int inf = 2e9;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// uniform_int_distribution <int> range(0, inf);

template <typename T> void Min(T &a, T b) {
    a = min(a, b);
}

template <typename T> void Max(T &a, T b) {
    a = max(a, b);
}

ostream &operator << (ostream &cout, vector <int> &a) {
	cout << "{";
	for (auto & it : a) cout << it << ", ";
	cout << "}";
	return cout;
}

ostream &operator << (ostream &cout, set <int> &a) {
	cout << "{";
	for (auto & it : a) cout << it << ", ";
	cout << "}";
	return cout;
}

const int mod[2] = {1000000007, 1000000009};
const int pr[2] = {9999901, 10000019};
const int N = 5e5 + 15;
int n, m;
ll a[N];
vector <int> gr[N];

int p[N], sz[N];

map <vector <int>, int> ind;

ll val[N];

int st[2][N];

inline void add(int &a, int b, int mod) {
	a += b;
	if(a >= mod)
		a -= mod;
}

inline int mt(int a, int b, int mod) {
	return (ll)a * b % mod;
}

int find(int v) {
	if(v == p[v])
		return v;
	return p[v] = find(p[v]);
}

inline void unio(int a, int b) {
	a = find(a);
	b = find(b);
	if(a != b) {
		if(sz[a] < sz[b])
			swap(a, b);
		p[b] = a;
		sz[a] += sz[b];
		val[a] += val[b];
	}
}

inline void input() {
	ind.clear();
	cin >> n >> m;
	for(int i = 1; i <= n; ++i)
		cin >> a[i];
	for(int i = 1; i <= n; ++i)
		gr[i].clear(), p[i] = i, sz[i] = 1, val[i] = a[i];
	for(int i = 1; i <= m; ++i) {
		int u, v;
		cin >> u >> v;
		gr[v].pb(u);
	}
}

inline void solve() {
	for(int i = 1; i <= n; ++i) {
		if(gr[i].empty())
			continue;
		// int h[2] = {0, 0};
		// for(int j = 0; j < 2; ++j)
		// 	for(int to : gr[i])
		// 		add(h[j], st[j][to], mod[j]);
		sort(gr[i].begin(), gr[i].end());
		if(ind.find(gr[i]) != ind.end())
			unio(i, ind[gr[i]]);
		ind[gr[i]] = i;
	}
	ll g = 0;
	for(int i = 1; i <= n; ++i) {
		if(gr[i].empty())
			continue;
		g = __gcd(g, val[find(i)]);
	}
	cout << g << endl;
}

main() {
	for(int i = 0; i < 2; ++i) {
		st[i][0] = 1;
		for(int j = 1; j < N; ++j)
			st[i][j] = mt(st[i][j-1], pr[i], mod[i]);
	}
	ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);
	int t;
	cin >> t;
	while(t--) {
		input();
		solve();
	}
	return 0;
}


// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx,avx2,fma")

#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <chrono>
#include <vector>
#include <map>
#include <random>
#include <set>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <stdio.h>
#include <queue>
#include <bitset>
#include <cstdlib>
#include <deque>
#include <cassert>
#include <stack>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;

#define mp make_pair
#define f first
#define se second
#define pb push_back
#define ppb pop_back
#define ll long long
#define ull unsigned long long
#define cntbit(x) __builtin_popcount(x)
#define endl '\n'
#define uset unordered_set
#define umap unordered_map
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define ld long double
#define pll pair<long long, long long>

const int inf = 2e9;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// uniform_int_distribution <int> range(0, inf);

template <typename T> void Min(T &a, T b) {
    a = min(a, b);
}

template <typename T> void Max(T &a, T b) {
    a = max(a, b);
}

ostream &operator << (ostream &cout, vector <int> &a) {
	cout << "{";
	for (auto & it : a) cout << it << ", ";
	cout << "}";
	return cout;
}

ostream &operator << (ostream &cout, set <int> &a) {
	cout << "{";
	for (auto & it : a) cout << it << ", ";
	cout << "}";
	return cout;
}

const int mod[2] = {1000000007, 1000000009};
const int pr[2] = {9999901, 10000019};
const int N = 5e5 + 15;
int n, m;
ll a[N];
vector <int> gr[N];

int p[N], sz[N];

map <pii, int> ind;

ll val[N];

int st[2][N];

inline void add(int &a, int b, int mod) {
	a += b;
	if(a >= mod)
		a -= mod;
}

inline int mt(int a, int b, int mod) {
	return (ll)a * b % mod;
}

int find(int v) {
	if(v == p[v])
		return v;
	return p[v] = find(p[v]);
}

inline void unio(int a, int b) {
	a = find(a);
	b = find(b);
	if(a != b) {
		if(sz[a] < sz[b])
			swap(a, b);
		p[b] = a;
		sz[a] += sz[b];
		val[a] += val[b];
	}
}

inline void input() {
	ind.clear();
	cin >> n >> m;
	for(int i = 1; i <= n; ++i)
		cin >> a[i];
	for(int i = 1; i <= n; ++i)
		gr[i].clear(), p[i] = i, sz[i] = 1, val[i] = a[i];
	for(int i = 1; i <= m; ++i) {
		int u, v;
		cin >> u >> v;
		gr[v].pb(u);
	}
}

inline void solve() {
	for(int i = 1; i <= n; ++i) {
		int h[2] = {0, 0};
		for(int j = 0; j < 2; ++j)
			for(int to : gr[i])
				add(h[j], st[j][to], mod[j]);
		if(ind.count(mp(h[0], h[1])))
			unio(i, ind[mp(h[0], h[1])]);
		ind[mp(h[0], h[1])] = i;
	}
	ll g = 0;
	for(int i = 1; i <= n; ++i) {
		if(gr[i].empty())
			continue;
		g = __gcd(g, val[find(i)]);
	}
	cout << g << endl;
}

main() {
	for(int i = 0; i < 2; ++i) {
		st[i][0] = 1;
		for(int j = 1; j < N; ++j)
			st[i][j] = mt(st[i][j-1], pr[i], mod[i]);
	}
	ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);
	int t;
	cin >> t;
	while(t--) {
		input();
		solve();
	}
	return 0;
}


// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx,avx2,fma")

#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <chrono>
#include <vector>
#include <map>
#include <random>
#include <set>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <stdio.h>
#include <queue>
#include <bitset>
#include <cstdlib>
#include <deque>
#include <cassert>
#include <stack>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;

#define mp make_pair
#define f first
#define se second
#define pb push_back
#define ppb pop_back
#define ll long long
#define ull unsigned long long
#define cntbit(x) __builtin_popcount(x)
#define endl '\n'
#define uset unordered_set
#define umap unordered_map
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define ld long double
#define pll pair<long long, long long>
#define int long long

const int inf = 2e9;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// uniform_int_distribution <int> range(0, inf);

template <typename T> void Min(T &a, T b) {
    a = min(a, b);
}

template <typename T> void Max(T &a, T b) {
    a = max(a, b);
}

ostream &operator << (ostream &cout, vector <int> &a) {
	cout << "{";
	for (auto & it : a) cout << it << ", ";
	cout << "}";
	return cout;
}

ostream &operator << (ostream &cout, set <int> &a) {
	cout << "{";
	for (auto & it : a) cout << it << ", ";
	cout << "}";
	return cout;
}

const int mod[2] = {1000000007, 1000000009};
const int pr[2] = {9999901, 10000019};
const int N = 5e5 + 15;
int n, m;
ll a[N];
vector <int> gr[N];

int p[N], sz[N];

map <pll, int> ind;

ll val[N];

int st[2][N];

inline void add(int &a, int b, int mod) {
	a += b;
	if(a >= mod)
		a -= mod;
}

inline int mt(int a, int b, int mod) {
	return (ll)a * b % mod;
}

int find(int v) {
	if(v == p[v])
		return v;
	return p[v] = find(p[v]);
}

inline void unio(int a, int b) {
	a = find(a);
	b = find(b);
	if(a != b) {
		if(sz[a] < sz[b])
			swap(a, b);
		p[b] = a;
		sz[a] += sz[b];
		val[a] += val[b];
	}
}

inline void input() {
	cin >> n >> m;
	for(int i = 1; i <= n; ++i)
		cin >> a[i];
	for(int i = 1; i <= n; ++i)
		gr[i].clear(), p[i] = i, sz[i] = 1, val[i] = a[i];
	for(int i = 1; i <= m; ++i) {
		int u, v;
		cin >> u >> v;
		gr[v].pb(u);
	}
}

inline void solve() {
	for(int i = 1; i <= n; ++i) {
		if(gr[i].empty())
			continue;
		int h[2] = {0, 0};
		for(int j = 0; j < 2; ++j)
			for(int to : gr[i])
				add(h[j], st[j][to], mod[j]);
		if(ind.count(mp(h[0], h[1])))
			unio(i, ind[mp(h[0], h[1])]);
		ind[mp(h[0], h[1])] = i;
	}
	ll g = 0;
	for(int i = 1; i <= n; ++i) {
		if(gr[i].empty())
			continue;
		g = __gcd(g, val[find(i)]);
	}
	cout << g << endl;
}

main() {
	for(int i = 0; i < 2; ++i) {
		st[i][0] = 1;
		for(int j = 1; j < N; ++j)
			st[i][j] = mt(st[i][j-1], pr[i], mod[i]);
	}
	ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);
	int t;
	cin >> t;
	while(t--) {
		input();
		solve();
	}
	return 0;
}


// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx,avx2,fma")

#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <chrono>
#include <vector>
#include <map>
#include <random>
#include <set>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <stdio.h>
#include <queue>
#include <bitset>
#include <cstdlib>
#include <deque>
#include <cassert>
#include <stack>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;

#define mp make_pair
#define f first
#define se second
#define pb push_back
#define ppb pop_back
#define ll long long
#define ull unsigned long long
#define cntbit(x) __builtin_popcount(x)
#define endl '\n'
#define uset unordered_set
#define umap unordered_map
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define ld long double
#define pll pair<long long, long long>

const int inf = 2e9;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_int_distribution <int> range(0, inf);

template <typename T> void Min(T &a, T b) {
    a = min(a, b);
}

template <typename T> void Max(T &a, T b) {
    a = max(a, b);
}

ostream &operator << (ostream &cout, vector <int> &a) {
	cout << "{";
	for (auto & it : a) cout << it << ", ";
	cout << "}";
	return cout;
}

ostream &operator << (ostream &cout, set <int> &a) {
	cout << "{";
	for (auto & it : a) cout << it << ", ";
	cout << "}";
	return cout;
}

const int mod[2] = {1000000007, 1000000009};
const int pr[2] = {9999901, 10000019};
const int N = 5e5 + 15;
int n, m;
ll a[N];
vector <int> gr[N];

int p[N], sz[N];

map <pii, int> ind;

ll val[N];

int st[2][N];

inline void add(int &a, int b, int mod) {
	a += b;
	if(a >= mod)
		a -= mod;
}

inline int mt(int a, int b, int mod) {
	return (ll)a * b % mod;
}

int find(int v) {
	if(v == p[v])
		return v;
	return p[v] = find(p[v]);
}

inline void unio(int a, int b) {
	a = find(a);
	b = find(b);
	if(a != b) {
		if(sz[a] < sz[b])
			swap(a, b);
		p[b] = a;
		sz[a] += sz[b];
		val[a] += val[b];
	}
}

inline void input() {
	cin >> n >> m;
	for(int i = 1; i <= n; ++i)
		cin >> a[i];
	for(int i = 1; i <= n; ++i)
		gr[i].clear(), p[i] = i, sz[i] = 1, val[i] = a[i];
	for(int i = 1; i <= m; ++i) {
		int u, v;
		cin >> u >> v;
		gr[v].pb(u);
	}
}

inline void solve() {
	for(int i = 1; i <= n; ++i) {
		int h[2] = {0, 0};
		for(int j = 0; j < 2; ++j)
			for(int to : gr[i])
				add(h[j], st[j][to], mod[j]);
		if(ind.count(mp(h[0], h[1])))
			unio(i, ind[mp(h[0], h[1])]);
		ind[mp(h[0], h[1])] = i;
	}
	ll g = 0;
	for(int i = 1; i <= n; ++i) {
		if(gr[i].empty())
			continue;
		g = __gcd(g, val[find(i)]);
	}
	cout << g << endl;
}

main() {
	for(int i = 0; i < 2; ++i) {
		st[i][0] = 1;
		for(int j = 1; j < N; ++j)
			st[i][j] = mt(st[i][j-1], pr[i], mod[i]);
	}
	ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);
	int t;
	cin >> t;
	while(t--) {
		input();
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define ii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define INF 100000000000000000
#define modulo 1000000007
#define mod 998244353
#define int long long int
using namespace std;

int32_t main(){
      ios_base::sync_with_stdio(false);
      cin.tie(NULL);

  //    freopen("q.gir", "r", stdin);
  //    freopen("q.cik", "w", stdout);

      int q;
      cin >> q;
      while(q--){
            int n, m;
            cin >> n >> m;
            vector<int> deg(n + 1, 0);
            vector<int> adj[n + n + 1];
            vector<int> arr(n + 1);
            for(int i = 1; i <= n; i++) cin >> arr[i];
            for(int i = 0; i < m; i++){
                  int x, y;
                  cin >> x >> y;
                  adj[x + n].pb(y);
                  deg[y]++;
            }
            int g = 0;
            for(int i = 1; i <= n; i++){
                  if(deg[i]){
                        g += arr[i];
                  }
            }
            for(int i = n + 1; i <= n + n; i++){
                  int s = 0;
                  for(auto x : adj[i]){
                        s += arr[x];
                  }
                  if(!s)continue;
                  if(!g) g = s;
                  g = __gcd(s, g);
            }

            for(int i = n + 1; i <= n + n; i++){
                  unordered_map<int, int> sum;
                  for(auto x : adj[i]){
                        sum[deg[x]] += arr[x];
                  }
                  for(auto s : sum){
                        g = __gcd(s.second * max(1ll, s.first - 1), g);
                  }

            }
            cout << g << "\n";
      }
}


#include<bits/stdc++.h>
#define pb push_back
#define ii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define INF 100000000000000000
#define modulo 1000000007
#define mod 998244353
#define int long long int
using namespace std;

int32_t main(){
      ios_base::sync_with_stdio(false);
      cin.tie(NULL);

  //    freopen("q.gir", "r", stdin);
  //    freopen("q.cik", "w", stdout);

      int q;
      cin >> q;
      while(q--){
            int n, m;
            cin >> n >> m;
            vector<int> deg(n + 1, 0);
            vector<int> adj[n + n + 1];
            vector<int> arr(n + 1);
            for(int i = 1; i <= n; i++) cin >> arr[i];
            for(int i = 0; i < m; i++){
                  int x, y;
                  cin >> x >> y;
                  adj[x + n].pb(y);
                  deg[y]++;
            }
            int g = 0;
            for(int i = 1; i <= n; i++){
                  if(deg[i]){
                        g += arr[i];
                  }
            }
            for(int i = n + 1; i <= n + n; i++){
                  int s = 0;
                  for(auto x : adj[i]){
                        s += arr[x];
                  }
                  if(!s)continue;
                  if(!g) g = s;
                  g = __gcd(s, g);
            }
            for(int k = 1; k < 100; k++)
            for(int i = n + 1; i <= n + n; i++){
                  int s = 0;
                  for(auto x : adj[i]){
                        if(deg[x] == k) s += arr[x];
                  }
                  if(!s)continue;
                  if(!g) g = s;
                  g = __gcd(s * max(1ll, k - 1), g);
            }
            cout << g << "\n";
      }
}


#include<bits/stdc++.h>
#define pb push_back
#define ii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define INF 100000000000000000
#define modulo 1000000007
#define mod 998244353
#define int long long int
using namespace std;

int32_t main(){
      ios_base::sync_with_stdio(false);
      cin.tie(NULL);

  //    freopen("q.gir", "r", stdin);
  //    freopen("q.cik", "w", stdout);

      int q;
      cin >> q;
      while(q--){
            int n, m;
            cin >> n >> m;
            vector<int> deg(n + 1, 0);
            vector<int> adj[n + n + 1];
            vector<int> arr(n + 1);
            for(int i = 1; i <= n; i++) cin >> arr[i];
            for(int i = 0; i < m; i++){
                  int x, y;
                  cin >> x >> y;
                  adj[x + n].pb(y);
                  deg[y]++;
            }
            int g = 0;
            for(int i = n + 1; i <= n + n; i++){
                  int s = 0;
                  for(auto x : adj[i]){
                        s += arr[x];
                  }
                  if(!s)continue;
                  if(!g) g = s;
                  g = __gcd(s, g);
            }
            for(int i = n + 1; i <= n + n; i++){
                  int s = 0;
                  for(auto x : adj[i]){
                        if(deg[x] == 1) s += arr[x];
                  }
                  if(!s)continue;
                  if(!g) g = s;
                  g = __gcd(s, g);
            }
            cout << g << "\n";
      }
}


#include<bits/stdc++.h>
#define pb push_back
#define ii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define INF 100000000000000000
#define modulo 1000000007
#define mod 998244353
#define int long long int
using namespace std;

int32_t main(){
      ios_base::sync_with_stdio(false);
      cin.tie(NULL);

  //    freopen("q.gir", "r", stdin);
  //    freopen("q.cik", "w", stdout);

      int q;
      cin >> q;
      while(q--){
            int n, m;
            cin >> n >> m;
            vector<int> deg(n + 1, 0);
            vector<int> adj[n + n + 1];
            vector<int> arr(n + 1);
            for(int i = 1; i <= n; i++) cin >> arr[i];
            for(int i = 0; i < m; i++){
                  int x, y;
                  cin >> x >> y;
                  adj[x + n].pb(y);
                  deg[y]++;
            }
            int g = 0;
            for(int i = 1; i <= n; i++){
                  if(deg[i]){
                        g += arr[i];
                  }
            }
            for(int i = n + 1; i <= n + n; i++){
                  int s = 0;
                  for(auto x : adj[i]){
                        s += arr[x];
                  }
                  if(!s)continue;
                  if(!g) g = s;
                  g = __gcd(s, g);
            }
            for(int i = n + 1; i <= n + n; i++){
                  int s = 0;
                  for(auto x : adj[i]){
                        if(deg[x] == 1) s += arr[x];
                  }
                  if(!s)continue;
                  if(!g) g = s;
                  g = __gcd(s, g);
            }
            cout << g << "\n";
      }
}


#include<bits/stdc++.h>
#define pb push_back
#define ii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define INF 100000000000000000
#define modulo 1000000007
#define mod 998244353
#define int long long int
using namespace std;

int32_t main(){
      ios_base::sync_with_stdio(false);
      cin.tie(NULL);

  //    freopen("q.gir", "r", stdin);
  //    freopen("q.cik", "w", stdout);

      int q;
      cin >> q;
      while(q--){
            int n, m;
            cin >> n >> m;
            vector<int> deg(n + 1, 0);
            vector<int> adj[n + n + 1];
            vector<int> arr(n + 1);
            for(int i = 1; i <= n; i++) cin >> arr[i];
            for(int i = 0; i < m; i++){
                  int x, y;
                  cin >> x >> y;
                  adj[x + n].pb(y);
                  deg[y]++;
            }
            int g = 0;
            for(int i = 1; i <= n; i++){
                  if(deg[i]){
                        g += arr[i];
                  }
            }
            for(int i = n + 1; i <= n + n; i++){
                  int s = 0;
                  for(auto x : adj[i]){
                        s += arr[x];
                  }
                  if(!s)continue;
                  if(!g) g = s;
                  g = __gcd(s, g);
            }
            for(int k = 1; k < 10; k++)
            for(int i = n + 1; i <= n + n; i++){
                  int s = 0;
                  for(auto x : adj[i]){
                        if(deg[x] == k) s += arr[x];
                  }
                  if(!s)continue;
                  if(!g) g = s;
                  g = __gcd(s * max(1ll, k - 1), g);
            }
            cout << g << "\n";
      }
}


#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
//using namespace __gnu_pbds;
//typedef tree<
//        pair<long long, int>, // change type
//        null_type,
//        less<pair<long long, int> >, // change type
//        rb_tree_tag,
//        tree_order_statistics_node_update>
//        ordered_set;

//template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

typedef long long ll;
#define rep(i, start, end) for(int i = start; i < end; ++i)
#define sz(x) (int)(x).size()
#define pb push_back
#define st first
#define en second
#define all(x) x.begin(), x.end()
#define clr(d, v) memset(d, v, sizeof(d))
#define pii pair<int, int>
#define debug(x) cerr << #x << " : " << (x) << endl
const double PI = 3.14159265358979323846;
const double eps = (1e-10);
int isGreater(long double x, long double y)
{
    if (abs(x - y) < eps)
        return 0;
    if (x > y)
        return 1;
    return -1;
}

void solve()
{
    int n, m;
    cin >> n >> m;
    vector<ll> val(n);
    rep(i,0,n)
        cin >> val[i];
    vector<vector<int> > adj(n);
    vector<vector<int> > rAdj(n);
    rep(i,0,m)
    {
        int u, v;
        cin >> u >> v;
        --u; --v;
        adj[u].pb(v);
        rAdj[v].pb(u);
    }
    ll g = 0;
    map<vector<int>, ll> mp;
    for (int v = 0; v < n; ++v)
    {
        vector<int> temp;
        for (int u : rAdj[v])
            temp.push_back(u);
        sort(all(temp));
        if (!temp.empty())
            mp[temp] += val[v];
    }

    for (auto &item : mp)
        g = __gcd(g, item.second);

    cout << g << '\n';
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
//    freopen("AC.txt", "w", stdout);
#endif
    int tc;
    cin >> tc;
    while(tc--)
        solve();
    return 0;
}


# include <bits/stdc++.h>
using namespace std;
const int MAXN = 500005;
    vector <int>  adj[MAXN];
long long val[MAXN];

void solve()
{
 int i,j;
    int n,m;
    cin>>n>>m;
for(i=1;i<=n;i++)adj[i].clear();
     vector <int> g;
     map <vector <int>, long long> mp;

    for(i=1;i<=n;i++)
        cin>>val[i];
        int u,v;
    for(i=1;i<=m;i++)
    {
        cin>>u>>v;
        adj[v].push_back(u);
    }
    for(i=1;i<=n;i++)
    {
        if(adj[i].size()==0)continue;
        g.clear();
        for(j=0;j<adj[i].size();j++)
        {
            g.push_back(adj[i][j]);
        }
        sort(g.begin(),g.end());
     //  for(j=0;j<g.size();j++)cout<<g[j]<<" & ";
      // cout<<endl;
        mp[g]+=val[i];
    }
    long long ans = 0;
    for(auto it: mp)
    {
      //  cout<<it.second<<" * ";
        ans = __gcd(ans,it.second);
    }
  //  cout<<endl;
    cout<<ans<<endl;
    return ;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
int t;
cin>>t;
while(t)
{
    t--;
    solve();
}
}


# include <bits/stdc++.h>
using namespace std;
const int MAXN = 500005;
    vector <int>  adj[MAXN];
long long val[MAXN];

void solve()
{
 int i,j;
    int n,m;
    cin>>n>>m;
for(i=1;i<=n;i++)adj[i].clear();
     vector <int> g;
     map <vector <int>, long long> mp;

    for(i=1;i<=n;i++)
        cin>>val[i];
        int u,v;
    for(i=1;i<=m;i++)
    {
        cin>>u>>v;
        adj[v].push_back(u);
    }
    for(i=1;i<=n;i++)
    {
        if(adj[i].size()==0)continue;
        g.clear();
        for(j=0;j<adj[i].size();j++)
        {
            g.push_back(adj[i][j]);
        }
        sort(g.begin(),g.end());
     //  for(j=0;j<g.size();j++)cout<<g[j]<<" & ";
      // cout<<endl;
        mp[g]+=val[i];
    }
    long long ans = 0;
    for(auto it: mp)
    {
      //  cout<<it.second<<" * ";
        ans = __gcd(ans,it.second);
    }
  //  cout<<endl;
    cout<<ans<<endl;
    return ;
}
int main()
{
int t;
cin>>t;
while(t)
{
    t--;
    solve();
}
}


# include <bits/stdc++.h>
using namespace std;
const int MAXN = 500005;

long long val[MAXN];

void solve()
{
 int i,j;
    int n,m;
    cin>>n>>m;
    vector <vector <int> > adj(n+2);
//for(i=1;i<=n;i++)adj[i].clear();
     vector <int> g;
     map <vector <int>, long long> mp;

    for(i=1;i<=n;i++)
        cin>>val[i];
        int u,v;
    for(i=1;i<=m;i++)
    {
        cin>>u>>v;
        adj[v].push_back(u);
    }
    for(i=1;i<=n;i++)
    {
        if(adj[i].size()==0)continue;
        g.clear();
        for(j=0;j<adj[i].size();j++)
        {
            g.push_back(adj[i][j]);
        }
        sort(g.begin(),g.end());
     //  for(j=0;j<g.size();j++)cout<<g[j]<<" & ";
      // cout<<endl;
        mp[g]+=val[i];
    }
    long long ans = 0;
    for(auto it: mp)
    {
      //  cout<<it.second<<" * ";
        ans = __gcd(ans,it.second);
    }
  //  cout<<endl;
    cout<<ans<<endl;
    return ;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
int t;
cin>>t;
while(t)
{
    t--;
    solve();
}
}


# include <bits/stdc++.h>
using namespace std;
const int MAXN = 500005;

//long long val[MAXN];

void solve()
{
 int i,j;
    int n,m;
    cin>>n>>m;
    vector <vector <int> > adj(n+2);
    vector <long long> val(n+3);
//for(i=1;i<=n;i++)adj[i].clear();
    //
     map <vector <int>, long long> mp;

    for(i=1;i<=n;i++)
        cin>>val[i];
        int u,v;
    for(i=1;i<=m;i++)
    {
        cin>>u>>v;
        adj[v].push_back(u);
    }
    for(i=1;i<=n;i++)
    {vector <int> g;
        if(adj[i].size()==0)continue;
       // g.clear();
        for(j=0;j<adj[i].size();j++)
        {
            g.push_back(adj[i][j]);
        }
        sort(g.begin(),g.end());
     //  for(j=0;j<g.size();j++)cout<<g[j]<<" & ";
      // cout<<endl;
        mp[g]+=val[i];
    }
    long long ans = 0;
    for(auto &it: mp)
    {
      //  cout<<it.second<<" * ";
        ans = __gcd(ans,it.second);
    }
  //  cout<<endl;
    cout<<ans<<endl;
    return ;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
int t;
cin>>t;
while(t)
{
    t--;
    solve();
}
return 0;
}


# include <bits/stdc++.h>
using namespace std;
const int MAXN = 500005;

//long long val[MAXN];

void solve()
{
 int i,j;
    int n,m;
    cin>>n>>m;
    vector <vector <int> > adj(n+2);
    vector <int> val(n);
//for(i=1;i<=n;i++)adj[i].clear();
    //
     map <vector <int>, long long> mp;

    for(i=1;i<=n;i++)
        cin>>val[i];
        int u,v;
    for(i=1;i<=m;i++)
    {
        cin>>u>>v;
        adj[v].push_back(u);
    }
    for(i=1;i<=n;i++)
    {vector <int> g;
        if(adj[i].size()==0)continue;
       // g.clear();
        for(j=0;j<adj[i].size();j++)
        {
            g.push_back(adj[i][j]);
        }
        sort(g.begin(),g.end());
     //  for(j=0;j<g.size();j++)cout<<g[j]<<" & ";
      // cout<<endl;
        mp[g]+=val[i];
    }
    long long ans = 0;
    for(auto &it: mp)
    {
      //  cout<<it.second<<" * ";
        ans = __gcd(ans,it.second);
    }
  //  cout<<endl;
    cout<<ans<<endl;
    return ;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
int t;
cin>>t;
while(t)
{
    t--;
    solve();
}
return 0;
}


# include <bits/stdc++.h>
using namespace std;
const int MAXN = 500005;

long long val[MAXN];

void solve()
{
 int i,j;
    int n,m;
    cin>>n>>m;
    vector <vector <int> > adj(n+2);
//for(i=1;i<=n;i++)adj[i].clear();
    //
     map <vector <int>, long long> mp;

    for(i=1;i<=n;i++)
        cin>>val[i];
        int u,v;
    for(i=1;i<=m;i++)
    {
        cin>>u>>v;
        adj[v].push_back(u);
    }
    for(i=1;i<=n;i++)
    {vector <int> g;
        if(adj[i].size()==0)continue;
       // g.clear();
        for(j=0;j<adj[i].size();j++)
        {
            g.push_back(adj[i][j]);
        }
        sort(g.begin(),g.end());
     //  for(j=0;j<g.size();j++)cout<<g[j]<<" & ";
      // cout<<endl;
        mp[g]+=val[i];
    }
    long long ans = 0;
    for(auto &it: mp)
    {
      //  cout<<it.second<<" * ";
        ans = __gcd(ans,it.second);
    }
  //  cout<<endl;
    cout<<ans<<endl;
    return ;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
int t;
cin>>t;
while(t)
{
    t--;
    solve();
}
}


# include <bits/stdc++.h>
using namespace std;
const int MAXN = 500005;

//long long val[MAXN];
vector <int> g;
void solve()
{
 int i,j;
    int n,m;
    cin>>n>>m;
    vector <vector <int> > adj(n+2);
    vector <long long> val(n+3);
//for(i=1;i<=n;i++)adj[i].clear();
    //
     map <vector <int>, long long> mp;

    for(i=1;i<=n;i++)
        cin>>val[i];
        int u,v;
    for(i=1;i<=m;i++)
    {
        cin>>u>>v;
        adj[v].push_back(u);
    }
    for(i=1;i<=n;i++)
    {
        if(adj[i].size()==0)continue;
       g.clear();
        for(j=0;j<adj[i].size();j++)
        {
            g.push_back(adj[i][j]);
        }
        sort(g.begin(),g.end());
     //  for(j=0;j<g.size();j++)cout<<g[j]<<" & ";
      // cout<<endl;
        mp[g]+=val[i];
    }
    long long ans = 0;
    for(auto &it: mp)
    {
      //  cout<<it.second<<" * ";
        ans = __gcd(ans,it.second);
    }
  //  cout<<endl;
    cout<<ans<<'\n';
    return ;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
int t;
cin>>t;
while(t)
{
    t--;
    solve();
}
return 0;
}


# include <bits/stdc++.h>
using namespace std;
const int MAXN = 500005;

long long val[MAXN];

void solve()
{
 int i,j;
    int n,m;
    cin>>n>>m;
    vector <vector <int> > adj(n+2);
//for(i=1;i<=n;i++)adj[i].clear();
    //
     map <vector <int>, long long> mp;

    for(i=1;i<=n;i++)
        cin>>val[i];
        int u,v;
    for(i=1;i<=m;i++)
    {
        cin>>u>>v;
        adj[v].push_back(u);
    }
    for(i=1;i<=n;i++)
    {vector <int> g;
        if(adj[i].size()==0)continue;
       // g.clear();
        for(j=0;j<adj[i].size();j++)
        {
            g.push_back(adj[i][j]);
        }
        sort(g.begin(),g.end());
     //  for(j=0;j<g.size();j++)cout<<g[j]<<" & ";
      // cout<<endl;
        mp[g]+=val[i];
    }
    long long ans = 0;
    for(auto it: mp)
    {
      //  cout<<it.second<<" * ";
        ans = __gcd(ans,it.second);
    }
  //  cout<<endl;
    cout<<ans<<endl;
    return ;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
int t;
cin>>t;
while(t)
{
    t--;
    solve();
}
}


#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
//using namespace __gnu_pbds;
//typedef tree<
//        pair<long long, int>, // change type
//        null_type,
//        less<pair<long long, int> >, // change type
//        rb_tree_tag,
//        tree_order_statistics_node_update>
//        ordered_set;

//template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

typedef long long ll;
#define rep(i, start, end) for(int i = start; i < end; ++i)
#define sz(x) (int)(x).size()
#define pb push_back
#define st first
#define en second
#define all(x) x.begin(), x.end()
#define clr(d, v) memset(d, v, sizeof(d))
#define pii pair<int, int>
#define debug(x) cerr << #x << " : " << (x) << endl
const double PI = 3.14159265358979323846;
const double eps = (1e-10);
int isGreater(long double x, long double y)
{
    if (abs(x - y) < eps)
        return 0;
    if (x > y)
        return 1;
    return -1;
}

void solve()
{
    int n, m;
    cin >> n >> m;
    vector<ll> val(m);
    rep(i,0,n)
        cin >> val[i];
    vector<vector<int> > adj(n);
    vector<vector<int> > rAdj(n);
    vector<int> deg(n);
    rep(i,0,m)
    {
        int u, v;
        cin >> u >> v;
        --u; --v;
        adj[u].pb(v);
        rAdj[v].pb(u);
        deg[v]++;
    }
    ll g = 0;
    rep(u,0,n)
    {
        ll sumAll = 0, sumInd = 0;
        for (int v : adj[u])
        {
            sumAll += val[v];
            if (deg[v] == 1)
                sumInd += val[v];
        }
        g = __gcd(g, sumAll);
        g = __gcd(g, sumInd);
    }
    map<vector<int>, ll> mp;
    for (int v = 0; v < n; ++v)
    {
        vector<int> temp;
        for (int u : rAdj[v])
            temp.push_back(u);
        sort(all(temp));
        mp[temp] += val[v];
    }

    for (auto &item : mp)
        g = __gcd(g, item.second);

    cout << g << '\n';
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
//    freopen("AC.txt", "w", stdout);
#endif
    int tc;
    cin >> tc;
    while(tc--)
        solve();
    return 0;
}


#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
//using namespace __gnu_pbds;
//typedef tree<
//        pair<long long, int>, // change type
//        null_type,
//        less<pair<long long, int> >, // change type
//        rb_tree_tag,
//        tree_order_statistics_node_update>
//        ordered_set;

//template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

typedef long long ll;
#define rep(i, start, end) for(int i = start; i < end; ++i)
#define sz(x) (int)(x).size()
#define pb push_back
#define st first
#define en second
#define all(x) x.begin(), x.end()
#define clr(d, v) memset(d, v, sizeof(d))
#define pii pair<int, int>
#define debug(x) cerr << #x << " : " << (x) << endl
const double PI = 3.14159265358979323846;
const double eps = (1e-10);
int isGreater(long double x, long double y)
{
    if (abs(x - y) < eps)
        return 0;
    if (x > y)
        return 1;
    return -1;
}

void solve()
{
    int n, m;
    cin >> n >> m;
    vector<ll> val(m);
    rep(i,0,n)
        cin >> val[i];
    vector<vector<int> > adj(n);
    vector<int> deg(n);
    rep(i,0,m)
    {
        int u, v;
        cin >> u >> v;
        --u; --v;
        adj[u].pb(v);
        deg[v]++;
    }
    ll g = 0;
    rep(u,0,n)
    {
        ll sumAll = 0, sumInd = 0;
        for (int v : adj[u])
        {
            sumAll += val[v];
            if (deg[v] == 1)
                sumInd += val[v];
        }
        g = __gcd(g, sumAll);
        g = __gcd(g, sumInd);
    }
    cout << g << '\n';
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
//    freopen("AC.txt", "w", stdout);
#endif
    int tc;
    cin >> tc;
    while(tc--)
        solve();
    return 0;
}


#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
//using namespace __gnu_pbds;
//typedef tree<
//        pair<long long, int>, // change type
//        null_type,
//        less<pair<long long, int> >, // change type
//        rb_tree_tag,
//        tree_order_statistics_node_update>
//        ordered_set;

//template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

typedef long long ll;
#define rep(i, start, end) for(int i = start; i < end; ++i)
#define sz(x) (int)(x).size()
#define pb push_back
#define st first
#define en second
#define all(x) x.begin(), x.end()
#define clr(d, v) memset(d, v, sizeof(d))
#define pii pair<int, int>
#define debug(x) cerr << #x << " : " << (x) << endl
const double PI = 3.14159265358979323846;
const double eps = (1e-10);
int isGreater(long double x, long double y)
{
    if (abs(x - y) < eps)
        return 0;
    if (x > y)
        return 1;
    return -1;
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void solve()
{
    int n, m;
    cin >> n >> m;
    vector<ll> val(m);
    rep(i,0,n)
        cin >> val[i];
    vector<vector<int> > adj(n);
    vector<vector<int> > rAdj(n);
    vector<int> deg(n);
    rep(i,0,m)
    {
        int u, v;
        cin >> u >> v;
        --u; --v;
        adj[u].pb(v);
        rAdj[v].pb(u);
        deg[v]++;
    }
    ll g = 0;
    rep(u,0,n)
    {
        ll sumAll = 0, sumInd = 0;
        for (int v : adj[u])
        {
            sumAll += val[v];
            if (deg[v] == 1)
                sumInd += val[v];
        }
        g = __gcd(g, sumAll);
        g = __gcd(g, sumInd);
    }
//    map<vector<int>, ll> mp;
//    for (int v = 0; v < n; ++v)
//    {
//        vector<int> temp;
//        for (int u : rAdj[v])
//            temp.push_back(u);
//        sort(all(temp));
//        mp[temp] += val[v];
//    }
//
//    for (auto &item : mp)
//        g = __gcd(g, item.second);

    uniform_int_distribution<> uniform(0, n - 1);
    for (int it = 0; it < 30; ++it)
    {
        int i = uniform(rng), j = uniform(rng);
        vector<bool> inQ(n + 2);
        ll sum = 0;
        for (int v : adj[i])
        {
            if (!inQ[v])
            {
                sum += val[v];
                inQ[v] = true;
            }
        }
        for (int v : adj[j])
        {
            if (!inQ[v])
            {
                sum += val[v];
                inQ[v] = true;
            }
        }
        g = __gcd(g, sum);
    }

    cout << g << '\n';
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
//    freopen("AC.txt", "w", stdout);
#endif
    int tc;
    cin >> tc;
    while(tc--)
        solve();
    return 0;
}


#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
//using namespace __gnu_pbds;
//typedef tree<
//        pair<long long, int>, // change type
//        null_type,
//        less<pair<long long, int> >, // change type
//        rb_tree_tag,
//        tree_order_statistics_node_update>
//        ordered_set;

//template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

typedef long long ll;
#define rep(i, start, end) for(int i = start; i < end; ++i)
#define sz(x) (int)(x).size()
#define pb push_back
#define st first
#define en second
#define all(x) x.begin(), x.end()
#define clr(d, v) memset(d, v, sizeof(d))
#define pii pair<int, int>
#define debug(x) cerr << #x << " : " << (x) << endl
const double PI = 3.14159265358979323846;
const double eps = (1e-10);
int isGreater(long double x, long double y)
{
    if (abs(x - y) < eps)
        return 0;
    if (x > y)
        return 1;
    return -1;
}

void solve()
{
    int n, m;
    cin >> n >> m;
    vector<ll> val(m);
    rep(i,0,n)
        cin >> val[i];
    vector<vector<int> > adj(n);
    vector<vector<int> > rAdj(n);
    vector<int> deg(n);
    rep(i,0,m)
    {
        int u, v;
        cin >> u >> v;
        --u; --v;
        adj[u].pb(v);
        rAdj[v].pb(u);
        deg[v]++;
    }
    ll g = 0;
    rep(u,0,n)
    {
        ll sumAll = 0, sumInd = 0;
        for (int v : adj[u])
        {
            sumAll += val[v];
            if (deg[v] == 1)
                sumInd += val[v];
        }
        g = __gcd(g, sumAll);
        g = __gcd(g, sumInd);
    }
    map<vector<int>, ll> mp;
    for (int v = 0; v < n; ++v)
    {
        vector<int> temp;
        for (int u : rAdj[v])
            temp.push_back(u);
        sort(all(temp));
        mp[temp] += val[v];
    }

    for (auto &item : mp)
        g = __gcd(g, item.second);

    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    uniform_int_distribution<> uniform(0, n - 1);
    for (int it = 0; it < 30; ++it)
    {
        int i = uniform(rng), j = uniform(rng);
        vector<bool> inQ(n);
        ll sum = 0;
        for (int v : adj[i])
        {
            if (!inQ[v])
            {
                sum += val[v];
                inQ[v] = true;
            }
        }
        for (int v : adj[j])
        {
            if (!inQ[v])
            {
                sum += val[v];
                inQ[v] = true;
            }
        }
        g = __gcd(g, sum);
    }

    cout << g << '\n';
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
//    freopen("AC.txt", "w", stdout);
#endif
    int tc;
    cin >> tc;
    while(tc--)
        solve();
    return 0;
}


#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
//using namespace __gnu_pbds;
//typedef tree<
//        pair<long long, int>, // change type
//        null_type,
//        less<pair<long long, int> >, // change type
//        rb_tree_tag,
//        tree_order_statistics_node_update>
//        ordered_set;

//template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

typedef long long ll;
#define rep(i, start, end) for(int i = start; i < end; ++i)
#define sz(x) (int)(x).size()
#define pb push_back
#define st first
#define en second
#define all(x) x.begin(), x.end()
#define clr(d, v) memset(d, v, sizeof(d))
#define pii pair<int, int>
#define debug(x) cerr << #x << " : " << (x) << endl
const double PI = 3.14159265358979323846;
const double eps = (1e-10);
int isGreater(long double x, long double y)
{
    if (abs(x - y) < eps)
        return 0;
    if (x > y)
        return 1;
    return -1;
}

void solve()
{
    int n, m;
    cin >> n >> m;
    vector<ll> val(m);
    rep(i,0,n)
        cin >> val[i];
    vector<vector<int> > adj(n);
    vector<vector<int> > rAdj(n);
    vector<int> deg(n);
    rep(i,0,m)
    {
        int u, v;
        cin >> u >> v;
        --u; --v;
        adj[u].pb(v);
        rAdj[v].pb(u);
        deg[v]++;
    }
    ll g = 0;
//    rep(u,0,n)
//    {
//        ll sumAll = 0, sumInd = 0;
//        for (int v : adj[u])
//        {
//            sumAll += val[v];
//            if (deg[v] == 1)
//                sumInd += val[v];
//        }
//        g = __gcd(g, sumAll);
//        g = __gcd(g, sumInd);
//    }
    map<vector<int>, ll> mp;
    for (int v = 0; v < n; ++v)
    {
        vector<int> temp;
        for (int u : rAdj[v])
            temp.push_back(u);
        sort(all(temp));
        mp[temp] += val[v];
    }

    for (auto &item : mp)
        g = __gcd(g, item.second);

    cout << g << '\n';
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
//    freopen("AC.txt", "w", stdout);
#endif
    int tc;
    cin >> tc;
    while(tc--)
        solve();
    return 0;
}


#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
//using namespace __gnu_pbds;
//typedef tree<
//        pair<long long, int>, // change type
//        null_type,
//        less<pair<long long, int> >, // change type
//        rb_tree_tag,
//        tree_order_statistics_node_update>
//        ordered_set;

//template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

typedef long long ll;
#define rep(i, start, end) for(int i = start; i < end; ++i)
#define sz(x) (int)(x).size()
#define pb push_back
#define st first
#define en second
#define all(x) x.begin(), x.end()
#define clr(d, v) memset(d, v, sizeof(d))
#define pii pair<int, int>
#define debug(x) cerr << #x << " : " << (x) << endl
const double PI = 3.14159265358979323846;
const double eps = (1e-10);
int isGreater(long double x, long double y)
{
    if (abs(x - y) < eps)
        return 0;
    if (x > y)
        return 1;
    return -1;
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void solve()
{
    int n, m;
    cin >> n >> m;
    vector<ll> val(m);
    rep(i,0,n)
        cin >> val[i];
    vector<vector<int> > adj(n);
    vector<vector<int> > rAdj(n);
    vector<int> deg(n);
    rep(i,0,m)
    {
        int u, v;
        cin >> u >> v;
        --u; --v;
        adj[u].pb(v);
        rAdj[v].pb(u);
        deg[v]++;
    }
    ll g = 0;
    rep(u,0,n)
    {
        ll sumAll = 0, sumInd = 0;
        for (int v : adj[u])
        {
            sumAll += val[v];
            if (deg[v] == 1)
                sumInd += val[v];
        }
        g = __gcd(g, sumAll);
        g = __gcd(g, sumInd);
    }
    map<vector<int>, ll> mp;
    for (int v = 0; v < n; ++v)
    {
        vector<int> temp;
        for (int u : rAdj[v])
            temp.push_back(u);
        sort(all(temp));
        mp[temp] += val[v];
    }

    for (auto &item : mp)
        g = __gcd(g, item.second);

    uniform_int_distribution<> uniform(0, n - 1);
    for (int it = 0; it < 30; ++it)
    {
        int i = uniform(rng), j = uniform(rng);
        vector<bool> inQ(n + 2);
        ll sum = 0;
        for (int v : adj[i])
        {
            if (!inQ[v])
            {
                sum += val[v];
                inQ[v] = true;
            }
        }
        for (int v : adj[j])
        {
            if (!inQ[v])
            {
                sum += val[v];
                inQ[v] = true;
            }
        }
        g = __gcd(g, sum);
    }

    cout << g << '\n';
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
//    freopen("AC.txt", "w", stdout);
#endif
    int tc;
    cin >> tc;
    while(tc--)
        solve();
    return 0;
}


#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
//using namespace __gnu_pbds;
//typedef tree<
//        pair<long long, int>, // change type
//        null_type,
//        less<pair<long long, int> >, // change type
//        rb_tree_tag,
//        tree_order_statistics_node_update>
//        ordered_set;

//template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

typedef long long ll;
#define rep(i, start, end) for(int i = start; i < end; ++i)
#define sz(x) (int)(x).size()
#define pb push_back
#define st first
#define en second
#define all(x) x.begin(), x.end()
#define clr(d, v) memset(d, v, sizeof(d))
#define pii pair<int, int>
#define debug(x) cerr << #x << " : " << (x) << endl
const double PI = 3.14159265358979323846;
const double eps = (1e-10);
int isGreater(long double x, long double y)
{
    if (abs(x - y) < eps)
        return 0;
    if (x > y)
        return 1;
    return -1;
}

typedef unsigned long long ull;

ull mix(ull o){
    o+=0x9e3779b97f4a7c15;
    o=(o^(o>>30))*0xbf58476d1ce4e5b9;
    o=(o^(o>>27))*0x94d049bb133111eb;
    return o^(o>>31);
}

void solve()
{
    int n, m;
    cin >> n >> m;
    vector<ll> val(n);
    rep(i,0,n)
        cin >> val[i];
    vector<vector<int> > adj(n);
    vector<vector<int> > rAdj(n);
    rep(i,0,m)
    {
        int u, v;
        cin >> u >> v;
        --u; --v;
        adj[u].pb(v);
        rAdj[v].pb(u);
    }
    ll g = 0;
    map<unsigned long long, ll> mp;
    for (int v = 0; v < n; ++v)
    {
        ull h = 0;
        for (int u : rAdj[v])
            h += mix(u);
        if (h != 0)
            mp[h] += val[v];
    }

    for (auto &item : mp)
        g = __gcd(g, item.second);

    cout << g << '\n';
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
//    freopen("AC.txt", "w", stdout);
#endif
    int tc;
    cin >> tc;
    while(tc--)
        solve();
    return 0;
}