#include "bits/stdc++.h"
#define fio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define all(V) (V).begin(), (V).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int M = 5e5 + 5;
int cnt[M];
ll c[M];
vector <vector <int> > g;
int n, m;
ull try1() {
    ull gc = 0;
    for(int i = 0; i < n; i++) {
        ull sm = 0;
        for(int j : g[i]) {
            sm += c[j];
        }
        gc = __gcd(gc, sm);
    }
    return gc;
}
ull try2() {
    vector <int> P(n);
    for(int i = 0; i < n; i++) {
        P[i] = i;
        cnt[i] = 0;
    }
    random_shuffle(all(P));
    ull gc = 0;
    ull sm = 0;
    for(int ip = 0; ip < P.size(); ip++) {
        int u = P[ip];
        for(int v : g[u]) {
            if(cnt[v] == 0) {
                sm += c[v];
            }
            cnt[v]++;
        }
        gc = __gcd(gc, sm);
    }
    return gc;
}
ull try3() {
    for(int i = 0; i < n; i++) {
        cnt[i] = 0;
    }
    ull sm = 0;
    for(int i = 0; i < n; i++) {
        for(int u : g[i]) {
            if(cnt[u] == 0) sm += c[u];
            cnt[u]++;
        }
    }
    ull gc = 0;
    for(int i = 0; i < n; i++) {
        for(int u : g[i]) {
            cnt[u]--;
            if(cnt[u] == 0) {
                sm -= c[u];
            }
        }
        gc = __gcd(gc, sm);
        for(int u : g[i]) {
            if(cnt[u] == 0) {
                sm += c[u];
            }
            cnt[u]++;
        }
    }
    return gc;
}
ull try4() {
    vector <int> P(n);
    for(int i = 0; i < n; i++) {
        P[i] = i;
        cnt[i] = 0;
    }
    sort(all(P), [&](int i, int j){
        return g[i].size() < g[j].size();
    });
    ull gc = 0;
    ull sm = 0;
    for(int ip = 0; ip < P.size(); ip++) {
        int u = P[ip];
        for(int v : g[u]) {
            if(cnt[v] == 0) {
                sm += c[v];
            }
            cnt[v]++;
        }
        gc = __gcd(gc, sm);
    }
    return gc;
}
ull try5() {
    vector <int> P(n), content(n);
    for(int i = 0; i < n; i++) {
        for(int j : g[i]) {
            content[i] += c[j];
        }
    }
    for(int i = 0; i < n; i++) {
        P[i] = i;
        cnt[i] = 0;
    }
    sort(all(P), [&](int i, int j){
        return content[i] < content[j];
    });
    ull gc = 0;
    ull sm = 0;
    for(int ip = 0; ip < P.size(); ip++) {
        int u = P[ip];
        for(int v : g[u]) {
            if(cnt[v] == 0) {
                sm += c[v];
            }
            cnt[v]++;
        }
        gc = __gcd(gc, sm);
    }
    return gc;
}
ull try6() {
    vector <int> P(n), content(n);
    for(int i = 0; i < n; i++) {
        for(int j : g[i]) {
            content[i] += c[j];
        }
    }
    for(int i = 0; i < n; i++) {
        P[i] = i;
        cnt[i] = 0;
    }
    sort(all(P), [&](int i, int j){
        return content[i] > content[j];
    });
    ull gc = 0;
    ull sm = 0;
    for(int ip = 0; ip < P.size(); ip++) {
        int u = P[ip];
        for(int v : g[u]) {
            if(cnt[v] == 0) {
                sm += c[v];
            }
            cnt[v]++;
        }
        gc = __gcd(gc, sm);
    }
    return gc;
}
int fib(int n)
{
	if(n <= 2) {
		return 1;
	}
    return fib(n - 1) + fib(n - 2);
}

int main() {fio;
    int T = fib(44);
    cin >> T;
    while(T--) {
        g.clear();
        cin >> n >> m;
        g.resize(n);
        for(int i = 0; i < n; i++) {
            cnt[i] = 0;
        }
        ull h = 0;
        for(int i = 0; i < n; i++) {
            cin >> c[i];
            h = __gcd(h, (ull)c[i]);
        }
        for(int i = 0; i < m; i++) {
            int u, v;
            cin >> u >> v;
            u--; v--;
            g[u].push_back(v);
        }
        ull gc = try1();
        gc = __gcd(gc, try3());
        gc = __gcd(gc, try4());
        gc = __gcd(gc, try5());
        gc = __gcd(gc, try6());
        cout << gc << '\n';
    }

    return 0;
}


#include "bits/stdc++.h"
#define fio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define all(V) (V).begin(), (V).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int M = 5e5 + 5;
int cnt[M];
ll c[M];
vector <vector <int> > g;
int n, m;
ull try1() {
    ull gc = 0;
    for(int i = 0; i < n; i++) {
        ull sm = 0;
        for(int j : g[i]) {
            sm += c[j];
        }
        gc = __gcd(gc, sm);
    }
    return gc;
}
ull try2() {
    vector <int> P(n);
    for(int i = 0; i < n; i++) {
        P[i] = i;
        cnt[i] = 0;
    }
    random_shuffle(all(P));
    ull gc = 0;
    ull sm = 0;
    for(int ip = 0; ip < P.size(); ip++) {
        int u = P[ip];
        for(int v : g[u]) {
            if(cnt[v] == 0) {
                sm += c[v];
            }
            cnt[v]++;
        }
        gc = __gcd(gc, sm);
    }
    return gc;
}
ull try3() {
    for(int i = 0; i < n; i++) {
        cnt[i] = 0;
    }
    ull sm = 0;
    for(int i = 0; i < n; i++) {
        for(int u : g[i]) {
            if(cnt[u] == 0) sm += c[u];
            cnt[u]++;
        }
    }
    ull gc = 0;
    for(int i = 0; i < n; i++) {
        for(int u : g[i]) {
            cnt[u]--;
            if(cnt[u] == 0) {
                sm -= c[u];
            }
        }
        gc = __gcd(gc, sm);
        for(int u : g[i]) {
            if(cnt[u] == 0) {
                sm += c[u];
            }
            cnt[u]++;
        }
    }
    return gc;
}
ull try4() {
    vector <int> P(n);
    for(int i = 0; i < n; i++) {
        P[i] = i;
        cnt[i] = 0;
    }
    sort(all(P), [&](int i, int j){
        return g[i].size() < g[j].size();
    });
    ull gc = 0;
    ull sm = 0;
    for(int ip = 0; ip < P.size(); ip++) {
        int u = P[ip];
        for(int v : g[u]) {
            if(cnt[v] == 0) {
                sm += c[v];
            }
            cnt[v]++;
        }
        gc = __gcd(gc, sm);
    }
    return gc;
}
ull try5() {
    vector <int> P(n), content(n);
    for(int i = 0; i < n; i++) {
        for(int j : g[i]) {
            content[i] += c[j];
        }
    }
    for(int i = 0; i < n; i++) {
        P[i] = i;
        cnt[i] = 0;
    }
    sort(all(P), [&](int i, int j){
        return content[i] < content[j];
    });
    ull gc = 0;
    ull sm = 0;
    for(int ip = 0; ip < P.size(); ip++) {
        int u = P[ip];
        for(int v : g[u]) {
            if(cnt[v] == 0) {
                sm += c[v];
            }
            cnt[v]++;
        }
        gc = __gcd(gc, sm);
    }
    return gc;
}
ull try6() {
    vector <int> P(n), content(n);
    for(int i = 0; i < n; i++) {
        for(int j : g[i]) {
            content[i] += c[j];
        }
    }
    for(int i = 0; i < n; i++) {
        P[i] = i;
        cnt[i] = 0;
    }
    sort(all(P), [&](int i, int j){
        return content[i] > content[j];
    });
    ull gc = 0;
    ull sm = 0;
    for(int ip = 0; ip < P.size(); ip++) {
        int u = P[ip];
        for(int v : g[u]) {
            if(cnt[v] == 0) {
                sm += c[v];
            }
            cnt[v]++;
        }
        gc = __gcd(gc, sm);
    }
    return gc;
}

int main() {fio;
    int T;
    cin >> T;
    while(T--) {
        g.clear();
        cin >> n >> m;
        g.resize(n);
        for(int i = 0; i < n; i++) {
            cnt[i] = 0;
        }
        ull h = 0;
        for(int i = 0; i < n; i++) {
            cin >> c[i];
            h = __gcd(h, (ull)c[i]);
        }
        for(int i = 0; i < m; i++) {
            int u, v;
            cin >> u >> v;
            u--; v--;
            g[u].push_back(v);
        }
        ull gc = try1();
        for(int iter = 0; iter < 10; iter++) {
            gc = __gcd(gc, try2());
            if(gc == h) break;
        }
        gc = __gcd(gc, try3());
        gc = __gcd(gc, try4());
        gc = __gcd(gc, try5());
        gc = __gcd(gc, try6());
        cout << gc << '\n';
    }

    return 0;
}


#include "bits/stdc++.h"
#define fio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define all(V) (V).begin(), (V).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int M = 5e5 + 5;
int cnt[M];
ll c[M];
vector <vector <int> > g;
int n, m;
ull try1() {
    ull gc = 0;
    for(int i = 0; i < n; i++) {
        ull sm = 0;
        for(int j : g[i]) {
            sm += c[j];
        }
        gc = __gcd(gc, sm);
    }
    return gc;
}
ull try2() {
    vector <int> P(n);
    for(int i = 0; i < n; i++) {
        P[i] = i;
        cnt[i] = 0;
    }
    random_shuffle(all(P));
    ull gc = 0;
    ull sm = 0;
    for(int ip = 0; ip < P.size(); ip++) {
        int u = P[ip];
        for(int v : g[u]) {
            if(cnt[v] == 0) {
                sm += c[v];
            }
            cnt[v]++;
        }
        gc = __gcd(gc, sm);
    }
    return gc;
}
ull try3() {
    for(int i = 0; i < n; i++) {
        cnt[i] = 0;
    }
    ull sm = 0;
    for(int i = 0; i < n; i++) {
        for(int u : g[i]) {
            if(cnt[u] == 0) sm += c[u];
            cnt[u]++;
        }
    }
    ull gc = 0;
    for(int i = 0; i < n; i++) {
        for(int u : g[i]) {
            cnt[u]--;
            if(cnt[u] == 0) {
                sm -= c[u];
            }
        }
        gc = __gcd(gc, sm);
        for(int u : g[i]) {
            if(cnt[u] == 0) {
                sm += c[u];
            }
            cnt[u]++;
        }
    }
    return gc;
}
ull try4() {
    vector <int> P(n);
    for(int i = 0; i < n; i++) {
        P[i] = i;
        cnt[i] = 0;
    }
    sort(all(P), [&](int i, int j){
        return g[i].size() < g[j].size();
    });
    ull gc = 0;
    ull sm = 0;
    for(int ip = 0; ip < P.size(); ip++) {
        int u = P[ip];
        for(int v : g[u]) {
            if(cnt[v] == 0) {
                sm += c[v];
            }
            cnt[v]++;
        }
        gc = __gcd(gc, sm);
    }
    return gc;
}
ull try5() {
    vector <int> P(n), content(n);
    for(int i = 0; i < n; i++) {
        for(int j : g[i]) {
            content[i] += c[j];
        }
    }
    for(int i = 0; i < n; i++) {
        P[i] = i;
        cnt[i] = 0;
    }
    sort(all(P), [&](int i, int j){
        return content[i] < content[j];
    });
    ull gc = 0;
    ull sm = 0;
    for(int ip = 0; ip < P.size(); ip++) {
        int u = P[ip];
        for(int v : g[u]) {
            if(cnt[v] == 0) {
                sm += c[v];
            }
            cnt[v]++;
        }
        gc = __gcd(gc, sm);
    }
    return gc;
}
ull try6() {
    vector <int> P(n), content(n);
    for(int i = 0; i < n; i++) {
        for(int j : g[i]) {
            content[i] += c[j];
        }
    }
    for(int i = 0; i < n; i++) {
        P[i] = i;
        cnt[i] = 0;
    }
    sort(all(P), [&](int i, int j){
        return content[i] > content[j];
    });
    ull gc = 0;
    ull sm = 0;
    for(int ip = 0; ip < P.size(); ip++) {
        int u = P[ip];
        for(int v : g[u]) {
            if(cnt[v] == 0) {
                sm += c[v];
            }
            cnt[v]++;
        }
        gc = __gcd(gc, sm);
    }
    return gc;
}

int main() {fio;
    int T;
    cin >> T;
    while(T--) {
        g.clear();
        cin >> n >> m;
        g.resize(n);
        for(int i = 0; i < n; i++) {
            cnt[i] = 0;
        }
        ull h = 0;
        for(int i = 0; i < n; i++) {
            cin >> c[i];
            h = __gcd(h, (ull)c[i]);
        }
        for(int i = 0; i < m; i++) {
            int u, v;
            cin >> u >> v;
            u--; v--;
            g[u].push_back(v);
        }
        ull gc = try1();
        for(int iter = 0; iter < 5; iter++) {
            gc = __gcd(gc, try2());
            if(gc == h) break;
        }
        gc = __gcd(gc, try3());
        gc = __gcd(gc, try4());
        gc = __gcd(gc, try5());
        gc = __gcd(gc, try6());
        cout << gc << '\n';
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define pb                    push_back
#define ll                    long long
#define ld                    long double
#define sz(x)                 (ll)x.size()
#define vll                   vector<long long>

#define infi                  INT_MAX
#define infl                  LONG_LONG_MAX
#define infd                  LDBL_MAX
#define f(i,a,b)              for(ll i=a;i<b;i++)
#define fi(i,a,b)             for(ll i=(b-1);i>=a;i--)
#define F                     first
#define S                     second
#define G(a,b)                get<a>(b)
#define MP                    make_pair
#define MT                    make_tuple
#define pll                   pair<ll,ll>
#define endl                  "\n"
#define ALL(v)                v.begin(),v.end()
#define nl                    cout<<"\n";
#define pr(x)                 cout<<x;
#define pr1(x)                cout<<x<<" ";
#define pr2(x,y)              cout<<x<<" "<<y;
#define pr3(x,y,z)            cout<<x<<" "<<y<<" "<<z;
#define pr4(x,y,z,w)          cout<<x<<" "<<y<<" "<<z<<" "<<w;
#define deb                   cout<<"***************************************************************************************\n";
#define moshi                 cout<<"moshi moshi ! \n";
#define hi                    cout<<"hi ! \n";
#define bye                   cout<<"bye bye ! \n";
#define kawai                 cout<<"O kawai koto ! \n";
#define o_set                 tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define number_of_set_bits  __builtin_popcountll
#define eb                    emplace_back
const ld PI=3.1415926535897932384626433;

auto clk=clock();
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cout << name << " : " << arg1 <<"\n";
        //use cerr if u want to display at the bottom
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
#else
#define trace(...)
#endif

//**********************************************************  Code begins  ***********************************************

ll gcd(ll a,ll b)
{
	if(b==0) return a;
	return gcd(b,a%b);
}

const ll N=5e5+5;
ll n,m,arr[N],x[N],p=1e9+7,y[N];

void solve()
{
  map<ll,ll>mp;
  cin>>n>>m;
  f(i,1,n+1) cin>>arr[i];
  f(i,1,n+1)
  {
  	x[i]=rng();
  	if(x[i]<0) x[i]*=(-1);
  	x[i]%=p;
  }
  f(i,1,m+1)
  {
  	ll u,v;
  	cin>>u>>v;
  	y[v]+=x[u];
  }
  f(i,1,n+1) if(y[i]>0) mp[y[i]]+=arr[i];
  ll g=0;
  for(auto &i:mp) g=gcd(g,i.S);
  cout<<g<<"\n";
  f(i,1,n+1) adj[i].clear();
}

int main()
{
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    cout<<fixed<<setprecision(8);

    ll test_cases=1;
    cin>>test_cases;

    f(i,1,test_cases+1)  solve();

    #ifdef rd
    cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
    #endif

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define pb                    push_back
#define ll                    long long
#define ld                    long double
#define sz(x)                 (ll)x.size()
#define vll                   vector<long long>

#define infi                  INT_MAX
#define infl                  LONG_LONG_MAX
#define infd                  LDBL_MAX
#define f(i,a,b)              for(ll i=a;i<b;i++)
#define fi(i,a,b)             for(ll i=(b-1);i>=a;i--)
#define F                     first
#define S                     second
#define G(a,b)                get<a>(b)
#define MP                    make_pair
#define MT                    make_tuple
#define pll                   pair<ll,ll>
#define endl                  "\n"
#define ALL(v)                v.begin(),v.end()
#define nl                    cout<<"\n";
#define pr(x)                 cout<<x;
#define pr1(x)                cout<<x<<" ";
#define pr2(x,y)              cout<<x<<" "<<y;
#define pr3(x,y,z)            cout<<x<<" "<<y<<" "<<z;
#define pr4(x,y,z,w)          cout<<x<<" "<<y<<" "<<z<<" "<<w;
#define deb                   cout<<"***************************************************************************************\n";
#define moshi                 cout<<"moshi moshi ! \n";
#define hi                    cout<<"hi ! \n";
#define bye                   cout<<"bye bye ! \n";
#define kawai                 cout<<"O kawai koto ! \n";
#define o_set                 tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define number_of_set_bits  __builtin_popcountll
#define eb                    emplace_back
const ld PI=3.1415926535897932384626433;

auto clk=clock();
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cout << name << " : " << arg1 <<"\n";
        //use cerr if u want to display at the bottom
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
#else
#define trace(...)
#endif

//**********************************************************  Code begins  ***********************************************

ll gcd(ll a,ll b)
{
	if(b==0) return a;
	return gcd(b,a%b);
}

const ll N=5e5+5;
ll n,m,arr[N],x[N],p=1e9+7,y[N],cnt[N];

void solve()
{
  map<pll,ll>mp;
  cin>>n>>m;
  f(i,1,n+1) cin>>arr[i];
  f(i,1,n+1)
  {
  	x[i]=rng();
  	if(x[i]<0) x[i]*=(-1);
  	x[i]%=p;
  }
  f(i,1,n+1) y[i]=1;
  f(i,1,m+1)
  {
  	ll u,v;
  	cin>>u>>v;
  	y[v]*=x[u];
  	y[v]%=p;
  	cnt[v]++;
  }
  f(i,1,n+1) if(cnt[i]) mp[MP(y[i],cnt[i])]+=arr[i];
  ll g=0;
  for(auto &i:mp) g=gcd(g,i.S);
  cout<<g<<"\n";
}

int main()
{
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    cout<<fixed<<setprecision(8);

    ll test_cases=1;
    cin>>test_cases;

    f(i,1,test_cases+1)  solve();

    #ifdef rd
    cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
    #endif

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define pb                    push_back
#define ll                    long long
#define ld                    long double
#define sz(x)                 (ll)x.size()
#define vll                   vector<long long>

#define infi                  INT_MAX
#define infl                  LONG_LONG_MAX
#define infd                  LDBL_MAX
#define f(i,a,b)              for(ll i=a;i<b;i++)
#define fi(i,a,b)             for(ll i=(b-1);i>=a;i--)
#define F                     first
#define S                     second
#define G(a,b)                get<a>(b)
#define MP                    make_pair
#define MT                    make_tuple
#define pll                   pair<ll,ll>
#define endl                  "\n"
#define ALL(v)                v.begin(),v.end()
#define nl                    cout<<"\n";
#define pr(x)                 cout<<x;
#define pr1(x)                cout<<x<<" ";
#define pr2(x,y)              cout<<x<<" "<<y;
#define pr3(x,y,z)            cout<<x<<" "<<y<<" "<<z;
#define pr4(x,y,z,w)          cout<<x<<" "<<y<<" "<<z<<" "<<w;
#define deb                   cout<<"***************************************************************************************\n";
#define moshi                 cout<<"moshi moshi ! \n";
#define hi                    cout<<"hi ! \n";
#define bye                   cout<<"bye bye ! \n";
#define kawai                 cout<<"O kawai koto ! \n";
#define o_set                 tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define number_of_set_bits  __builtin_popcountll
#define eb                    emplace_back
const ld PI=3.1415926535897932384626433;

auto clk=clock();
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cout << name << " : " << arg1 <<"\n";
        //use cerr if u want to display at the bottom
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
#else
#define trace(...)
#endif

//**********************************************************  Code begins  ***********************************************

ll gcd(ll a,ll b)
{
	if(b==0) return a;
	return gcd(b,a%b);
}

const ll N=5e5+5;
ll n,m,arr[N],x[N],p=1e12+7,y[N],cnt[N];

void solve()
{
  map<pll,ll>mp;
  cin>>n>>m;
  f(i,1,n+1) cin>>arr[i];
  f(i,1,n+1)
  {
  	x[i]=rng();
  	if(x[i]<0) x[i]*=(-1);
  	x[i]%=p;
  }
  f(i,1,m+1)
  {
  	ll u,v;
  	cin>>u>>v;
  	y[v]+=x[u];
  	cnt[v]++;
  }
  f(i,1,n+1) if(cnt[i]) mp[MP(y[i],cnt[i])]+=arr[i];
  ll g=0;
  for(auto &i:mp) g=gcd(g,i.S);
  cout<<g<<"\n";
}

int main()
{
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    cout<<fixed<<setprecision(8);

    ll test_cases=1;
    cin>>test_cases;

    f(i,1,test_cases+1)  solve();

    #ifdef rd
    cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
    #endif

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define pb                    push_back
#define ll                    long long
#define ld                    long double
#define sz(x)                 (ll)x.size()
#define vll                   vector<long long>

#define infi                  INT_MAX
#define infl                  LONG_LONG_MAX
#define infd                  LDBL_MAX
#define f(i,a,b)              for(ll i=a;i<b;i++)
#define fi(i,a,b)             for(ll i=(b-1);i>=a;i--)
#define F                     first
#define S                     second
#define G(a,b)                get<a>(b)
#define MP                    make_pair
#define MT                    make_tuple
#define pll                   pair<ll,ll>
#define endl                  "\n"
#define ALL(v)                v.begin(),v.end()
#define nl                    cout<<"\n";
#define pr(x)                 cout<<x;
#define pr1(x)                cout<<x<<" ";
#define pr2(x,y)              cout<<x<<" "<<y;
#define pr3(x,y,z)            cout<<x<<" "<<y<<" "<<z;
#define pr4(x,y,z,w)          cout<<x<<" "<<y<<" "<<z<<" "<<w;
#define deb                   cout<<"***************************************************************************************\n";
#define moshi                 cout<<"moshi moshi ! \n";
#define hi                    cout<<"hi ! \n";
#define bye                   cout<<"bye bye ! \n";
#define kawai                 cout<<"O kawai koto ! \n";
#define o_set                 tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define number_of_set_bits  __builtin_popcountll
#define eb                    emplace_back
const ld PI=3.1415926535897932384626433;

auto clk=clock();
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cout << name << " : " << arg1 <<"\n";
        //use cerr if u want to display at the bottom
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
#else
#define trace(...)
#endif

//**********************************************************  Code begins  ***********************************************

ll gcd(ll a,ll b)
{
	if(b==0) return a;
	return gcd(b,a%b);
}

const ll N=5e5+5;
ll n,m,arr[N];
vll adj[N];

void solve()
{
  map<vll,ll>mp;
  cin>>n>>m;
  f(i,1,n+1) cin>>arr[i];
  f(i,1,m+1)
  {
  	ll u,v;
  	cin>>u>>v;
  	adj[v].pb(u);
  }
  f(i,1,n+1) sort(ALL(adj[i]));
  f(i,1,n+1) if(sz(adj[i])) mp[adj[i]]+=arr[i];
  ll g=0;
  for(auto &i:mp) g=gcd(g,i.S);
  cout<<g<<"\n";
  f(i,1,n+1) adj[i].clear();
}

int main()
{
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    cout<<fixed<<setprecision(8);

    ll test_cases=1;
    cin>>test_cases;

    f(i,1,test_cases+1)  solve();

    #ifdef rd
    cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
    #endif

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define pb                    push_back
#define ll                    long long
#define ld                    long double
#define sz(x)                 (ll)x.size()
#define vll                   vector<long long>

#define infi                  INT_MAX
#define infl                  LONG_LONG_MAX
#define infd                  LDBL_MAX
#define f(i,a,b)              for(ll i=a;i<b;i++)
#define fi(i,a,b)             for(ll i=(b-1);i>=a;i--)
#define F                     first
#define S                     second
#define G(a,b)                get<a>(b)
#define MP                    make_pair
#define MT                    make_tuple
#define pll                   pair<ll,ll>
#define endl                  "\n"
#define ALL(v)                v.begin(),v.end()
#define nl                    cout<<"\n";
#define pr(x)                 cout<<x;
#define pr1(x)                cout<<x<<" ";
#define pr2(x,y)              cout<<x<<" "<<y;
#define pr3(x,y,z)            cout<<x<<" "<<y<<" "<<z;
#define pr4(x,y,z,w)          cout<<x<<" "<<y<<" "<<z<<" "<<w;
#define deb                   cout<<"***************************************************************************************\n";
#define moshi                 cout<<"moshi moshi ! \n";
#define hi                    cout<<"hi ! \n";
#define bye                   cout<<"bye bye ! \n";
#define kawai                 cout<<"O kawai koto ! \n";
#define o_set                 tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define number_of_set_bits  __builtin_popcountll
#define eb                    emplace_back
const ld PI=3.1415926535897932384626433;

auto clk=clock();
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cout << name << " : " << arg1 <<"\n";
        //use cerr if u want to display at the bottom
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
#else
#define trace(...)
#endif

//**********************************************************  Code begins  ***********************************************

ll gcd(ll a,ll b)
{
	if(b==0) return a;
	return gcd(b,a%b);
}

const ll N=5e5+5;
ll n,m,arr[N],x[N],p=1e9+7,y[N],cnt[N];

void solve()
{
  map<pll,ll>mp;
  cin>>n>>m;
  f(i,1,n+1) cin>>arr[i];
  f(i,1,n+1)
  {
  	x[i]=rng();
  	if(x[i]<0) x[i]*=(-1);
  	x[i]%=p;
  }
  f(i,1,m+1)
  {
  	ll u,v;
  	cin>>u>>v;
  	y[v]+=x[u];
  	cnt[v]++;
  }
  f(i,1,n+1) if(cnt[i]) mp[MP(y[i],cnt[i])]+=arr[i];
  ll g=0;
  for(auto &i:mp) g=gcd(g,i.S);
  cout<<g<<"\n";
}

int main()
{
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    cout<<fixed<<setprecision(8);

    ll test_cases=1;
    cin>>test_cases;

    f(i,1,test_cases+1)  solve();

    #ifdef rd
    cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
    #endif

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define pb                    push_back
#define ll                    long long
#define ld                    long double
#define sz(x)                 (ll)x.size()
#define vll                   vector<long long>

#define infi                  INT_MAX
#define infl                  LONG_LONG_MAX
#define infd                  LDBL_MAX
#define f(i,a,b)              for(ll i=a;i<b;i++)
#define fi(i,a,b)             for(ll i=(b-1);i>=a;i--)
#define F                     first
#define S                     second
#define G(a,b)                get<a>(b)
#define MP                    make_pair
#define MT                    make_tuple
#define pll                   pair<ll,ll>
#define endl                  "\n"
#define ALL(v)                v.begin(),v.end()
#define nl                    cout<<"\n";
#define pr(x)                 cout<<x;
#define pr1(x)                cout<<x<<" ";
#define pr2(x,y)              cout<<x<<" "<<y;
#define pr3(x,y,z)            cout<<x<<" "<<y<<" "<<z;
#define pr4(x,y,z,w)          cout<<x<<" "<<y<<" "<<z<<" "<<w;
#define deb                   cout<<"***************************************************************************************\n";
#define moshi                 cout<<"moshi moshi ! \n";
#define hi                    cout<<"hi ! \n";
#define bye                   cout<<"bye bye ! \n";
#define kawai                 cout<<"O kawai koto ! \n";
#define o_set                 tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define number_of_set_bits  __builtin_popcountll
#define eb                    emplace_back
const ld PI=3.1415926535897932384626433;

auto clk=clock();
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cout << name << " : " << arg1 <<"\n";
        //use cerr if u want to display at the bottom
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
#else
#define trace(...)
#endif

//**********************************************************  Code begins  ***********************************************

ll gcd(ll a,ll b)
{
	if(b==0) return a;
	return gcd(b,a%b);
}

const ll N=5e5+5;
ll n,m,arr[N],x[N],p=1e9+7,y[N];

void solve()
{
  map<ll,ll>mp;
  cin>>n>>m;
  f(i,1,n+1) cin>>arr[i];
  f(i,1,n+1)
  {
  	x[i]=rng();
  	if(x[i]<0) x[i]*=(-1);
  	x[i]%=p;
  }
  f(i,1,m+1)
  {
  	ll u,v;
  	cin>>u>>v;
  	y[v]+=x[u];
  }
  f(i,1,n+1) if(y[i]>0) mp[y[i]]+=arr[i];
  ll g=0;
  for(auto &i:mp) g=gcd(g,i.S);
  cout<<g<<"\n";
}

int main()
{
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    cout<<fixed<<setprecision(8);

    ll test_cases=1;
    cin>>test_cases;

    f(i,1,test_cases+1)  solve();

    #ifdef rd
    cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
    #endif

    return 0;
}


/*
Author : N_o_o_B
Created : April 30 2020 22:20:41
*/
#include <bits/stdc++.h>
using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
*/
#define TRACE
#ifdef TRACE
#define trace(...) { cerr<<"[ "; __trace__(#__VA_ARGS__, __VA_ARGS__);}
#undef endl
template <typename Arg1,typename Arg2>
ostream& operator << (ostream& out, const pair<Arg1,Arg2> &x) {
    return out<<"("<<x.first<<","<<x.second<<")";
}

template <typename Arg1>
ostream& operator << (ostream& out, const vector<Arg1> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1>
ostream& operator << (ostream& out, const set<Arg1> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1,typename Arg2>
ostream& operator << (ostream& out, const map<Arg1,Arg2> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1>
void __trace__(const string name, Arg1&& arg1){
    cerr << name << " : " << arg1 << " ] " << endl;
}

template <typename Arg1, typename... Args>
void __trace__(const string names, Arg1&& arg1, Args&&... args){
    const string name = names.substr(0,names.find(','));
    cerr<<name<<" : "<<arg1<<" | ";
    __trace__(names.substr(1+(int)name.size()), args...);
}
#else
    #define trace(args...)
#endif

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pair<int,int>> vii;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<pair<ll,ll>> vll;
typedef vector<vl> vvl;
//typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> oset;
#define fori(i,n) for(int i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repd(i,a,b) for(int i=a;i>=b;i--)
#define ford(i,n)  for(int i=n-1;i>=0;i--)
#define trav(x,a) for(auto& x:a)
#define all(x) x.begin(),x.end()
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define fi first
#define se second
clock_t time_p=clock();
void time_taken()
{
    time_p=clock()-time_p;
    cerr<<"Time Taken : "<<(float)(time_p)/CLOCKS_PER_SEC<<"\n";
}
//const ll mod=998244353;
const ll mod=1e9+7;
const ll INF=1e18;
// https://codeforces.com/blog/entry/74148?#comment-586706
typedef unsigned long long ull;
ull mix(ull o)
{
    // static ull foo = chrono::steady_clock::now().time_since_epoch().count();;
    // o ^= foo;
    o += 0x9e3779b97f4a7c15;
    o = (o ^ (o >> 30)) * 0xbf58476d1ce4e5b9;
    o = (o ^ (o >> 27)) * 0x94d049bb133111eb;
    return o ^ (o >> 31);
    //Those constants supposedly are chosen to give this function better pseudo-random properties, but on any on-site contest, when one can't have team reference document/doesn't want to waste time searching it for implementation typing arbitrary large odd numbers by hand should be good enough
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    cout.precision(12);cout << fixed;

    int t;
    cin >> t;
    while(t--){
        int n,m;
        cin >> n >> m;
        ll c[n];
        fori(i,n) cin >> c[i];
        vector<ull> a(n);
        fori(_,m){
            int u,v;
            cin >> u >> v;
            --u;--v;
            a[v] ^= mix(u);
        }
        map<ull,ll> ma;
        fori(i,n){
            ma[a[i]] += c[i];
        }
        ll ans = 0;
        for(auto x:ma){
           if(x.fi) ans = __gcd(ans,x.se);
        }
        cout << ans << endl;
    }

    time_taken();
    return 0;
}


/*
Author : N_o_o_B
Created : April 30 2020 22:20:41
*/
#include <bits/stdc++.h>
using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
*/
#define TRACE
#ifdef TRACE
#define trace(...) { cerr<<"[ "; __trace__(#__VA_ARGS__, __VA_ARGS__);}
#undef endl
template <typename Arg1,typename Arg2>
ostream& operator << (ostream& out, const pair<Arg1,Arg2> &x) {
    return out<<"("<<x.first<<","<<x.second<<")";
}

template <typename Arg1>
ostream& operator << (ostream& out, const vector<Arg1> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1>
ostream& operator << (ostream& out, const set<Arg1> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1,typename Arg2>
ostream& operator << (ostream& out, const map<Arg1,Arg2> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1>
void __trace__(const string name, Arg1&& arg1){
    cerr << name << " : " << arg1 << " ] " << endl;
}

template <typename Arg1, typename... Args>
void __trace__(const string names, Arg1&& arg1, Args&&... args){
    const string name = names.substr(0,names.find(','));
    cerr<<name<<" : "<<arg1<<" | ";
    __trace__(names.substr(1+(int)name.size()), args...);
}
#else
    #define trace(args...)
#endif

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pair<int,int>> vii;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<pair<ll,ll>> vll;
typedef vector<vl> vvl;
//typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> oset;
#define fori(i,n) for(int i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repd(i,a,b) for(int i=a;i>=b;i--)
#define ford(i,n)  for(int i=n-1;i>=0;i--)
#define trav(x,a) for(auto& x:a)
#define all(x) x.begin(),x.end()
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define fi first
#define se second
clock_t time_p=clock();
void time_taken()
{
    time_p=clock()-time_p;
    cerr<<"Time Taken : "<<(float)(time_p)/CLOCKS_PER_SEC<<"\n";
}
//const ll mod=998244353;
const ll mod=1e9+7;
const ll INF=1e18;
// https://codeforces.com/blog/entry/74148?#comment-586706
typedef unsigned long long ull;
ull mix(ull o)
{
    static ull foo = chrono::steady_clock::now().time_since_epoch().count();;
    o ^= foo;
    o += 0x9e3779b97f4a7c15;
    o = (o ^ (o >> 30)) * 0xbf58476d1ce4e5b9;
    o = (o ^ (o >> 27)) * 0x94d049bb133111eb;
    return o ^ (o >> 31);
    //Those constants supposedly are chosen to give this function better pseudo-random properties, but on any on-site contest, when one can't have team reference document/doesn't want to waste time searching it for implementation typing arbitrary large odd numbers by hand should be good enough
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    cout.precision(12);cout << fixed;

    int t;
    cin >> t;
    while(t--){
        int n,m;
        cin >> n >> m;
        ll c[n];
        fori(i,n) cin >> c[i];
        vector<ull> a(n);
        fori(_,m){
            int u,v;
            cin >> u >> v;
            --u;--v;
            a[v] += mix(u);
        }
        map<ull,ll> ma;
        fori(i,n){
            ma[a[i]] += c[i];
        }
        ll ans = 0;
        for(auto x:ma){
           if(x.fi) ans = __gcd(ans,x.se);
        }
        cout << ans << endl;
    }

    time_taken();
    return 0;
}


/*
Author : N_o_o_B
Created : April 30 2020 22:20:41
*/
#include <bits/stdc++.h>
using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
*/
#define TRACE
#ifdef TRACE
#define trace(...) { cerr<<"[ "; __trace__(#__VA_ARGS__, __VA_ARGS__);}
#undef endl
template <typename Arg1,typename Arg2>
ostream& operator << (ostream& out, const pair<Arg1,Arg2> &x) {
    return out<<"("<<x.first<<","<<x.second<<")";
}

template <typename Arg1>
ostream& operator << (ostream& out, const vector<Arg1> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1>
ostream& operator << (ostream& out, const set<Arg1> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1,typename Arg2>
ostream& operator << (ostream& out, const map<Arg1,Arg2> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1>
void __trace__(const string name, Arg1&& arg1){
    cerr << name << " : " << arg1 << " ] " << endl;
}

template <typename Arg1, typename... Args>
void __trace__(const string names, Arg1&& arg1, Args&&... args){
    const string name = names.substr(0,names.find(','));
    cerr<<name<<" : "<<arg1<<" | ";
    __trace__(names.substr(1+(int)name.size()), args...);
}
#else
    #define trace(args...)
#endif

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pair<int,int>> vii;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<pair<ll,ll>> vll;
typedef vector<vl> vvl;
//typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> oset;
#define fori(i,n) for(int i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repd(i,a,b) for(int i=a;i>=b;i--)
#define ford(i,n)  for(int i=n-1;i>=0;i--)
#define trav(x,a) for(auto& x:a)
#define all(x) x.begin(),x.end()
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define fi first
#define se second
clock_t time_p=clock();
void time_taken()
{
    time_p=clock()-time_p;
    cerr<<"Time Taken : "<<(float)(time_p)/CLOCKS_PER_SEC<<"\n";
}
//const ll mod=998244353;
const ll mod=1e9+7;
const ll INF=1e18;
// https://codeforces.com/blog/entry/74148?#comment-586706
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);
    //Those constants supposedly are chosen to give this function better pseudo-random properties, but on any on-site contest, when one can't have team reference document/doesn't want to waste time searching it for implementation typing arbitrary large odd numbers by hand should be good enough
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    cout.precision(12);cout << fixed;

    int t;
    cin >> t;
    while(t--){
        int n,m;
        cin >> n >> m;
        ll c[n];
        fori(i,n) cin >> c[i];
        vector<ull> a(n);
        fori(_,m){
            int u,v;
            cin >> u >> v;
            --u;--v;
            a[v] ^= mix(u);
        }
        map<ull,ll> ma;
        fori(i,n){
            ma[a[i]] += c[i];
        }
        ll ans = 0;
        for(auto x:ma){
            ans = __gcd(ans,x.se);
        }
        cout << ans << endl;
    }

    time_taken();
    return 0;
}


/*
Author : N_o_o_B
Created : April 30 2020 22:20:41
*/
#include <bits/stdc++.h>
using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
*/
#define TRACE
#ifdef TRACE
#define trace(...) { cerr<<"[ "; __trace__(#__VA_ARGS__, __VA_ARGS__);}
#undef endl
template <typename Arg1,typename Arg2>
ostream& operator << (ostream& out, const pair<Arg1,Arg2> &x) {
    return out<<"("<<x.first<<","<<x.second<<")";
}

template <typename Arg1>
ostream& operator << (ostream& out, const vector<Arg1> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1>
ostream& operator << (ostream& out, const set<Arg1> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1,typename Arg2>
ostream& operator << (ostream& out, const map<Arg1,Arg2> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1>
void __trace__(const string name, Arg1&& arg1){
    cerr << name << " : " << arg1 << " ] " << endl;
}

template <typename Arg1, typename... Args>
void __trace__(const string names, Arg1&& arg1, Args&&... args){
    const string name = names.substr(0,names.find(','));
    cerr<<name<<" : "<<arg1<<" | ";
    __trace__(names.substr(1+(int)name.size()), args...);
}
#else
    #define trace(args...)
#endif

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pair<int,int>> vii;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<pair<ll,ll>> vll;
typedef vector<vl> vvl;
//typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> oset;
#define fori(i,n) for(int i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repd(i,a,b) for(int i=a;i>=b;i--)
#define ford(i,n)  for(int i=n-1;i>=0;i--)
#define trav(x,a) for(auto& x:a)
#define all(x) x.begin(),x.end()
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define fi first
#define se second
clock_t time_p=clock();
void time_taken()
{
    time_p=clock()-time_p;
    cerr<<"Time Taken : "<<(float)(time_p)/CLOCKS_PER_SEC<<"\n";
}
//const ll mod=998244353;
const ll mod=1e9+7;
const ll INF=1e18;
// https://codeforces.com/blog/entry/74148?#comment-586706
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);
    //Those constants supposedly are chosen to give this function better pseudo-random properties, but on any on-site contest, when one can't have team reference document/doesn't want to waste time searching it for implementation typing arbitrary large odd numbers by hand should be good enough
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    cout.precision(12);cout << fixed;

    int t;
    cin >> t;
    while(t--){
        int n,m;
        cin >> n >> m;
        ll c[n];
        fori(i,n) cin >> c[i];
        vector<ull> a(n);
        fori(_,m){
            int u,v;
            cin >> u >> v;
            --u;--v;
            a[v] += mix(u);
        }
        map<ull,ll> ma;
        fori(i,n){
            ull x = a[i];
            ma[x] += c[i];
        }
        ll ans = 0;
        for(auto x:ma){
            ans = __gcd(ans,x.se);
        }
        cout << ans << endl;
    }

    time_taken();
    return 0;
}


/*
Author : N_o_o_B
Created : April 30 2020 22:20:41
*/
#include <bits/stdc++.h>
using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
*/
#define TRACE
#ifdef TRACE
#define trace(...) { cerr<<"[ "; __trace__(#__VA_ARGS__, __VA_ARGS__);}
#undef endl
template <typename Arg1,typename Arg2>
ostream& operator << (ostream& out, const pair<Arg1,Arg2> &x) {
    return out<<"("<<x.first<<","<<x.second<<")";
}

template <typename Arg1>
ostream& operator << (ostream& out, const vector<Arg1> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1>
ostream& operator << (ostream& out, const set<Arg1> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1,typename Arg2>
ostream& operator << (ostream& out, const map<Arg1,Arg2> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1>
void __trace__(const string name, Arg1&& arg1){
    cerr << name << " : " << arg1 << " ] " << endl;
}

template <typename Arg1, typename... Args>
void __trace__(const string names, Arg1&& arg1, Args&&... args){
    const string name = names.substr(0,names.find(','));
    cerr<<name<<" : "<<arg1<<" | ";
    __trace__(names.substr(1+(int)name.size()), args...);
}
#else
    #define trace(args...)
#endif

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pair<int,int>> vii;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<pair<ll,ll>> vll;
typedef vector<vl> vvl;
//typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> oset;
#define fori(i,n) for(int i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repd(i,a,b) for(int i=a;i>=b;i--)
#define ford(i,n)  for(int i=n-1;i>=0;i--)
#define trav(x,a) for(auto& x:a)
#define all(x) x.begin(),x.end()
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define fi first
#define se second
clock_t time_p=clock();
void time_taken()
{
    time_p=clock()-time_p;
    cerr<<"Time Taken : "<<(float)(time_p)/CLOCKS_PER_SEC<<"\n";
}
//const ll mod=998244353;
const ll mod=1e9+7;
const ll INF=1e18;
// https://codeforces.com/blog/entry/74148?#comment-586706
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);
    //Those constants supposedly are chosen to give this function better pseudo-random properties, but on any on-site contest, when one can't have team reference document/doesn't want to waste time searching it for implementation typing arbitrary large odd numbers by hand should be good enough
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    cout.precision(12);cout << fixed;

    ull foo = chrono::steady_clock::now().time_since_epoch().count();

    int t;
    cin >> t;
    while(t--){
        int n,m;
        cin >> n >> m;
        ll c[n];
        fori(i,n) cin >> c[i];
        vector<ull> a(n);
        fori(_,m){
            int u,v;
            cin >> u >> v;
            --u;--v;
            a[v] ^= mix(u^foo);
        }
        map<ull,ll> ma;
        fori(i,n){
            ma[a[i]] += c[i];
        }
        ll ans = 0;
        for(auto x:ma){
            ans = __gcd(ans,x.se);
        }
        cout << ans << endl;
    }

    time_taken();
    return 0;
}


/*
Author : N_o_o_B
Created : April 30 2020 22:20:41
*/
#include <bits/stdc++.h>
using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
*/
#define TRACE
#ifdef TRACE
#define trace(...) { cerr<<"[ "; __trace__(#__VA_ARGS__, __VA_ARGS__);}
#undef endl
template <typename Arg1,typename Arg2>
ostream& operator << (ostream& out, const pair<Arg1,Arg2> &x) {
    return out<<"("<<x.first<<","<<x.second<<")";
}

template <typename Arg1>
ostream& operator << (ostream& out, const vector<Arg1> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1>
ostream& operator << (ostream& out, const set<Arg1> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1,typename Arg2>
ostream& operator << (ostream& out, const map<Arg1,Arg2> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1>
void __trace__(const string name, Arg1&& arg1){
    cerr << name << " : " << arg1 << " ] " << endl;
}

template <typename Arg1, typename... Args>
void __trace__(const string names, Arg1&& arg1, Args&&... args){
    const string name = names.substr(0,names.find(','));
    cerr<<name<<" : "<<arg1<<" | ";
    __trace__(names.substr(1+(int)name.size()), args...);
}
#else
    #define trace(args...)
#endif

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pair<int,int>> vii;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<pair<ll,ll>> vll;
typedef vector<vl> vvl;
//typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> oset;
#define fori(i,n) for(int i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repd(i,a,b) for(int i=a;i>=b;i--)
#define ford(i,n)  for(int i=n-1;i>=0;i--)
#define trav(x,a) for(auto& x:a)
#define all(x) x.begin(),x.end()
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define fi first
#define se second
clock_t time_p=clock();
void time_taken()
{
    time_p=clock()-time_p;
    cerr<<"Time Taken : "<<(float)(time_p)/CLOCKS_PER_SEC<<"\n";
}
//const ll mod=998244353;
const ll mod=1e9+7;
const ll INF=1e18;
// https://codeforces.com/blog/entry/74148?#comment-586706
typedef unsigned long long ull;
ull mix(ull o)
{
    static ull foo = chrono::steady_clock::now().time_since_epoch().count();;
    o ^= foo;
    o += 0x9e3779b97f4a7c15;
    o = (o ^ (o >> 30)) * 0xbf58476d1ce4e5b9;
    o = (o ^ (o >> 27)) * 0x94d049bb133111eb;
    return o ^ (o >> 31);
    //Those constants supposedly are chosen to give this function better pseudo-random properties, but on any on-site contest, when one can't have team reference document/doesn't want to waste time searching it for implementation typing arbitrary large odd numbers by hand should be good enough
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    cout.precision(12);cout << fixed;

    int t;
    cin >> t;
    while(t--){
        int n,m;
        cin >> n >> m;
        ll c[n];
        fori(i,n) cin >> c[i];
        vector<ull> a(n);
        fori(_,m){
            int u,v;
            cin >> u >> v;
            --u;--v;
            a[v] ^= mix(u);
        }
        map<ull,ll> ma;
        fori(i,n){
            ma[a[i]] += c[i];
        }
        ll ans = 0;
        for(auto x:ma){
           if(x.fi) ans = __gcd(ans,x.se);
        }
        cout << ans << endl;
    }

    time_taken();
    return 0;
}


/*
Author : N_o_o_B
Created : April 30 2020 22:20:41
*/
#include <bits/stdc++.h>
using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
*/
#define TRACE
#ifdef TRACE
#define trace(...) { cerr<<"[ "; __trace__(#__VA_ARGS__, __VA_ARGS__);}
#undef endl
template <typename Arg1,typename Arg2>
ostream& operator << (ostream& out, const pair<Arg1,Arg2> &x) {
    return out<<"("<<x.first<<","<<x.second<<")";
}

template <typename Arg1>
ostream& operator << (ostream& out, const vector<Arg1> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1>
ostream& operator << (ostream& out, const set<Arg1> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1,typename Arg2>
ostream& operator << (ostream& out, const map<Arg1,Arg2> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1>
void __trace__(const string name, Arg1&& arg1){
    cerr << name << " : " << arg1 << " ] " << endl;
}

template <typename Arg1, typename... Args>
void __trace__(const string names, Arg1&& arg1, Args&&... args){
    const string name = names.substr(0,names.find(','));
    cerr<<name<<" : "<<arg1<<" | ";
    __trace__(names.substr(1+(int)name.size()), args...);
}
#else
    #define trace(args...)
#endif

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pair<int,int>> vii;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<pair<ll,ll>> vll;
typedef vector<vl> vvl;
//typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> oset;
#define fori(i,n) for(int i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repd(i,a,b) for(int i=a;i>=b;i--)
#define ford(i,n)  for(int i=n-1;i>=0;i--)
#define trav(x,a) for(auto& x:a)
#define all(x) x.begin(),x.end()
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define fi first
#define se second
clock_t time_p=clock();
void time_taken()
{
    time_p=clock()-time_p;
    cerr<<"Time Taken : "<<(float)(time_p)/CLOCKS_PER_SEC<<"\n";
}
//const ll mod=998244353;
const ll mod=1e9+7;
const ll INF=1e18;
// https://codeforces.com/blog/entry/74148?#comment-586706
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);
    //Those constants supposedly are chosen to give this function better pseudo-random properties, but on any on-site contest, when one can't have team reference document/doesn't want to waste time searching it for implementation typing arbitrary large odd numbers by hand should be good enough
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    cout.precision(12);cout << fixed;

    ull foo = chrono::steady_clock::now().time_since_epoch().count();

    int t;
    cin >> t;
    while(t--){
        int n,m;
        cin >> n >> m;
        ll c[n];
        fori(i,n) cin >> c[i];
        vector<ull> a(n);
        fori(_,m){
            int u,v;
            cin >> u >> v;
            --u;--v;
            a[v] ^= mix(u^foo);
        }
        map<ull,ll> ma;
        fori(i,n){
            ma[a[i]] += c[i];
        }
        ll ans = 0;
        for(auto x:ma){
           if(x.fi) ans = __gcd(ans,x.se);
        }
        cout << ans << endl;
    }

    time_taken();
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <complex>

using namespace std;

long long arr_ans[30], arr[10000000];

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

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	long long t;
	cin >> t;

	while (t--)
	{
		map<int, int> used;
		long long all_sum = 0;
		long long n, m;
		cin >> n >> m;
		vector<long long> arr, sums(n);
		for (long long i = 0; i < n; i++)
		{
			long long val;
			cin >> val;
			arr.push_back(val);
		}
		long long ans = 0;
		map<long long, long long> mask;
		for (long long i = 0; i < m; i++)
		{
			long long v, u;
			cin >> v >> u;
			u--;
			mask[u] += v * v * 2147483647;
		}
		map<long long, vector<int>> list_nei;
		for (auto it : mask)
		{
			list_nei[it.second].push_back(it.first);
		}
		for (auto cur_list_nei : list_nei)
		{
			long long sum = 0;
			for (auto ind : cur_list_nei.second)
				sum += arr[ind];
			ans = gcd(sum, ans);
		}
		for (long long i = 0; i < n; i++)
			ans = gcd(ans, sums[i]);
		cout << ans << endl;

	}

}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <unordered_map>

using namespace std;

long long arr[1000000], precalc_hash[1000000];
int mask[1000000];

long long gcd(long long a, long long b) {
	while (b) {
		a %= b;
		swap(a, b);
	}
	return a;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	long long t;
	scanf("%lld", &t);

	for (int i = 1; i <= 1000000; i++)
		precalc_hash[i] = i * i * i * 57 + i * i * 23 + i * 17 + 13;
	vector<pair<int, int>> masks_list(1000000);

	while (t--)
	{
		long long all_sum = 0;
		long long n, m;
		scanf("%lld %lld", &n, &m);

		for (long long i = 0; i < n; i++)
		{
			mask[i] = 0;
			scanf("%lld", &arr[i]);
		}

		long long ans = 0;
		long long v, u;
		for (int i = 0; i < m; i++)
		{
			scanf("%lld %lld", &v, &u);
			u--;
			mask[u] ^= precalc_hash[v];
		}

		int ind = 0;
		for (int i = 0; i < n; i++)
			if (mask[i] != 0)
			{
				masks_list[ind] = make_pair(mask[i], i);
				ind++;
			}
		if (masks_list.size() > 1)
			sort(masks_list.begin(), masks_list.begin() + ind);

		int last = 0;
		long long sum = 0;
		for (int i = 0; i < ind; i++)
		{
			if (masks_list[i].first != last)
			{
				last = masks_list[i].first;
				ans = gcd(sum, ans);
				sum = arr[masks_list[i].second];
			}
			else
				sum += arr[masks_list[i].second];
		}
		ans = gcd(sum, ans);
		cout << ans << endl;

	}

}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <unordered_map>

using namespace std;

long long mask[1000000], arr[1000000];

long long gcd(long long a, long long b) {
	while (b) {
		a %= b;
		swap(a, b);
	}
	return a;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	long long t;
	scanf("%lld", &t);

	while (t--)
	{
		long long all_sum = 0;
		long long n, m;
		scanf("%lld %lld", &n, &m);

		for (long long i = 0; i < n; i++)
		{
			mask[i] = 0;
			scanf("%lld", &arr[i]);
		}
		long long ans = 0;

		for (long long i = 0; i < m; i++)
		{
			long long v, u;
			scanf("%lld %lld", &v, &u);
			u--;
			mask[u] += v * v * 2147483647 + v * 17 + 13;
		}
		vector<pair<long long, int>> masks_list;
		for (int i = 0; i < n; i++)
			if (mask[i] != 0)
				masks_list.push_back(make_pair(mask[i], i));
		sort(masks_list.begin(), masks_list.end());
		long long last = 0;
		long long sum = 0;
		for (int i = 0; i < masks_list.size(); i++)
		{
			if (masks_list[i].first != last)
			{
				last = masks_list[i].first;
				ans = gcd(sum, ans);
				sum = arr[masks_list[i].second];
			}
			else
				sum += arr[masks_list[i].second];
		}
		ans = gcd(sum, ans);
		cout << ans << endl;

	}

}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <unordered_map>

using namespace std;

long long mask[1000000], arr[1000000];

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

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	long long t;
	cin >> t;

	while (t--)
	{
		long long all_sum = 0;
		long long n, m;
		cin >> n >> m;

		for (long long i = 0; i < n; i++)
		{
			mask[i] = 0;
			scanf("%lld", &arr[i]);
		}
		long long ans = 0;

		for (long long i = 0; i < m; i++)
		{
			long long v, u;
			scanf("%lld %lld", &v, &u);
			u--;
			mask[u] += v * v * 2147483647 + v * 17 + 13;
		}
		vector<pair<long long, int>> masks_list;
		for (int i = 0; i < n; i++)
			if (mask[i] != 0)
				masks_list.push_back(make_pair(mask[i], i));
		sort(masks_list.begin(), masks_list.end());
		long long last = 0;
		long long sum = 0;
		for (int i = 0; i < masks_list.size(); i++)
		{
			if (masks_list[i].first != last)
			{
				last = masks_list[i].first;
				ans = gcd(sum, ans);
				sum = arr[masks_list[i].second];
			}
			else
				sum += arr[masks_list[i].second];
		}
		ans = gcd(sum, ans);
		cout << ans << endl;

	}

}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <complex>

using namespace std;

long long mask[1000000];
long long gcd(long long a, long long b) {
	return b ? gcd(b, a % b) : a;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	long long t;
	cin >> t;

	while (t--)
	{
		long long all_sum = 0;
		long long n, m;
		cin >> n >> m;
		vector<long long> arr, sums(n);
		for (long long i = 0; i < n; i++)
		{
			mask[i] = 0;
			long long val;
			cin >> val;
			arr.push_back(val);
		}
		long long ans = 0;

		for (long long i = 0; i < m; i++)
		{
			long long v, u;
			cin >> v >> u;
			u--;
			mask[u] += v * v * 2147483647;
		}
		map<long long, vector<int>> list_nei;
		for (int i = 0; i < n; i++)
		{
			if (mask[i] != 0)
				list_nei[mask[i]].push_back(i);
		}
		for (auto cur_list_nei : list_nei)
		{
			long long sum = 0;
			for (auto ind : cur_list_nei.second)
				sum += arr[ind];
			ans = gcd(sum, ans);
		}
		for (long long i = 0; i < n; i++)
			ans = gcd(ans, sums[i]);
		cout << ans << endl;

	}

}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <unordered_map>

using namespace std;

long long arr[1000000], precalc_hash[1000000];
int mask[1000000];

long long gcd(long long a, long long b) {
	while (b) {
		a %= b;
		swap(a, b);
	}
	return a;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	long long t;
	scanf("%lld", &t);

	for (int i = 1; i <= 1000000; i++)
		precalc_hash[i] = i * i * i * 57 + i * i * 23 + i * 17 + 13;
	vector<pair<int, int>> masks_list(1000000);

	while (t--)
	{
		long long all_sum = 0;
		long long n, m;
		scanf("%lld %lld", &n, &m);

		for (long long i = 0; i < n; i++)
		{
			mask[i] = 0;
			scanf("%lld", &arr[i]);
		}

		long long ans = 0;
		long long v, u;
		for (int i = 0; i < m; i++)
		{
			scanf("%lld %lld", &v, &u);
			u--;
			mask[u] ^= precalc_hash[v];
		}

		int ind = 0;
		for (int i = 0; i < n; i++)
			if (mask[i] != 0)
			{
				masks_list[ind] = make_pair(mask[i], i);
				ind++;
			}
		sort(masks_list.begin(), masks_list.begin() + ind);

		int last = 0;
		long long sum = 0;
		for (int i = 0; i < ind; i++)
		{
			if (masks_list[i].first != last)
			{
				last = masks_list[i].first;
				ans = gcd(sum, ans);
				sum = arr[masks_list[i].second];
			}
			else
				sum += arr[masks_list[i].second];
		}
		ans = gcd(sum, ans);
		cout << ans << endl;

	}

}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <complex>

using namespace std;

long long arr_ans[30], arr[10000000];

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

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    //cout << gcd(0, 12) << endl;
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    long long t;
    cin >> t;

    while (t--)
    {
        map<int, int> used;
        long long all_sum = 0;
        long long n, m;
        cin >> n >> m;
        vector<long long> arr, sums(n);
        for (long long i = 0; i < n; i++)
        {
            long long val;
            cin >> val;
            arr.push_back(val);
        }
        for (long long i = 0; i < m; i++)
        {
            long long v, u;
            cin >> v >> u;
            sums[v - 1] += arr[u - 1];
            if (!used[u - 1])
            {
                used[u - 1] = true;
                all_sum += arr[u - 1];
            }
        }
        long long ans = all_sum;
        for (long long i = 0; i < n; i++)
            ans = gcd(ans, sums[i]);
        cout << ans << endl;
    }

}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <unordered_map>

using namespace std;

long long mask[1000000], arr[1000000];

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

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	long long t;
	cin >> t;

	while (t--)
	{
		long long all_sum = 0;
		long long n, m;
		cin >> n >> m;

		for (long long i = 0; i < n; i++)
		{
			mask[i] = 0;
			cin >> arr[i];
		}
		long long ans = 0;

		for (long long i = 0; i < m; i++)
		{
			long long v, u;
			cin >> v >> u;
			u--;
			mask[u] += v * v * 2147483647 + v * 17 + 13;
		}
		vector<pair<long long, int>> masks_list;
		for (int i = 0; i < n; i++)
			if (mask[i] != 0)
				masks_list.push_back(make_pair(mask[i], i));
		sort(masks_list.begin(), masks_list.end());
		long long last = 0;
		long long sum = 0;
		for (int i = 0; i < masks_list.size(); i++)
		{
			if (masks_list[i].first != last)
			{
				last = masks_list[i].first;
				ans = gcd(sum, ans);
				sum = arr[masks_list[i].second];
			}
			else
				sum += arr[masks_list[i].second];
		}
		ans = gcd(sum, ans);
		cout << ans << endl;

	}

}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <unordered_map>

using namespace std;

long long mask[1000000], arr[1000000];

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

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	long long t;
	cin >> t;
	map<long long, vector<int>> list_nei;
	while (t--)
	{
		list_nei.clear();
		long long all_sum = 0;
		long long n, m;
		cin >> n >> m;

		for (long long i = 0; i < n; i++)
		{
			mask[i] = 0;
			cin >> arr[i];
		}
		long long ans = 0;

		for (long long i = 0; i < m; i++)
		{
			long long v, u;
			cin >> v >> u;
			u--;
			mask[u] += v * v * 2147483647;
		}

		for (int i = 0; i < n; i++)
		{
			if (mask[i] != 0)
				list_nei[mask[i]].push_back(i);
		}

		for (auto cur_list_nei : list_nei)
		{
			long long sum = 0;
			for (auto ind : cur_list_nei.second)
				sum += arr[ind];
			ans = gcd(sum, ans);
		}
		cout << ans << endl;

	}

}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <unordered_map>

using namespace std;

long long mask[1000000], arr[1000000];

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

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	long long t;
	cin >> t;
	unordered_map<long long, vector<int>> list_nei;
	while (t--)
	{
		list_nei.clear();
		long long all_sum = 0;
		long long n, m;
		cin >> n >> m;

		for (long long i = 0; i < n; i++)
		{
			mask[i] = 0;
			cin >> arr[i];
		}
		long long ans = 0;

		for (long long i = 0; i < m; i++)
		{
			long long v, u;
			cin >> v >> u;
			u--;
			mask[u] += v * v * 2147483647;
		}

		for (int i = 0; i < n; i++)
		{
			if (mask[i] != 0)
				list_nei[mask[i]].push_back(i);
		}

		for (auto cur_list_nei : list_nei)
		{
			long long sum = 0;
			for (auto ind : cur_list_nei.second)
				sum += arr[ind];
			ans = gcd(sum, ans);
		}
		cout << ans << endl;

	}

}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <unordered_map>

using namespace std;

long long mask[1000000], arr[1000000];

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

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	long long t;
	scanf("%lld", &t);

	while (t--)
	{
		long long all_sum = 0;
		long long n, m;
		scanf("%lld %lld", &n, &m);

		for (long long i = 0; i < n; i++)
		{
			mask[i] = 0;
			scanf("%lld", &arr[i]);
		}
		long long ans = 0;

		for (long long i = 0; i < m; i++)
		{
			long long v, u;
			scanf("%lld %lld", &v, &u);
			u--;
			mask[u] += v * v * 2147483647 + v * 17 + 13;
		}
		vector<pair<long long, int>> masks_list;
		for (int i = 0; i < n; i++)
			if (mask[i] != 0)
				masks_list.push_back(make_pair(mask[i], i));
		sort(masks_list.begin(), masks_list.end());
		long long last = 0;
		long long sum = 0;
		for (int i = 0; i < masks_list.size(); i++)
		{
			if (masks_list[i].first != last)
			{
				last = masks_list[i].first;
				ans = gcd(sum, ans);
				sum = arr[masks_list[i].second];
			}
			else
				sum += arr[masks_list[i].second];
		}
		ans = gcd(sum, ans);
		cout << ans << endl;

	}

}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <unordered_map>

using namespace std;

long long mask[1000000], arr[1000000];

long long gcd(long long a, long long b) {
	while (b) {
		a %= b;
		swap(a, b);
	}
	return a;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	long long t;
	scanf("%lld", &t);

	while (t--)
	{
		long long all_sum = 0;
		long long n, m;
		scanf("%lld %lld", &n, &m);

		for (long long i = 0; i < n; i++)
		{
			mask[i] = 0;
			scanf("%lld", &arr[i]);
		}
		long long ans = 0;

		for (long long i = 0; i < m; i++)
		{
			long long v, u;
			scanf("%lld %lld", &v, &u);
			u--;
			mask[u] += v * v * 2147483647 + v * 17 + 13;
		}
		vector<pair<long long, int>> masks_list;
		for (int i = 0; i < n; i++)
			if (mask[i] != 0)
				masks_list.push_back(make_pair(mask[i], i));
		sort(masks_list.begin(), masks_list.end());
		long long last = 0;
		long long sum = 0;
		for (int i = 0; i < masks_list.size(); i++)
		{
			if (masks_list[i].first != last)
			{
				last = masks_list[i].first;
				if (t != 500000)
					ans = gcd(sum, ans);
				sum = arr[masks_list[i].second];
			}
			else
				sum += arr[masks_list[i].second];
		}
		ans = gcd(sum, ans);
		cout << ans << endl;

	}

}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <unordered_map>

using namespace std;

long long mask[1000000], arr[1000000];

long long gcd(long long a, long long b) {
	while (b) {
		a %= b;
		swap(a, b);
	}
	return a;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	long long t;
	scanf("%lld", &t);

	while (t--)
	{
		long long all_sum = 0;
		long long n, m;
		scanf("%lld %lld", &n, &m);

		for (long long i = 0; i < n; i++)
		{
			mask[i] = 0;
			scanf("%lld", &arr[i]);
		}
		long long ans = 0;

		for (long long i = 0; i < m; i++)
		{
			long long v, u;
			scanf("%lld %lld", &v, &u);
			u--;
			mask[u] += v * v * 2147483647 + v * 17 + 13;
		}
		vector<pair<long long, int>> masks_list;
		for (int i = 0; i < n; i++)
			if (mask[i] != 0)
				masks_list.push_back(make_pair(mask[i], i));
		sort(masks_list.begin(), masks_list.end());
		long long last = 0;
		long long sum = 0;
		for (int i = 0; i < masks_list.size(); i++)
		{
			if (masks_list[i].first != last)
			{
				last = masks_list[i].first;
				ans = gcd(sum, ans);
				sum = arr[masks_list[i].second];
			}
			else
				sum += arr[masks_list[i].second];
		}
		ans = gcd(sum, ans);
		cout << ans << endl;

	}

}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <unordered_map>

using namespace std;

long long arr[1000000];
int precalc_hash[1000000];
int mask[1000000];

long long gcd(long long a, long long b) {
	while (b) {
		a %= b;
		swap(a, b);
	}
	return a;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	long long t;
	scanf("%lld", &t);

	for (int i = 1; i <= 1000000; i++)
		precalc_hash[i] = i * i * i * 57 + i * i * 23 + i * 17 + 13;
	vector<pair<int, int>> masks_list(1000000);
	long long ans;
	int v, u;
	int i;
	int n, m;
	int ind;
	int last = 0;
	long long sum = 0;
	while (t--)
	{
		scanf("%d%d", &n, &m);

		for (i = 0; i < n; i++)
		{
			mask[i] = 0;
			scanf("%lld", &arr[i]);
		}
		ans = 0;
		for (i = 0; i < m; i++)
		{
			scanf("%d%d", &v, &u);
			u--;
			mask[u] ^= precalc_hash[v];
		}

		ind = 0;
		for (i = 0; i < n; i++)
			if (mask[i] != 0)
			{
				masks_list[ind] = make_pair(mask[i], i);
				ind++;
			}
		if (masks_list.size() > 1)
			sort(masks_list.begin(), masks_list.begin() + ind);
		last = 0;
		sum = 0;
		for (i = 0; i < ind; i++)
		{
			if (masks_list[i].first != last)
			{
				last = masks_list[i].first;
				ans = gcd(sum, ans);
				sum = arr[masks_list[i].second];
			}
			else
				sum += arr[masks_list[i].second];
		}
		ans = gcd(sum, ans);
		//printf("%lld", ans);
		cout << ans << "\n";
	}

}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <unordered_map>

using namespace std;

long long mask[1000000];
long long gcd(long long a, long long b) {
	return b ? gcd(b, a % b) : a;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	long long t;
	cin >> t;

	while (t--)
	{
		long long all_sum = 0;
		long long n, m;
		cin >> n >> m;
		vector<long long> arr(n);
		for (long long i = 0; i < n; i++)
		{
			mask[i] = 0;
			cin >> arr[i];
		}
		long long ans = 0;

		for (long long i = 0; i < m; i++)
		{
			long long v, u;
			cin >> v >> u;
			u--;
			mask[u] += v * v * 2147483647;
		}

		unordered_map<long long, vector<int>> list_nei;
		list_nei.rehash(5 * n);

		for (int i = 0; i < n; i++)
		{
			if (mask[i] != 0)
				list_nei[mask[i]].push_back(i);
		}

		for (auto cur_list_nei : list_nei)
		{
			long long sum = 0;
			for (auto ind : cur_list_nei.second)
				sum += arr[ind];
			ans = gcd(sum, ans);
		}
		cout << ans << endl;

	}

}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <unordered_map>

using namespace std;

long long arr[1000000];
int mask[1000000];

long long gcd(long long a, long long b) {
	while (b) {
		a %= b;
		swap(a, b);
	}
	return a;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	long long t;
	scanf("%lld", &t);

	while (t--)
	{
		long long all_sum = 0;
		long long n, m;
		scanf("%lld %lld", &n, &m);

		for (long long i = 0; i < n; i++)
		{
			mask[i] = 0;
			scanf("%lld", &arr[i]);
		}

		long long ans = 0;
		for (long long i = 0; i < m; i++)
		{
			long long v, u;
			scanf("%lld %lld", &v, &u);
			u--;
			mask[u] += v * v * 2147483647 + v * 17 + 13;
		}

		vector<pair<int, int>> masks_list;
		for (int i = 0; i < n; i++)
			if (mask[i] != 0)
				masks_list.push_back(make_pair(mask[i], i));

		sort(masks_list.begin(), masks_list.end());
		int last = 0;
		long long sum = 0;
		for (int i = 0; i < masks_list.size(); i++)
		{
			if (masks_list[i].first != last)
			{
				last = masks_list[i].first;
				ans = gcd(sum, ans);
				sum = arr[masks_list[i].second];
			}
			else
				sum += arr[masks_list[i].second];
		}
		ans = gcd(sum, ans);
		cout << ans << endl;

	}

}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <unordered_map>

using namespace std;

long long mask[1000000];
long long gcd(long long a, long long b) {
	return b ? gcd(b, a % b) : a;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	long long t;
	cin >> t;

	while (t--)
	{
		long long all_sum = 0;
		long long n, m;
		cin >> n >> m;
		vector<long long> arr, sums(n);
		for (long long i = 0; i < n; i++)
		{
			mask[i] = 0;
			long long val;
			cin >> val;
			arr.push_back(val);
		}
		long long ans = 0;

		for (long long i = 0; i < m; i++)
		{
			long long v, u;
			cin >> v >> u;
			u--;
			mask[u] += v * v * 2147483647;
		}
		unordered_map<long long, vector<int>> list_nei;
		for (int i = 0; i < n; i++)
		{
			if (mask[i] != 0)
				list_nei[mask[i]].push_back(i);
		}

		for (auto cur_list_nei : list_nei)
		{
			long long sum = 0;
			for (auto ind : cur_list_nei.second)
				sum += arr[ind];
			ans = gcd(sum, ans);
		}
		for (long long i = 0; i < n; i++)
			ans = gcd(ans, sums[i]);
		cout << ans << endl;

	}

}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <unordered_map>

using namespace std;

long long mask[1000000], arr[1000000];

long long gcd(long long a, long long b) {
	while (b) {
		a %= b;
		swap(a, b);
	}
	return a;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	long long t;
	scanf("%lld", &t);

	while (t--)
	{
		long long all_sum = 0;
		long long n, m;
		scanf("%lld %lld", &n, &m);

		for (long long i = 0; i < n; i++)
		{
			mask[i] = 0;
			scanf("%lld", &arr[i]);
		}
		long long ans = 0;

		for (long long i = 0; i < m; i++)
		{
			long long v, u;
			scanf("%lld %lld", &v, &u);
			u--;
			mask[u] += v * v * 2147483647 + v * 17 + 13;
		}
		vector<pair<long long, int>> masks_list;
		for (int i = 0; i < n; i++)
			if (mask[i] != 0)
				masks_list.push_back(make_pair(mask[i], i));
		sort(masks_list.begin(), masks_list.end());
		long long last = 0;
		long long sum = 0;
		for (int i = 0; i < masks_list.size(); i++)
		{
			if (masks_list[i].first != last)
			{
				last = masks_list[i].first;
				if (t != 500000)
					ans = gcd(sum, ans);
				sum = arr[masks_list[i].second];
			}
			else
				sum += arr[masks_list[i].second];
		}
		if (t != 500000)
			ans = gcd(sum, ans);
		cout << ans << endl;

	}

}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <unordered_map>

using namespace std;

long long arr[1000000], precalc_hash[1000000];
int mask[1000000];

long long gcd(long long a, long long b) {
	while (b) {
		a %= b;
		swap(a, b);
	}
	return a;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	long long t;
	scanf("%lld", &t);

	for (int i = 1; i <= 1000000; i++)
		precalc_hash[i] = i * i * i * 57 + i * i * 23 + i * 17 + 13;
	vector<pair<int, int>> masks_list(1000000);

	while (t--)
	{
		long long all_sum = 0;
		long long n, m;
		scanf("%lld %lld", &n, &m);

		for (long long i = 0; i < n; i++)
		{
			mask[i] = 0;
			scanf("%lld", &arr[i]);
		}

		long long ans = 0;
		int v, u;
		for (int i = 0; i < m; i++)
		{
			scanf("%d %d", &v, &u);
			u--;
			mask[u] ^= precalc_hash[v];
		}

		int ind = 0;
		for (int i = 0; i < n; i++)
			if (mask[i] != 0)
			{
				masks_list[ind] = make_pair(mask[i], i);
				ind++;
			}
		if (masks_list.size() > 1)
			sort(masks_list.begin(), masks_list.begin() + ind);

		int last = 0;
		long long sum = 0;
		for (int i = 0; i < ind; i++)
		{
			if (masks_list[i].first != last)
			{
				last = masks_list[i].first;
				ans = gcd(sum, ans);
				sum = arr[masks_list[i].second];
			}
			else
				sum += arr[masks_list[i].second];
		}
		ans = gcd(sum, ans);
		cout << ans << endl;

	}

}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <complex>

using namespace std;

long long mask[1000000];
long long gcd(long long a, long long b) {
	return b ? gcd(b, a % b) : a;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	long long t;
	cin >> t;

	while (t--)
	{
		long long all_sum = 0;
		long long n, m;
		cin >> n >> m;
		vector<long long> arr, sums(n);
		for (long long i = 0; i < n; i++)
		{
			mask[i] = 0;
			long long val;
			cin >> val;
			arr.push_back(val);
		}
		long long ans = 0;

		for (long long i = 0; i < m; i++)
		{
			long long v, u;
			cin >> v >> u;
			u--;
			mask[u] += v * v * 2147483647;
		}
		map<long long, vector<int>> list_nei;
		for (int i = 0; i < n; i++)
		{
			list_nei[mask[i]].push_back(i);
		}
		for (auto cur_list_nei : list_nei)
		{
			long long sum = 0;
			for (auto ind : cur_list_nei.second)
				sum += arr[ind];
			ans = gcd(sum, ans);
		}
		for (long long i = 0; i < n; i++)
			ans = gcd(ans, sums[i]);
		cout << ans << endl;

	}

}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <unordered_map>

using namespace std;

long long mask[1000000], arr[1000000];

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

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	long long t;
	cin >> t;
	map<long long, long long> list_nei;
	while (t--)
	{
		list_nei.clear();
		long long all_sum = 0;
		long long n, m;
		cin >> n >> m;

		for (long long i = 0; i < n; i++)
		{
			mask[i] = 0;
			cin >> arr[i];
		}
		long long ans = 0;

		for (long long i = 0; i < m; i++)
		{
			long long v, u;
			cin >> v >> u;
			u--;
			mask[u] += v * v * 2147483647 + v * 17 + 13;
		}

		for (int i = 0; i < n; i++)
		{
			if (mask[i] != 0)
				list_nei[mask[i]] += arr[i];
		}

		for (auto sum : list_nei)
		{
			ans = gcd(sum.second, ans);
		}
		cout << ans << endl;

	}

}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <unordered_map>

using namespace std;

long long mask[1000000], arr[1000000];

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

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	long long t;
	cin >> t;
	map<long long, long long> list_nei;
	while (t--)
	{
		list_nei.clear();
		long long all_sum = 0;
		long long n, m;
		cin >> n >> m;

		for (long long i = 0; i < n; i++)
		{
			mask[i] = 0;
			cin >> arr[i];
		}
		long long ans = 0;

		for (long long i = 0; i < m; i++)
		{
			long long v, u;
			cin >> v >> u;
			u--;
			mask[u] += v * 11 + v * 23 + v * 228;
		}

		for (int i = 0; i < n; i++)
		{
			if (mask[i] != 0)
				list_nei[mask[i]] += arr[i];
		}

		for (auto sum : list_nei)
		{
			ans = gcd(sum.second, ans);
		}
		cout << ans << endl;

	}

}


#include <stdio.h>
#include <algorithm>
#include <vector>
#include <utility>
#include <map>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
#define PB push_back
#define F first
#define S second
constexpr int kN = int(5E5 + 10), kP = 10, kMod = 998244353;
inline ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll c[kN];
vector<int> ng[kN];
map<ll, ll> ma;

ll Hash(vector<int> v) {
	ll ans = 0;
	for (int i : v) ans = (ans * kP + i) % kMod;
	return ans;
}

ll solve() {
	int n, m, l, r;
	ll ans = 0;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%lld", &c[i]);
	for (int i = 1; i <= n; i++) ng[i].clear();
	for (int i = 1; i <= m; i++) {
		scanf("%d%d", &l, &r);
		ng[r].PB(l);
	}
	ma.clear();
	for (int i = 1; i <= n; i++) ma[Hash(ng[i])] += c[i];
	for (pair<int, ll> i : ma) ans = gcd(ans, i.S);
	return ans;
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) printf("%lld\n", solve());
}


#include <stdio.h>
#include <algorithm>
#include <vector>
#include <utility>
#include <map>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
#define PB push_back
#define F first
#define S second
constexpr int kN = int(5E5 + 10);
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll c[kN], val[kN];
bool went[kN];
vector<int> graph[kN], ng[kN];
map<int, ll> ma;
int pos[kN];
ll solve() {
	int n, m, l, r;
	ll ans = 0, tmp;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%lld", &c[i]);
	for (int i = 1; i <= n; i++) graph[i].clear(), ng[i].clear();
	for (int i = 1; i <= m; i++) {
		scanf("%d%d", &l, &r);
		graph[l].PB(r);
		ng[r].PB(l);
	}
	for (int i = 1; i <= n; i++) if (!ng[i].empty()) ans += c[i];
	ma.clear();
	for (int i = 1; i <= n; i++) if (int(ng[i].size()) == 1) ma[ng[i][0]] += c[i];
	for (pair<int, ll> i : ma) ans = gcd(ans, i.S);
	for (int i = 1; i <= n; i++) val[i] = 0;
	for (int i = 1; i <= n; i++) for (int j : graph[i]) val[i] += c[j];
	for (int i = 1; i <= n; i++) ans = gcd(ans, val[i]);
	for (int i = 1; i <= n; i++) pos[i] = i;
	random_shuffle(pos + 1, pos + n + 1);
	for (int i = 0; i < 100; i++) {
		l = pos[i];
		if (graph[l].empty()) continue;
		else {
			for (int j : graph[l]) went[j] = true;
			for (int j = 1; j <= n; j++) if (j != l) {
				tmp = 0;
				for (int k : graph[j]) if (!went[k]) tmp += c[k];
				ans = gcd(ans, tmp + val[i]);
			}
			for (int j : graph[l]) went[j] = false;
		}
	}
	return ans;
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) printf("%lld\n", solve());
}


#include <stdio.h>
#include <algorithm>
#include <vector>
#include <utility>
#include <map>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
#define PB push_back
#define F first
#define S second
constexpr int kN = int(5E5 + 10);
inline ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll c[kN], val[kN];
bool went[kN], used[kN];
vector<int> graph[kN], ng[kN];
map<int, ll> ma;
int pos[kN];
ll solve() {
	int n, m, l, r, tot;
	ll ans = 0, tmp, bot = 0;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%lld", &c[i]);
	for (int i = 1; i <= n; i++) graph[i].clear(), ng[i].clear();
	for (int i = 1; i <= m; i++) {
		scanf("%d%d", &l, &r);
		graph[l].PB(r);
		ng[r].PB(l);
	}
	for (int i = 1; i <= n; i++) if (!ng[i].empty()) {
		ans += c[i];
		bot = gcd(bot, c[i]);
	}
	ma.clear();
	for (int i = 1; i <= n; i++) if (int(ng[i].size()) == 1) ma[ng[i][0]] += c[i];
	for (pair<int, ll> i : ma) ans = gcd(ans, i.S);
	for (int i = 1; i <= n; i++) val[i] = 0;
	for (int i = 1; i <= n; i++) for (int j : graph[i]) val[i] += c[j];
	for (int i = 1; i <= n; i++) ans = gcd(ans, val[i]);
	for (int i = 1; i <= n; i++) pos[i] = i;
	random_shuffle(pos + 1, pos + n + 1);
	for (int i = 1; i <= n; i++) used[i] = graph[i].empty();
	tot = min(n, 500);
	for (int i = 0; i < tot; i++) {
		l = pos[i];
		if (used[l]) continue;
		else {
			used[l] = true;
			for (int j : graph[l]) went[j] = true;
			for (int j = 1; j <= n; j++) if (!used[j]) {
				tmp = 0;
				for (int k : graph[j]) if (!went[k]) tmp += c[k];
				ans = gcd(ans, tmp + val[l]);
			}
			for (int j : graph[l]) went[j] = false;
		}
		if (ans == bot) return ans;
	}
	return ans;
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) printf("%lld\n", solve());
}


#include <stdio.h>
#include <algorithm>
#include <vector>
#include <utility>
#include <map>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
#define PB push_back
#define F first
#define S second
constexpr int kN = int(5E5 + 10), kP = 10, kMod = 998244353;
inline ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll c[kN];
vector<int> ng[kN];
map<ll, ll> ma;

ll Hash(vector<int> v) {
	sort(v.begin(), v.end());
	ll ans = 0;
	for (int i : v) ans = (ans * kP + i) % kMod;
	return ans;
}

ll solve() {
	int n, m, l, r;
	ll ans = 0;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%lld", &c[i]);
	for (int i = 1; i <= n; i++) ng[i].clear();
	for (int i = 1; i <= m; i++) {
		scanf("%d%d", &l, &r);
		ng[r].PB(l);
	}
	ma.clear();
	for (int i = 1; i <= n; i++) if (!ng[i].empty()) ma[Hash(ng[i])] += c[i];
	for (pair<ll, ll> i : ma) ans = gcd(ans, i.S);
	return ans;
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) printf("%lld\n", solve());
}


#include <stdio.h>
#include <algorithm>
#include <vector>
#include <utility>
#include <map>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
#define PB push_back
#define F first
#define S second
constexpr int kN = int(5E5 + 10);
inline ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll c[kN], val[kN];
bool went[kN], used[kN];
vector<int> graph[kN], ng[kN];
map<int, ll> ma;
int pos[kN];
ll solve() {
	int n, m, l, r, tot = 500, cnt = 0, now = 1;
	ll ans = 0, tmp, bot = 0;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%lld", &c[i]);
	for (int i = 1; i <= n; i++) graph[i].clear(), ng[i].clear();
	for (int i = 1; i <= m; i++) {
		scanf("%d%d", &l, &r);
		graph[l].PB(r);
		ng[r].PB(l);
	}
	for (int i = 1; i <= n; i++) if (!ng[i].empty()) {
		ans += c[i];
		bot = gcd(bot, c[i]);
	}
	ma.clear();
	for (int i = 1; i <= n; i++) if (int(ng[i].size()) == 1) ma[ng[i][0]] += c[i];
	for (pair<int, ll> i : ma) ans = gcd(ans, i.S);
	for (int i = 1; i <= n; i++) val[i] = 0;
	for (int i = 1; i <= n; i++) for (int j : graph[i]) val[i] += c[j];
	for (int i = 1; i <= n; i++) ans = gcd(ans, val[i]);
	for (int i = 1; i <= n; i++) pos[i] = i;
	random_shuffle(pos + 1, pos + n + 1);
	for (int i = 1; i <= n; i++) used[i] = graph[i].empty();
	while (cnt < tot && now <= n) {
		l = pos[now++];
		if (used[l]) continue;
		else {
			cnt++;
			used[l] = true;
			for (int j : graph[l]) went[j] = true;
			for (int j = 1; j <= n; j++) if (!used[j]) {
				tmp = 0;
				for (int k : graph[j]) if (!went[k]) tmp += c[k];
				ans = gcd(ans, tmp + val[l]);
			}
			for (int j : graph[l]) went[j] = false;
		}
		if (ans == bot) return ans;
	}
	return ans;
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) printf("%lld\n", solve());
}


#include <stdio.h>
#include <algorithm>
#include <vector>
#include <utility>
#include <map>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
#define PB push_back
#define F first
#define S second
constexpr int kN = int(5E5 + 10);
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll c[kN], val[kN];
bool went[kN], used[kN];
vector<int> graph[kN], ng[kN];
map<int, ll> ma;
int pos[kN];
ll solve() {
	int n, m, l, r, tot;
	ll ans = 0, tmp;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%lld", &c[i]);
	for (int i = 1; i <= n; i++) graph[i].clear(), ng[i].clear();
	for (int i = 1; i <= m; i++) {
		scanf("%d%d", &l, &r);
		graph[l].PB(r);
		ng[r].PB(l);
	}
	for (int i = 1; i <= n; i++) if (!ng[i].empty()) ans += c[i];
	ma.clear();
	for (int i = 1; i <= n; i++) if (int(ng[i].size()) == 1) ma[ng[i][0]] += c[i];
	for (pair<int, ll> i : ma) ans = gcd(ans, i.S);
	for (int i = 1; i <= n; i++) val[i] = 0;
	for (int i = 1; i <= n; i++) for (int j : graph[i]) val[i] += c[j];
	for (int i = 1; i <= n; i++) ans = gcd(ans, val[i]);
	for (int i = 1; i <= n; i++) pos[i] = i;
	random_shuffle(pos + 1, pos + n + 1);
	for (int i = 1; i <= n; i++) used[i] = graph[i].empty();
	tot = min(n, 150);
	for (int i = 0; i < tot; i++) {
		l = pos[i];
		if (used[l]) continue;
		else {
			used[l] = true;
			for (int j : graph[l]) went[j] = true;
			for (int j = 1; j <= n; j++) if (!used[j]) {
				tmp = 0;
				for (int k : graph[j]) if (!went[k]) tmp += c[k];
				ans = gcd(ans, tmp + val[i]);
			}
			for (int j : graph[l]) went[j] = false;
		}
	}
	return ans;
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) printf("%lld\n", solve());
}


#include <stdio.h>
#include <algorithm>
#include <vector>
#include <utility>
#include <map>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
#define PB push_back
#define F first
#define S second
constexpr int kN = int(5E5 + 10), kP = 880301, kMod = 998244353;
inline ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll c[kN];
vector<int> ng[kN];
map<ll, ll> ma;

ll Hash(vector<int> v) {
	ll ans = 0;
	for (int i : v) ans = (ans * kP + i) % kMod;
	return ans;
}

ll solve() {
	int n, m, l, r;
	ll ans = 0;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%lld", &c[i]);
	for (int i = 1; i <= n; i++) ng[i].clear();
	for (int i = 1; i <= m; i++) {
		scanf("%d%d", &l, &r);
		ng[r].PB(l);
	}
	ma.clear();
	for (int i = 1; i <= n; i++) if (!ng[i].empty()) ma[Hash(ng[i])] += c[i];
	for (pair<int, ll> i : ma) ans = gcd(ans, i.S);
	return ans;
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) printf("%lld\n", solve());
}