// Problem : C. Instant Noodles
// Contest : Codeforces Round #626 (Div. 1, based on Moscow Open Olympiad in Informatics)
// URL : https://codeforces.com/contest/1322/problem/C
// Memory Limit : 512 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cp-editor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define D(x) cerr << #x << " is " << (x) << "\n";
#define ld long double

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const deque<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}

ll g(ll a,ll b){return b==0?a:g(b,a%b);}
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int t;
	scanf("%i",&t);
	while(t--){
		int n,m;
		scanf("%i %i",&n,&m);
		ll vreme=(ll)1500*max(n,m)/500000;
		ll beg=clock();
		vector<ll> vals(n);
		for(int i=0;i<n;i++)
			scanf("%lld",&vals[i]);
		vector<vector<int> > koji(n);
		for(int i=0;i<m;i++){
			int a,b;
			scanf("%i %i",&a,&b);
			a--;b--;
			koji[a].pb(b);
		}
		vector<bool> taken(n);
		ll gcd=0;
		int tr=0;
		while(clock()-beg<vreme||tr<300){
		  tr++;
			vector<int> undo;
			ll sum=0;
			for(int i=0;i<n;i++){
				if(rng()%100==0){
					for(auto p:koji[i])
						if(!taken[p])
							taken[p]=1,undo.pb(p),sum+=vals[p];
				}
			}
			for(auto p:undo)
				taken[p]=0;
			gcd=g(gcd,sum);
		}
		printf("%lld\n",gcd);
	}
	return 0;
}


// Problem : C. Instant Noodles
// Contest : Codeforces Round #626 (Div. 1, based on Moscow Open Olympiad in Informatics)
// URL : https://codeforces.com/contest/1322/problem/C
// Memory Limit : 512 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cp-editor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define D(x) cerr << #x << " is " << (x) << "\n";
#define ld long double

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const deque<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}

ll g(ll a,ll b){return b==0?a:g(b,a%b);}
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int t;
	scanf("%i",&t);
	while(t--){
		int n,m;
		scanf("%i %i",&n,&m);
		ll vreme=max((ll)10,(ll)1800*max(n,m)/500000);
		ll beg=clock();
		vector<ll> vals(n);
		for(int i=0;i<n;i++)
			scanf("%lld",&vals[i]);
		vector<vector<int> > koji(n);
		for(int i=0;i<m;i++){
			int a,b;
			scanf("%i %i",&a,&b);
			a--;b--;
			koji[a].pb(b);
		}
		vector<bool> taken(n);
		ll gcd=0;
		int tr=0;
		while(clock()-beg<vreme||tr<100){
		  tr++;
			vector<int> undo;
			ll sum=0;
			for(int i=0;i<n;i++){
				if(rng()%2){
					for(auto p:koji[i])
						if(!taken[p])
							taken[p]=1,undo.pb(p),sum+=vals[p];
				}
			}
			for(auto p:undo)
				taken[p]=0;
			gcd=g(gcd,sum);
		}
		printf("%lld\n",gcd);
	}
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll g(ll a,ll b){return b==0?a:g(b,a%b);}
int main()
{
	int t,n,m,a,b;
	scanf("%i",&t);
	while(t--){
		cin>>n>>m;
		vector<ll> v(n);
		for(int i=0;i<n;i++)
			cin>>v[i];
		vector<vector<int> > k(n);
		for(int i=0;i<m;i++){
			cin>>a>>b;
			k[b-1].push_back(a-1);
		}
		map<vector<int>,ll> sum;
		for(int i=0;i<n;i++){
			sort(k[i].begin(),k[i].end());
			sum[k[i]]+=v[i];
		}
		ll gcd=0;
		for(auto p:sum)
			gcd=g(gcd,p.second);
		cout<<gcd<<'\n';
	}
	return 0;
}


// Problem : C. Instant Noodles
// Contest : Codeforces Round #626 (Div. 1, based on Moscow Open Olympiad in Informatics)
// URL : https://codeforces.com/contest/1322/problem/C
// Memory Limit : 512 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cp-editor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define D(x) cerr << #x << " is " << (x) << "\n";
#define ld long double

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const deque<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}

ll g(ll a,ll b){return b==0?a:g(b,a%b);}
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int t;
	scanf("%i",&t);
	while(t--){
		int n,m;
		scanf("%i %i",&n,&m);
		ll vreme=(ll)1500*max(n,m)/500000;
		ll beg=clock();
		vector<ll> vals(n);
		for(int i=0;i<n;i++)
			scanf("%lld",&vals[i]);
		vector<vector<int> > koji(n);
		for(int i=0;i<m;i++){
			int a,b;
			scanf("%i %i",&a,&b);
			a--;b--;
			koji[a].pb(b);
		}
		vector<bool> taken(n);
		ll gcd=0;
		int tr=0;
		while(clock()-beg<vreme||tr<10){
		  tr++;
			vector<int> undo;
			ll sum=0;
			for(int i=0;i<n;i++){
				if(rng()%2){
					for(auto p:koji[i])
						if(!taken[p])
							taken[p]=1,undo.pb(p),sum+=vals[p];
				}
			}
			for(auto p:undo)
				taken[p]=0;
			gcd=g(gcd,sum);
		}
		printf("%lld\n",gcd);
	}
	return 0;
}


// Problem : C. Instant Noodles
// Contest : Codeforces Round #626 (Div. 1, based on Moscow Open Olympiad in Informatics)
// URL : https://codeforces.com/contest/1322/problem/C
// Memory Limit : 512 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cp-editor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define D(x) cerr << #x << " is " << (x) << "\n";
#define ld long double

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const deque<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}

ll g(ll a,ll b){return b==0?a:g(b,a%b);}
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int t;
	scanf("%i",&t);
	while(t--){
		int n,m;
		scanf("%i %i",&n,&m);
		ll vreme=(ll)1500*max(n,m)/500000;
		ll beg=clock();
		vector<ll> vals(n);
		for(int i=0;i<n;i++)
			scanf("%lld",&vals[i]);
		vector<vector<int> > koji(n);
		for(int i=0;i<m;i++){
			int a,b;
			scanf("%i %i",&a,&b);
			a--;b--;
			koji[a].pb(b);
		}
		vector<bool> taken(n);
		ll gcd=0;
		int tr=0;
		while(clock()-beg<vreme||tr<200){
		  tr++;
			vector<int> undo;
			ll sum=0;
			for(int i=0;i<n;i++){
				if(rng()%max(2,n/100)==0){
					for(auto p:koji[i])
						if(!taken[p])
							taken[p]=1,undo.pb(p),sum+=vals[p];
				}
			}
			for(auto p:undo)
				taken[p]=0;
			gcd=g(gcd,sum);
		}
		printf("%lld\n",gcd);
	}
	return 0;
}


// Problem : C. Instant Noodles
// Contest : Codeforces Round #626 (Div. 1, based on Moscow Open Olympiad in Informatics)
// URL : https://codeforces.com/contest/1322/problem/C
// Memory Limit : 512 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cp-editor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define D(x) cerr << #x << " is " << (x) << "\n";
#define ld long double

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const deque<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}

ll g(ll a,ll b){return b==0?a:g(b,a%b);}
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int t;
	scanf("%i",&t);
	while(t--){
		int n,m;
		scanf("%i %i",&n,&m);
		ll vreme=(ll)1500*max(n,m)/500000;
		ll beg=clock();
		vector<ll> vals(n);
		for(int i=0;i<n;i++)
			scanf("%lld",&vals[i]);
		vector<vector<int> > koji(n);
		for(int i=0;i<m;i++){
			int a,b;
			scanf("%i %i",&a,&b);
			a--;b--;
			koji[a].pb(b);
		}
		vector<bool> taken(n);
		ll gcd=0;
		int tr=0;
		while(clock()-beg<vreme||tr<200){
		  tr++;
			vector<int> undo;
			ll sum=0;
			for(int i=0;i<n;i++){
				if(rng()%max(2,n/50)==0){
					for(auto p:koji[i])
						if(!taken[p])
							taken[p]=1,undo.pb(p),sum+=vals[p];
				}
			}
			for(auto p:undo)
				taken[p]=0;
			gcd=g(gcd,sum);
		}
		printf("%lld\n",gcd);
	}
	return 0;
}


// Problem : C. Instant Noodles
// Contest : Codeforces Round #626 (Div. 1, based on Moscow Open Olympiad in Informatics)
// URL : https://codeforces.com/contest/1322/problem/C
// Memory Limit : 512 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cp-editor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define D(x) cerr << #x << " is " << (x) << "\n";
#define ld long double

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const deque<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}

ll g(ll a,ll b){return b==0?a:g(b,a%b);}
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int t;
	scanf("%i",&t);
	while(t--){
		int n,m;
		scanf("%i %i",&n,&m);
		ll vreme=(ll)1500*max(n,m)/500000;
		ll beg=clock();
		vector<ll> vals(n);
		for(int i=0;i<n;i++)
			scanf("%lld",&vals[i]);
		vector<vector<int> > koji(n);
		for(int i=0;i<m;i++){
			int a,b;
			scanf("%i %i",&a,&b);
			a--;b--;
			koji[a].pb(b);
		}
		vector<bool> taken(n);
		ll gcd=0;
		int tr=0;
		while(clock()-beg<vreme||tr<500){
		  tr++;
			vector<int> undo;
			ll sum=0;
			for(int i=0;i<n;i++){
				if(rng()%2){
					for(auto p:koji[i])
						if(!taken[p])
							taken[p]=1,undo.pb(p),sum+=vals[p];
				}
			}
			for(auto p:undo)
				taken[p]=0;
			gcd=g(gcd,sum);
		}
		printf("%lld\n",gcd);
	}
	return 0;
}


// Problem : C. Instant Noodles
// Contest : Codeforces Round #626 (Div. 1, based on Moscow Open Olympiad in Informatics)
// URL : https://codeforces.com/contest/1322/problem/C
// Memory Limit : 512 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cp-editor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define D(x) cerr << #x << " is " << (x) << "\n";
#define ld long double

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const deque<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}

ll g(ll a,ll b){return b==0?a:g(b,a%b);}
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int t;
	scanf("%i",&t);
	while(t--){
		int n,m;
		scanf("%i %i",&n,&m);
		ll vreme=(ll)1800*max(n,m)/500000;
		ll beg=clock();
		vector<ll> vals(n);
		for(int i=0;i<n;i++)
			scanf("%lld",&vals[i]);
		vector<vector<int> > koji(n);
		for(int i=0;i<m;i++){
			int a,b;
			scanf("%i %i",&a,&b);
			a--;b--;
			koji[a].pb(b);
		}
		vector<bool> taken(n);
		ll gcd=0;
		int tr=0;
		while(clock()-beg<vreme||tr<20){
		  tr++;
			vector<int> undo;
			ll sum=0;
			for(int i=0;i<n;i++){
				if(rng()%2){
					for(auto p:koji[i])
						if(!taken[p])
							taken[p]=1,undo.pb(p),sum+=vals[p];
				}
			}
			for(auto p:undo)
				taken[p]=0;
			gcd=g(gcd,sum);
		}
		printf("%lld\n",gcd);
	}
	return 0;
}


// Problem : C. Instant Noodles
// Contest : Codeforces Round #626 (Div. 1, based on Moscow Open Olympiad in Informatics)
// URL : https://codeforces.com/contest/1322/problem/C
// Memory Limit : 512 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cp-editor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define D(x) cerr << #x << " is " << (x) << "\n";
#define ld long double

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const deque<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}

ll g(ll a,ll b){return b==0?a:g(b,a%b);}
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int t;
	scanf("%i",&t);
	while(t--){
		int n,m;
		scanf("%i %i",&n,&m);
		ll vreme=(ll)1500*max(n,m)/500000;
		ll beg=clock();
		vector<ll> vals(n);
		for(int i=0;i<n;i++)
			scanf("%lld",&vals[i]);
		vector<vector<int> > koji(n);
		for(int i=0;i<m;i++){
			int a,b;
			scanf("%i %i",&a,&b);
			a--;b--;
			koji[a].pb(b);
		}
		vector<bool> taken(n);
		ll gcd=0;
		int tr=0;
		while(clock()-beg<vreme||tr<250){
		  tr++;
			vector<int> undo;
			ll sum=0;
			for(int i=0;i<n;i++){
				if(rng()%max(2,n/40)==0){
					for(auto p:koji[i])
						if(!taken[p])
							taken[p]=1,undo.pb(p),sum+=vals[p];
				}
			}
			for(auto p:undo)
				taken[p]=0;
			gcd=g(gcd,sum);
		}
		printf("%lld\n",gcd);
	}
	return 0;
}


// Problem : C. Instant Noodles
// Contest : Codeforces Round #626 (Div. 1, based on Moscow Open Olympiad in Informatics)
// URL : https://codeforces.com/contest/1322/problem/C
// Memory Limit : 512 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cp-editor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define D(x) cerr << #x << " is " << (x) << "\n";
#define ld long double

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const deque<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}

ll g(ll a,ll b){return b==0?a:g(b,a%b);}
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int t;
	scanf("%i",&t);
	while(t--){
		int n,m;
		scanf("%i %i",&n,&m);
		ll vreme=(ll)1500*max(n,m)/500000;
		ll beg=clock();
		vector<ll> vals(n);
		for(int i=0;i<n;i++)
			scanf("%lld",&vals[i]);
		vector<vector<int> > koji(n);
		for(int i=0;i<m;i++){
			int a,b;
			scanf("%i %i",&a,&b);
			a--;b--;
			koji[a].pb(b);
		}
		vector<bool> taken(n);
		ll gcd=0;
		int tr=0;
		while(clock()-beg<vreme||tr<100){
		  tr++;
			vector<int> undo;
			ll sum=0;
			for(int i=0;i<n;i++){
				if(rng()%5==0){
					for(auto p:koji[i])
						if(!taken[p])
							taken[p]=1,undo.pb(p),sum+=vals[p];
				}
			}
			for(auto p:undo)
				taken[p]=0;
			gcd=g(gcd,sum);
		}
		printf("%lld\n",gcd);
	}
	return 0;
}


// Problem : C. Instant Noodles
// Contest : Codeforces Round #626 (Div. 1, based on Moscow Open Olympiad in Informatics)
// URL : https://codeforces.com/contest/1322/problem/C
// Memory Limit : 512 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cp-editor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define D(x) cerr << #x << " is " << (x) << "\n";
#define ld long double

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const deque<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}

ll g(ll a,ll b){return b==0?a:g(b,a%b);}
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int t;
	scanf("%i",&t);
	while(t--){
		int n,m;
		scanf("%i %i",&n,&m);
		ll vreme=(ll)1500*max(n,m)/500000;
		ll beg=clock();
		vector<ll> vals(n);
		for(int i=0;i<n;i++)
			scanf("%lld",&vals[i]);
		vector<vector<int> > koji(n);
		for(int i=0;i<m;i++){
			int a,b;
			scanf("%i %i",&a,&b);
			a--;b--;
			koji[a].pb(b);
		}
		vector<bool> taken(n);
		ll gcd=0;
		int tr=0;
		while(clock()-beg<vreme||tr<250){
		  tr++;
			vector<int> undo;
			ll sum=0;
			for(int i=0;i<n;i++){
				if(rng()%max(2,n/50)==0){
					for(auto p:koji[i])
						if(!taken[p])
							taken[p]=1,undo.pb(p),sum+=vals[p];
				}
			}
			for(auto p:undo)
				taken[p]=0;
			gcd=g(gcd,sum);
		}
		printf("%lld\n",gcd);
	}
	return 0;
}


// Problem : C. Instant Noodles
// Contest : Codeforces Round #626 (Div. 1, based on Moscow Open Olympiad in Informatics)
// URL : https://codeforces.com/contest/1322/problem/C
// Memory Limit : 512 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cp-editor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define D(x) cerr << #x << " is " << (x) << "\n";
#define ld long double

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const deque<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}

ll g(ll a,ll b){return b==0?a:g(b,a%b);}
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int t;
	scanf("%i",&t);
	while(t--){
		int n,m;
		scanf("%i %i",&n,&m);
		ll vreme=(ll)1800*max(n,m)/500000;
		ll beg=clock();
		vector<ll> vals(n);
		for(int i=0;i<n;i++)
			scanf("%lld",&vals[i]);
		vector<vector<int> > koji(n);
		for(int i=0;i<m;i++){
			int a,b;
			scanf("%i %i",&a,&b);
			a--;b--;
			koji[a].pb(b);
		}
		vector<bool> taken(n);
		ll gcd=0;
		int tr=0;
		while(clock()-beg<vreme||tr<100){
		  tr++;
			vector<int> undo;
			ll sum=0;
			for(int i=0;i<n;i++){
				if(rng()%2){
					for(auto p:koji[i])
						if(!taken[p])
							taken[p]=1,undo.pb(p),sum+=vals[p];
				}
			}
			for(auto p:undo)
				taken[p]=0;
			gcd=g(gcd,sum);
		}
		printf("%lld\n",gcd);
	}
	return 0;
}


// Problem : C. Instant Noodles
// Contest : Codeforces Round #626 (Div. 1, based on Moscow Open Olympiad in Informatics)
// URL : https://codeforces.com/contest/1322/problem/C
// Memory Limit : 512 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cp-editor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define D(x) cerr << #x << " is " << (x) << "\n";
#define ld long double

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const deque<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}

ll g(ll a,ll b){return b==0?a:g(b,a%b);}
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int t;
	scanf("%i",&t);
	while(t--){
		int n,m;
		scanf("%i %i",&n,&m);
		ll vreme=(ll)1500*max(n,m)/500000;
		ll beg=clock();
		vector<ll> vals(n);
		for(int i=0;i<n;i++)
			scanf("%lld",&vals[i]);
		vector<vector<int> > koji(n);
		for(int i=0;i<m;i++){
			int a,b;
			scanf("%i %i",&a,&b);
			a--;b--;
			koji[a].pb(b);
		}
		vector<bool> taken(n);
		ll gcd=0;
		int tr=0;
		while(clock()-beg<vreme||tr<300){
		  tr++;
			vector<int> undo;
			ll sum=0;
			for(int i=0;i<n;i++){
				if(rng()%3==0){
					for(auto p:koji[i])
						if(!taken[p])
							taken[p]=1,undo.pb(p),sum+=vals[p];
				}
			}
			for(auto p:undo)
				taken[p]=0;
			gcd=g(gcd,sum);
		}
		printf("%lld\n",gcd);
	}
	return 0;
}


// Problem : C. Instant Noodles
// Contest : Codeforces Round #626 (Div. 1, based on Moscow Open Olympiad in Informatics)
// URL : https://codeforces.com/contest/1322/problem/C
// Memory Limit : 512 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cp-editor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define D(x) cerr << #x << " is " << (x) << "\n";
#define ld long double

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const deque<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}

ll g(ll a,ll b){return b==0?a:g(b,a%b);}
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int t;
	scanf("%i",&t);
	while(t--){
		int n,m;
		scanf("%i %i",&n,&m);
		ll vreme=(ll)1500*max(n,m)/500000;
		ll beg=clock();
		vector<ll> vals(n);
		for(int i=0;i<n;i++)
			scanf("%lld",&vals[i]);
		vector<vector<int> > koji(n);
		for(int i=0;i<m;i++){
			int a,b;
			scanf("%i %i",&a,&b);
			a--;b--;
			koji[a].pb(b);
		}
		vector<bool> taken(n);
		ll gcd=0;
		int tr=0;
		while(clock()-beg<vreme||tr<200){
		  tr++;
			vector<int> undo;
			ll sum=0;
			for(int i=0;i<n;i++){
				if(rng()%max(2,n/1000)==0){
					for(auto p:koji[i])
						if(!taken[p])
							taken[p]=1,undo.pb(p),sum+=vals[p];
				}
			}
			for(auto p:undo)
				taken[p]=0;
			gcd=g(gcd,sum);
		}
		printf("%lld\n",gcd);
	}
	return 0;
}


// Problem : C. Instant Noodles
// Contest : Codeforces Round #626 (Div. 1, based on Moscow Open Olympiad in Informatics)
// URL : https://codeforces.com/contest/1322/problem/C
// Memory Limit : 512 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cp-editor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define D(x) cerr << #x << " is " << (x) << "\n";
#define ld long double

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const deque<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}

ll g(ll a,ll b){return b==0?a:g(b,a%b);}
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int t;
	scanf("%i",&t);
	while(t--){
		int n,m;
		scanf("%i %i",&n,&m);
		ll vreme=(ll)1500*max(n,m)/500000;

		ll beg=clock();
		vector<ll> vals(n);
		for(int i=0;i<n;i++)
			scanf("%lld",&vals[i]);
		vector<vector<int> > koji(n);
		for(int i=0;i<m;i++){
			int a,b;
			scanf("%i %i",&a,&b);
			a--;b--;
			koji[a].pb(b);
		}
		vector<bool> taken(n);
		ll gcd=0;
		int tr=0;
		while(clock()-beg<vreme||tr<10){
		  tr++;
			vector<int> undo;
			ll sum=0;
			for(int i=0;i<n;i++){
				if(rng()%2){
					for(auto p:koji[i])
						if(!taken[p])
							taken[p]=1,undo.pb(p),sum+=vals[p];
				}
			}
			for(auto p:undo)
				taken[p]=0;
			gcd=g(gcd,sum);
		}
		printf("%lld\n",gcd);
	}
	return 0;
}


// Problem : C. Instant Noodles
// Contest : Codeforces Round #626 (Div. 1, based on Moscow Open Olympiad in Informatics)
// URL : https://codeforces.com/contest/1322/problem/C
// Memory Limit : 512 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cp-editor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define D(x) cerr << #x << " is " << (x) << "\n";
#define ld long double

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const deque<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}

ll g(ll a,ll b){return b==0?a:g(b,a%b);}
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int t;
	scanf("%i",&t);
	while(t--){
		int n,m;
		scanf("%i %i",&n,&m);
		ll vreme=(ll)1500*max(n,m)/500000;
		ll beg=clock();
		vector<ll> vals(n);
		for(int i=0;i<n;i++)
			scanf("%lld",&vals[i]);
		vector<vector<int> > koji(n);
		for(int i=0;i<m;i++){
			int a,b;
			scanf("%i %i",&a,&b);
			a--;b--;
			koji[a].pb(b);
		}
		vector<bool> taken(n);
		ll gcd=0;
		int tr=0;
		while(clock()-beg<vreme||tr<200){
		  tr++;
			vector<int> undo;
			ll sum=0;
			for(int i=0;i<n;i++){
				if(rng()%max(2,n/30)==0){
					for(auto p:koji[i])
						if(!taken[p])
							taken[p]=1,undo.pb(p),sum+=vals[p];
				}
			}
			for(auto p:undo)
				taken[p]=0;
			gcd=g(gcd,sum);
		}
		printf("%lld\n",gcd);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
long long v[1<<20],o;
int main()
{
	int t,n,m,a,b;
	scanf("%i",&t);
	while(t--){
		scanf("%i%i",&n,&m);
		for(int i=0;i<n;i++)
			scanf("%lld",&v[i]);
		vector<vector<int> > k(n);
		for(int i=0;i<m;i++)
			scanf("%i%i",&a,&b),k[b-1].push_back(a-1);
		map<vector<int>,long long> s;
		for(int i=0;i<n;i++){
			if(k[i].empty())
				continue;
			sort(k[i].begin(),k[i].end());
			s[k[i]]+=v[i];
		}
		o=0;
		for(auto p:s)
			o=__gcd(o,p.second);
		printf("%lld\n",o);
	}
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll g(ll a,ll b){return b==0?a:g(b,a%b);}
int main()
{
	int t,n,m,a,b;
	cin>>t;
	while(t--){
		cin>>n>>m;
		vector<ll> v(n);
		for(int i=0;i<n;i++)
			cin>>v[i];
		vector<vector<int> > k(n);
		for(int i=0;i<m;i++){
			cin>>a>>b;
			k[b-1].push_back(a-1);
		}
		map<vector<int>,ll> sum;
		for(int i=0;i<n;i++){
			if(k[i].empty())
				continue;
			sort(k[i].begin(),k[i].end());
			sum[k[i]]+=v[i];
		}
		ll gcd=0;
		for(auto p:sum)
			gcd=g(gcd,p.second);
		cout<<gcd<<'\n';
	}
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll g(ll a,ll b){return b==0?a:g(b,a%b);}
int main()
{
	int t,n,m,a,b;
	scanf("%i",&t);
	while(t--){
		cin>>n>>m;
		vector<ll> v(n);
		for(int i=0;i<n;i++)
			cin>>v[i];
		vector<vector<int> > k(n);
		for(int i=0;i<m;i++)
			cin>>a>>b,a--,b--,k[b].push_back(a);
		map<vector<int>,ll> s;
		for(int i=0;i<n;i++){
			sort(k[i].begin(),k[i].end());
			s[k[i]]+=v[i];
		}
		ll o=0;
		for(auto p:s)
			o=g(o,p.second);
		cout<<o<<'\n';
	}
	return 0;
}


// Problem : C. Instant Noodles
// Contest : Codeforces Round #626 (Div. 1, based on Moscow Open Olympiad in Informatics)
// URL : https://codeforces.com/contest/1322/problem/C
// Memory Limit : 512 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cp-editor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define D(x) cerr << #x << " is " << (x) << "\n";
#define ld long double

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const deque<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}

ll g(ll a,ll b){return b==0?a:g(b,a%b);}
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int t;
	scanf("%i",&t);
	while(t--){
		int n,m;
		scanf("%i %i",&n,&m);
		ll vreme=(ll)1500*max(n,m)/500000;
		ll beg=clock();
		vector<ll> vals(n);
		for(int i=0;i<n;i++)
			scanf("%lld",&vals[i]);
		vector<vector<int> > koji(n);
		for(int i=0;i<m;i++){
			int a,b;
			scanf("%i %i",&a,&b);
			a--;b--;
			koji[a].pb(b);
		}
		vector<bool> taken(n);
		ll gcd=0;
		int tr=0;
		while(clock()-beg<vreme||tr<20){
		  tr++;
			vector<int> undo;
			ll sum=0;
			for(int i=0;i<n;i++){
				if(rng()%2){
					for(auto p:koji[i])
						if(!taken[p])
							taken[p]=1,undo.pb(p),sum+=vals[p];
				}
			}
			for(auto p:undo)
				taken[p]=0;
			gcd=g(gcd,sum);
		}
		printf("%lld\n",gcd);
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<queue>
#include<stack>
#include<bitset>
#include<functional>
#include<map>
#include<iomanip>
#include<limits>
#include<unordered_set>
#include<cmath>
#include <numeric>
#include <array>
#include <complex>
using namespace std;
//long long p = 998244353;
long long p = 1000000007;
#define int long long
#define ll long long
#define vel vector<ll>
#define vvel vector<vel>
#define rep(i,n) for(int i=0;i<n;i++)
#define sor(v) sort(v.begin(),v.end())
#define mmax(a,b) a=max(a,b)
#define mmin(a,b) a=min(a,b)
#define mkp make_pair
#define pin pair<int,int>
#define qin pair<pin,int>
#define V vector
#define Endl endl
#define veb vector<bool>
#define fcout cout << fixed << setprecision(15)
#define rev(s) reverse(s.begin(),s.end())
#define lower(h,val) (lower_bound(h.begin(),h.end(),val)-h.begin())
#define upper(h,val) (upper_bound(h.begin(),h.end(),val)-h.begin())
vel kai;
vel inv_kai;
int rui(int a, int n, int mod) {
	if (n == 0) { return 1 % mod; }
	int x = rui(a, n / 2, mod);
	x *= x; x %= mod;
	if (n % 2 == 1) { x *= a; x %= mod; }
	return x;
}
int root(int x, vel& pa) {
	if (pa[x] == -1) { return x; }
	int ans = root(pa[x], pa); pa[x] = ans;
	return ans;
}
bool mar(int x, int y, vel& pa) {
	x = root(x, pa);
	y = root(y, pa);
	if (x != y) { pa[x] = y; }
	return (x != y);
}
int gcd(int x, int y) {
	if (x < y) { return gcd(y, x); }
	if (y == 0) { return x; }
	return gcd(y, x % y);
}
int lcm(ll x, ll y) {
	x = abs(x); y = abs(y);
	return x * y / gcd(x, y);
}
long long modinv(long long a, long long m) {
	long long b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}
void make_kai(int max_kai) {
	kai = vel(max_kai, 1);
	inv_kai = kai;
	rep(i, max_kai - 1) {
		kai[i + 1] = kai[i] * (i + 1); kai[i + 1] %= p;
		inv_kai[i + 1] = modinv(kai[i + 1], p);
	}
}
int com(int n, int r) {
	if ((n < 0) || (r < 0) || (r > n)) { return 0; }
	int ans = (kai[n] * inv_kai[r]) % p;
	return (ans * inv_kai[n - r]) % p;
}
vel uni(vel x) {
	if (x.size() == 0) { return x; }
	sor(x);
	int n = x.size();
	vel ans(1, x[0]);
	for (int j = 1; j < n; j++) {
		if (x[j - 1] != x[j]) { ans.push_back(x[j]); }
	}
	x = ans;
	return x;
}
void pr(vel& v) {
	int n = v.size();
	if (n != 0) {
		cout << v[0];
		rep(i, n - 1) {
			cout << " " << v[i + 1];
		}
		cout << endl;
	}
}
vel dijk(V<V<pin>>& way, int st, int inf) {
	int n = way.size();
	vel dist(n, inf); dist[st] = 0;
	priority_queue<pin, vector<pin>, greater<pin>> pq;
	pq.push(mkp(0, st));
	veb is_checked(n, false);
	while (!pq.empty()) {
		pin x = pq.top(); pq.pop();
		int pot = x.second;
		if (!is_checked[pot]) {
			is_checked[pot] = true;
			for (auto y : way[pot]) {
				int nex_dist = x.first + y.second;
				int nex_pot = y.first;
				if (dist[nex_pot] > nex_dist) {
					dist[nex_pot] = nex_dist;
					pq.push(mkp(nex_dist, y.first));
				}
			}
		}
	}
	return dist;
}
vel mul(vel& a, vel& b) {
	int n = a.size();
	int m = b.size();
	vel ans(n + m - 1, 0);
	rep(i, n) {
		rep(j, m) {
			ans[i + j] += a[i] * b[j];
			ans[i + j] %= p;
		}
	}
	return ans;
}
vel rui_p(vel& a, int n) {
	if (n == 0) { return { 1 }; }
	vel qans = rui_p(a, n / 2);
	qans = mul(qans, qans);
	if (n % 2 == 1) {
		qans = mul(qans, a);
	}
	return qans;
}
bool is_prime(int n) {
	if (n == 0 || n == 1) { return false; }
	for (int i = 2; i * i <= n; i++) {
		if (n % i == 0) { return false; }
	}
	return true;
}
#define bs bitset<50>
void per(int& ans) {
	ans %= p;
	if (ans < 0) { ans += p; }
}
#define upperbound(v,val) upper_bound(v.begin(),v.end(),val)-v.begin()
#define lowerbound(v,val) lower_bound(v.begin(),v.end(),val)-v.begin()
#define mat V<V<pin>>
vvel disj_min(vel& v) {
	int n = v.size();
	vvel ret(22, vel(n));
	ret[0] = v;
	rep(i, 21) {
		rep(j, n) {
			int nex = j + (1 << i);
			if (nex < n) {
				ret[i + 1][j] = min(ret[i][j], ret[i][nex]);
			}
			else {
				ret[i + 1][j] = ret[i][j];
			}
		}
	}
	return ret;
}
vvel disj_max(vel& v) {
	int n = v.size();
	vvel ret(22, vel(n));
	ret[0] = v;
	rep(i, 21) {
		rep(j, n) {
			int nex = j + (1 << i);
			if (nex < n) {
				ret[i + 1][j] = max(ret[i][j], ret[i][nex]);
			}
			else {
				ret[i + 1][j] = ret[i][j];
			}
		}
	}
	return ret;
}
int find_min(vvel& dv, int l, int r) {
	int i = 21;
	while (l + (1 << i) > r) {
		i--;
	}
	return min(dv[i][l], dv[i][r - (1 << i)]);
}
int find_max(vvel& dv, int l, int r) {
	int i = 21;
	while (l + (1 << i) > r) {
		i--;
	}
	return max(dv[i][l], dv[i][r - (1 << i)]);
}
void pri(vel& v) {
	if (v.size() == 0) { return; }
	cout << v[0];
	rep(i, v.size() - 1) { cout << " " << v[i + 1]; }
	cout << endl;
	return;
}
vvel dbl(vel& v) {
	vvel ans(20, vel(v));
	int n = v.size();
	rep(i, 19) {
		rep(j, n) {
			ans[i + 1][j] = ans[i][ans[i][j]];
		}
	}
	return ans;
}
int lca(int s, int t, int diff, vvel& pa) {
	if (diff < 0) { return lca(t, s, -diff, pa); }
	rep(i, 19) {
		if ((diff & (1 << i)) != 0) {
			s = pa[i][s];
		}
	}
	for (int i = 19; i >= 0; i--) {
		if (pa[i][s] != pa[i][t]) {
			s = pa[i][s];
			t = pa[i][t];
		}
	}
	if (s != t) {
		s = pa[0][s];
	}
	return s;
}
#define d double
d dist(d x1, d y1, d x2, d y2) {
	return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int sz = 1024 * 1024;
vel bit(2 * sz, 0);
vel bit2(2 * sz, 0);
void add(int a, int w) {
	a += sz; bit[a] += w; bit2[a] = bit[a]; a /= 2;
	while (a != 0) {
		bit[a] += w;
		bit2[a] = max(bit2[2 * a], bit[2 * a] + max(bit2[2 * a + 1], (int)0));
		a /= 2;
	}
}
#define vveb V<veb>
signed main() {
	int t; cin >> t;
	rep(_, t) {
		int n = 0;int m=0;
		scanf("%ld", &n);
		scanf("%ld", &m);
		vel pp = { p,p + 2,11111101 };
		int sz = pp.size();
		vel a(n); vvel le(n, vel(sz+1,0));
		vvel qle(n, vel(sz,0));
		rep(i, n) { cin >> a[i]; le[i][3] = i; }
		int ba = 3242123;
		rep(i, m) {
			int u, v; cin >> u >> v; u--; v--;
			rep(j,sz) {
				int p1 = pp[j];
				le[v][j] += rui(ba, u, p1); le[v][j] %= p1;
				qle[v][j] = le[v][j];
			}
		}
		sor(le);
		sor(qle);
		int ans = 0;
		int nsum = 0;
		vel ze(sz,0);
		rep(i, n) {
			if (qle[i] != ze) {
				nsum += a[le[i][sz]];
				if (i==n-1||qle[i] != qle[i + 1]) { ans = gcd(ans, nsum); nsum = 0; }
			}
		}
		cout <<ans<<"/n";
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<queue>
#include<stack>
#include<bitset>
#include<functional>
#include<map>
#include<iomanip>
#include<limits>
#include<unordered_set>
#include<cmath>
#include <numeric>
#include <array>
#include <complex>
using namespace std;
//long long p = 998244353;
long long p = 1000000007;
#define int long long
#define ll long long
#define vel vector<ll>
#define vvel vector<vel>
#define rep(i,n) for(int i=0;i<n;i++)
#define sor(v) sort(v.begin(),v.end())
#define mmax(a,b) a=max(a,b)
#define mmin(a,b) a=min(a,b)
#define mkp make_pair
#define pin pair<int,int>
#define qin pair<pin,int>
#define V vector
#define Endl endl
#define veb vector<bool>
#define fcout cout << fixed << setprecision(15)
#define rev(s) reverse(s.begin(),s.end())
#define lower(h,val) (lower_bound(h.begin(),h.end(),val)-h.begin())
#define upper(h,val) (upper_bound(h.begin(),h.end(),val)-h.begin())
vel kai;
vel inv_kai;
int rui(int a, int n, int mod) {
	if (n == 0) { return 1 % mod; }
	int x = rui(a, n / 2, mod);
	x *= x; x %= mod;
	if (n % 2 == 1) { x *= a; x %= mod; }
	return x;
}
int root(int x, vel& pa) {
	if (pa[x] == -1) { return x; }
	int ans = root(pa[x], pa); pa[x] = ans;
	return ans;
}
bool mar(int x, int y, vel& pa) {
	x = root(x, pa);
	y = root(y, pa);
	if (x != y) { pa[x] = y; }
	return (x != y);
}
int gcd(int x, int y) {
	if (x < y) { return gcd(y, x); }
	if (y == 0) { return x; }
	return gcd(y, x % y);
}
int lcm(ll x, ll y) {
	x = abs(x); y = abs(y);
	return x * y / gcd(x, y);
}
long long modinv(long long a, long long m) {
	long long b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}
void make_kai(int max_kai) {
	kai = vel(max_kai, 1);
	inv_kai = kai;
	rep(i, max_kai - 1) {
		kai[i + 1] = kai[i] * (i + 1); kai[i + 1] %= p;
		inv_kai[i + 1] = modinv(kai[i + 1], p);
	}
}
int com(int n, int r) {
	if ((n < 0) || (r < 0) || (r > n)) { return 0; }
	int ans = (kai[n] * inv_kai[r]) % p;
	return (ans * inv_kai[n - r]) % p;
}
vel uni(vel x) {
	if (x.size() == 0) { return x; }
	sor(x);
	int n = x.size();
	vel ans(1, x[0]);
	for (int j = 1; j < n; j++) {
		if (x[j - 1] != x[j]) { ans.push_back(x[j]); }
	}
	x = ans;
	return x;
}
void pr(vel& v) {
	int n = v.size();
	if (n != 0) {
		cout << v[0];
		rep(i, n - 1) {
			cout << " " << v[i + 1];
		}
		cout << endl;
	}
}
vel dijk(V<V<pin>>& way, int st, int inf) {
	int n = way.size();
	vel dist(n, inf); dist[st] = 0;
	priority_queue<pin, vector<pin>, greater<pin>> pq;
	pq.push(mkp(0, st));
	veb is_checked(n, false);
	while (!pq.empty()) {
		pin x = pq.top(); pq.pop();
		int pot = x.second;
		if (!is_checked[pot]) {
			is_checked[pot] = true;
			for (auto y : way[pot]) {
				int nex_dist = x.first + y.second;
				int nex_pot = y.first;
				if (dist[nex_pot] > nex_dist) {
					dist[nex_pot] = nex_dist;
					pq.push(mkp(nex_dist, y.first));
				}
			}
		}
	}
	return dist;
}
vel mul(vel& a, vel& b) {
	int n = a.size();
	int m = b.size();
	vel ans(n + m - 1, 0);
	rep(i, n) {
		rep(j, m) {
			ans[i + j] += a[i] * b[j];
			ans[i + j] %= p;
		}
	}
	return ans;
}
vel rui_p(vel& a, int n) {
	if (n == 0) { return { 1 }; }
	vel qans = rui_p(a, n / 2);
	qans = mul(qans, qans);
	if (n % 2 == 1) {
		qans = mul(qans, a);
	}
	return qans;
}
bool is_prime(int n) {
	if (n == 0 || n == 1) { return false; }
	for (int i = 2; i * i <= n; i++) {
		if (n % i == 0) { return false; }
	}
	return true;
}
#define bs bitset<50>
void per(int& ans) {
	ans %= p;
	if (ans < 0) { ans += p; }
}
#define upperbound(v,val) upper_bound(v.begin(),v.end(),val)-v.begin()
#define lowerbound(v,val) lower_bound(v.begin(),v.end(),val)-v.begin()
#define mat V<V<pin>>
vvel disj_min(vel& v) {
	int n = v.size();
	vvel ret(22, vel(n));
	ret[0] = v;
	rep(i, 21) {
		rep(j, n) {
			int nex = j + (1 << i);
			if (nex < n) {
				ret[i + 1][j] = min(ret[i][j], ret[i][nex]);
			}
			else {
				ret[i + 1][j] = ret[i][j];
			}
		}
	}
	return ret;
}
vvel disj_max(vel& v) {
	int n = v.size();
	vvel ret(22, vel(n));
	ret[0] = v;
	rep(i, 21) {
		rep(j, n) {
			int nex = j + (1 << i);
			if (nex < n) {
				ret[i + 1][j] = max(ret[i][j], ret[i][nex]);
			}
			else {
				ret[i + 1][j] = ret[i][j];
			}
		}
	}
	return ret;
}
int find_min(vvel& dv, int l, int r) {
	int i = 21;
	while (l + (1 << i) > r) {
		i--;
	}
	return min(dv[i][l], dv[i][r - (1 << i)]);
}
int find_max(vvel& dv, int l, int r) {
	int i = 21;
	while (l + (1 << i) > r) {
		i--;
	}
	return max(dv[i][l], dv[i][r - (1 << i)]);
}
void pri(vel& v) {
	if (v.size() == 0) { return; }
	cout << v[0];
	rep(i, v.size() - 1) { cout << " " << v[i + 1]; }
	cout << endl;
	return;
}
vvel dbl(vel& v) {
	vvel ans(20, vel(v));
	int n = v.size();
	rep(i, 19) {
		rep(j, n) {
			ans[i + 1][j] = ans[i][ans[i][j]];
		}
	}
	return ans;
}
int lca(int s, int t, int diff, vvel& pa) {
	if (diff < 0) { return lca(t, s, -diff, pa); }
	rep(i, 19) {
		if ((diff & (1 << i)) != 0) {
			s = pa[i][s];
		}
	}
	for (int i = 19; i >= 0; i--) {
		if (pa[i][s] != pa[i][t]) {
			s = pa[i][s];
			t = pa[i][t];
		}
	}
	if (s != t) {
		s = pa[0][s];
	}
	return s;
}
#define d double
d dist(d x1, d y1, d x2, d y2) {
	return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int sz = 1024 * 1024;
vel bit(2 * sz, 0);
vel bit2(2 * sz, 0);
void add(int a, int w) {
	a += sz; bit[a] += w; bit2[a] = bit[a]; a /= 2;
	while (a != 0) {
		bit[a] += w;
		bit2[a] = max(bit2[2 * a], bit[2 * a] + max(bit2[2 * a + 1], (int)0));
		a /= 2;
	}
}
#define vveb V<veb>
signed main() {
	int t; cin >> t;
	rep(_, t) {
		int n = 0;int m=0;
		scanf("%ld", &n);
		scanf("%ld", &m);
		vel pp = { p,p + 2,11111101 };
		int sz = pp.size();
		vel a(n); vvel le(n, vel(sz+1,0));
		vvel qle(n, vel(sz,0));
		rep(i, n) { cin >> a[i]; le[i][3] = i; }
		int ba = 3242123;
		rep(i, m) {
			int u, v; cin >> u >> v; u--; v--;
			rep(j,sz) {
				int p1 = pp[j];
				le[v][j] += rui(ba, u, p1); le[v][j] %= p1;
				qle[v][j] = le[v][j];
			}
		}
		sor(le);
		sor(qle);
		int ans = 0;
		int nsum = 0;
		vel ze(sz,0);
		rep(i, n) {
			if (qle[i] != ze) {
				nsum += a[le[i][sz]];
				if (i==n-1||qle[i] != qle[i + 1]) { ans = gcd(ans, nsum); nsum = 0; }
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<queue>
#include<stack>
#include<bitset>
#include<functional>
#include<map>
#include<iomanip>
#include<limits>
#include<unordered_set>
#include<cmath>
#include <numeric>
#include <array>
#include <complex>
using namespace std;
//long long p = 998244353;
long long p = 1000000007;
#define int long long
#define ll long long
#define vel vector<ll>
#define vvel vector<vel>
#define rep(i,n) for(int i=0;i<n;i++)
#define sor(v) sort(v.begin(),v.end())
#define mmax(a,b) a=max(a,b)
#define mmin(a,b) a=min(a,b)
#define mkp make_pair
#define pin pair<int,int>
#define qin pair<pin,int>
#define V vector
#define Endl endl
#define veb vector<bool>
#define fcout cout << fixed << setprecision(15)
#define rev(s) reverse(s.begin(),s.end())
#define lower(h,val) (lower_bound(h.begin(),h.end(),val)-h.begin())
#define upper(h,val) (upper_bound(h.begin(),h.end(),val)-h.begin())
vel kai;
vel inv_kai;
int rui(int a, int n, int mod) {
	if (n == 0) { return 1 % mod; }
	int x = rui(a, n / 2, mod);
	x *= x; x %= mod;
	if (n % 2 == 1) { x *= a; x %= mod; }
	return x;
}
int root(int x, vel& pa) {
	if (pa[x] == -1) { return x; }
	int ans = root(pa[x], pa); pa[x] = ans;
	return ans;
}
bool mar(int x, int y, vel& pa) {
	x = root(x, pa);
	y = root(y, pa);
	if (x != y) { pa[x] = y; }
	return (x != y);
}
int gcd(int x, int y) {
	if (x < y) { return gcd(y, x); }
	if (y == 0) { return x; }
	return gcd(y, x % y);
}
int lcm(ll x, ll y) {
	x = abs(x); y = abs(y);
	return x * y / gcd(x, y);
}
long long modinv(long long a, long long m) {
	long long b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}
void make_kai(int max_kai) {
	kai = vel(max_kai, 1);
	inv_kai = kai;
	rep(i, max_kai - 1) {
		kai[i + 1] = kai[i] * (i + 1); kai[i + 1] %= p;
		inv_kai[i + 1] = modinv(kai[i + 1], p);
	}
}
int com(int n, int r) {
	if ((n < 0) || (r < 0) || (r > n)) { return 0; }
	int ans = (kai[n] * inv_kai[r]) % p;
	return (ans * inv_kai[n - r]) % p;
}
vel uni(vel x) {
	if (x.size() == 0) { return x; }
	sor(x);
	int n = x.size();
	vel ans(1, x[0]);
	for (int j = 1; j < n; j++) {
		if (x[j - 1] != x[j]) { ans.push_back(x[j]); }
	}
	x = ans;
	return x;
}
void pr(vel& v) {
	int n = v.size();
	if (n != 0) {
		cout << v[0];
		rep(i, n - 1) {
			cout << " " << v[i + 1];
		}
		cout << endl;
	}
}
vel dijk(V<V<pin>>& way, int st, int inf) {
	int n = way.size();
	vel dist(n, inf); dist[st] = 0;
	priority_queue<pin, vector<pin>, greater<pin>> pq;
	pq.push(mkp(0, st));
	veb is_checked(n, false);
	while (!pq.empty()) {
		pin x = pq.top(); pq.pop();
		int pot = x.second;
		if (!is_checked[pot]) {
			is_checked[pot] = true;
			for (auto y : way[pot]) {
				int nex_dist = x.first + y.second;
				int nex_pot = y.first;
				if (dist[nex_pot] > nex_dist) {
					dist[nex_pot] = nex_dist;
					pq.push(mkp(nex_dist, y.first));
				}
			}
		}
	}
	return dist;
}
vel mul(vel& a, vel& b) {
	int n = a.size();
	int m = b.size();
	vel ans(n + m - 1, 0);
	rep(i, n) {
		rep(j, m) {
			ans[i + j] += a[i] * b[j];
			ans[i + j] %= p;
		}
	}
	return ans;
}
vel rui_p(vel& a, int n) {
	if (n == 0) { return { 1 }; }
	vel qans = rui_p(a, n / 2);
	qans = mul(qans, qans);
	if (n % 2 == 1) {
		qans = mul(qans, a);
	}
	return qans;
}
bool is_prime(int n) {
	if (n == 0 || n == 1) { return false; }
	for (int i = 2; i * i <= n; i++) {
		if (n % i == 0) { return false; }
	}
	return true;
}
#define bs bitset<50>
void per(int& ans) {
	ans %= p;
	if (ans < 0) { ans += p; }
}
#define upperbound(v,val) upper_bound(v.begin(),v.end(),val)-v.begin()
#define lowerbound(v,val) lower_bound(v.begin(),v.end(),val)-v.begin()
#define mat V<V<pin>>
vvel disj_min(vel& v) {
	int n = v.size();
	vvel ret(22, vel(n));
	ret[0] = v;
	rep(i, 21) {
		rep(j, n) {
			int nex = j + (1 << i);
			if (nex < n) {
				ret[i + 1][j] = min(ret[i][j], ret[i][nex]);
			}
			else {
				ret[i + 1][j] = ret[i][j];
			}
		}
	}
	return ret;
}
vvel disj_max(vel& v) {
	int n = v.size();
	vvel ret(22, vel(n));
	ret[0] = v;
	rep(i, 21) {
		rep(j, n) {
			int nex = j + (1 << i);
			if (nex < n) {
				ret[i + 1][j] = max(ret[i][j], ret[i][nex]);
			}
			else {
				ret[i + 1][j] = ret[i][j];
			}
		}
	}
	return ret;
}
int find_min(vvel& dv, int l, int r) {
	int i = 21;
	while (l + (1 << i) > r) {
		i--;
	}
	return min(dv[i][l], dv[i][r - (1 << i)]);
}
int find_max(vvel& dv, int l, int r) {
	int i = 21;
	while (l + (1 << i) > r) {
		i--;
	}
	return max(dv[i][l], dv[i][r - (1 << i)]);
}
void pri(vel& v) {
	if (v.size() == 0) { return; }
	cout << v[0];
	rep(i, v.size() - 1) { cout << " " << v[i + 1]; }
	cout << endl;
	return;
}
vvel dbl(vel& v) {
	vvel ans(20, vel(v));
	int n = v.size();
	rep(i, 19) {
		rep(j, n) {
			ans[i + 1][j] = ans[i][ans[i][j]];
		}
	}
	return ans;
}
int lca(int s, int t, int diff, vvel& pa) {
	if (diff < 0) { return lca(t, s, -diff, pa); }
	rep(i, 19) {
		if ((diff & (1 << i)) != 0) {
			s = pa[i][s];
		}
	}
	for (int i = 19; i >= 0; i--) {
		if (pa[i][s] != pa[i][t]) {
			s = pa[i][s];
			t = pa[i][t];
		}
	}
	if (s != t) {
		s = pa[0][s];
	}
	return s;
}
#define d double
d dist(d x1, d y1, d x2, d y2) {
	return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int sz = 1024 * 1024;
vel bit(2 * sz, 0);
vel bit2(2 * sz, 0);
void add(int a, int w) {
	a += sz; bit[a] += w; bit2[a] = bit[a]; a /= 2;
	while (a != 0) {
		bit[a] += w;
		bit2[a] = max(bit2[2 * a], bit[2 * a] + max(bit2[2 * a + 1], (int)0));
		a /= 2;
	}
}
#define vveb V<veb>
signed main() {
	int t; cin >> t;
	vel pp = { p,p + 2,11111101 };
	int inf = 500000;
	int ba = 3242123;
	vvel rui(3, vel(inf + 1, 1));
	rep(i, 3) {
		rep(j, inf) {
			rui[i][j + 1] = rui[i][j] * ba;
			rui[i][j + 1] %= p;
		}
	}
	rep(_, t) {
		int n = 0; int m = 0;
		scanf("%ld", &n);
		scanf("%ld", &m);
		int sz = pp.size();
		vel a(n); vvel le(n, vel(sz + 1, 0));
		rep(i, n) { cin >> a[i]; le[i][3] = i; }
		rep(i, m) {
			int u, v; cin >> u >> v; u--; v--;
			rep(j, sz) {
				int p1 = pp[j];
				le[v][j] += rui[j][u]; le[v][j] %= p1;
			}
		}
		sor(le);
		vvel qle(n, vel(sz, 0));
		rep(i, n) {
			rep(j, sz) {
				qle[i][j] = le[i][j];
			}
		}
		int ans = 0;
		int nsum = 0;
		vel ze(sz, 0);
		rep(i, n) {
			if (qle[i] != ze) {
				nsum += a[le[i][sz]];
				if (i == n - 1 || qle[i] != qle[i + 1]) { ans = gcd(ans, nsum); nsum = 0; }
			}
		}
		printf("%lld\n", ans);
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<queue>
#include<stack>
#include<bitset>
#include<functional>
#include<map>
#include<iomanip>
#include<limits>
#include<unordered_set>
#include<cmath>
#include <numeric>
#include <array>
#include <complex>
using namespace std;
//long long p = 998244353;
long long p = 1000000007;
#define int long long
#define ll long long
#define vel vector<ll>
#define vvel vector<vel>
#define rep(i,n) for(int i=0;i<n;i++)
#define sor(v) sort(v.begin(),v.end())
#define mmax(a,b) a=max(a,b)
#define mmin(a,b) a=min(a,b)
#define mkp make_pair
#define pin pair<int,int>
#define qin pair<pin,int>
#define V vector
#define Endl endl
#define veb vector<bool>
#define fcout cout << fixed << setprecision(15)
#define rev(s) reverse(s.begin(),s.end())
#define lower(h,val) (lower_bound(h.begin(),h.end(),val)-h.begin())
#define upper(h,val) (upper_bound(h.begin(),h.end(),val)-h.begin())
vel kai;
vel inv_kai;
int rui(int a, int n, int mod) {
	if (n == 0) { return 1 % mod; }
	int x = rui(a, n / 2, mod);
	x *= x; x %= mod;
	if (n % 2 == 1) { x *= a; x %= mod; }
	return x;
}
int root(int x, vel& pa) {
	if (pa[x] == -1) { return x; }
	int ans = root(pa[x], pa); pa[x] = ans;
	return ans;
}
bool mar(int x, int y, vel& pa) {
	x = root(x, pa);
	y = root(y, pa);
	if (x != y) { pa[x] = y; }
	return (x != y);
}
int gcd(int x, int y) {
	if (x < y) { return gcd(y, x); }
	if (y == 0) { return x; }
	return gcd(y, x % y);
}
int lcm(ll x, ll y) {
	x = abs(x); y = abs(y);
	return x * y / gcd(x, y);
}
long long modinv(long long a, long long m) {
	long long b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}
void make_kai(int max_kai) {
	kai = vel(max_kai, 1);
	inv_kai = kai;
	rep(i, max_kai - 1) {
		kai[i + 1] = kai[i] * (i + 1); kai[i + 1] %= p;
		inv_kai[i + 1] = modinv(kai[i + 1], p);
	}
}
int com(int n, int r) {
	if ((n < 0) || (r < 0) || (r > n)) { return 0; }
	int ans = (kai[n] * inv_kai[r]) % p;
	return (ans * inv_kai[n - r]) % p;
}
vel uni(vel x) {
	if (x.size() == 0) { return x; }
	sor(x);
	int n = x.size();
	vel ans(1, x[0]);
	for (int j = 1; j < n; j++) {
		if (x[j - 1] != x[j]) { ans.push_back(x[j]); }
	}
	x = ans;
	return x;
}
void pr(vel& v) {
	int n = v.size();
	if (n != 0) {
		cout << v[0];
		rep(i, n - 1) {
			cout << " " << v[i + 1];
		}
		cout << endl;
	}
}
vel dijk(V<V<pin>>& way, int st, int inf) {
	int n = way.size();
	vel dist(n, inf); dist[st] = 0;
	priority_queue<pin, vector<pin>, greater<pin>> pq;
	pq.push(mkp(0, st));
	veb is_checked(n, false);
	while (!pq.empty()) {
		pin x = pq.top(); pq.pop();
		int pot = x.second;
		if (!is_checked[pot]) {
			is_checked[pot] = true;
			for (auto y : way[pot]) {
				int nex_dist = x.first + y.second;
				int nex_pot = y.first;
				if (dist[nex_pot] > nex_dist) {
					dist[nex_pot] = nex_dist;
					pq.push(mkp(nex_dist, y.first));
				}
			}
		}
	}
	return dist;
}
vel mul(vel& a, vel& b) {
	int n = a.size();
	int m = b.size();
	vel ans(n + m - 1, 0);
	rep(i, n) {
		rep(j, m) {
			ans[i + j] += a[i] * b[j];
			ans[i + j] %= p;
		}
	}
	return ans;
}
vel rui_p(vel& a, int n) {
	if (n == 0) { return { 1 }; }
	vel qans = rui_p(a, n / 2);
	qans = mul(qans, qans);
	if (n % 2 == 1) {
		qans = mul(qans, a);
	}
	return qans;
}
bool is_prime(int n) {
	if (n == 0 || n == 1) { return false; }
	for (int i = 2; i * i <= n; i++) {
		if (n % i == 0) { return false; }
	}
	return true;
}
#define bs bitset<50>
void per(int& ans) {
	ans %= p;
	if (ans < 0) { ans += p; }
}
#define upperbound(v,val) upper_bound(v.begin(),v.end(),val)-v.begin()
#define lowerbound(v,val) lower_bound(v.begin(),v.end(),val)-v.begin()
#define mat V<V<pin>>
vvel disj_min(vel& v) {
	int n = v.size();
	vvel ret(22, vel(n));
	ret[0] = v;
	rep(i, 21) {
		rep(j, n) {
			int nex = j + (1 << i);
			if (nex < n) {
				ret[i + 1][j] = min(ret[i][j], ret[i][nex]);
			}
			else {
				ret[i + 1][j] = ret[i][j];
			}
		}
	}
	return ret;
}
vvel disj_max(vel& v) {
	int n = v.size();
	vvel ret(22, vel(n));
	ret[0] = v;
	rep(i, 21) {
		rep(j, n) {
			int nex = j + (1 << i);
			if (nex < n) {
				ret[i + 1][j] = max(ret[i][j], ret[i][nex]);
			}
			else {
				ret[i + 1][j] = ret[i][j];
			}
		}
	}
	return ret;
}
int find_min(vvel& dv, int l, int r) {
	int i = 21;
	while (l + (1 << i) > r) {
		i--;
	}
	return min(dv[i][l], dv[i][r - (1 << i)]);
}
int find_max(vvel& dv, int l, int r) {
	int i = 21;
	while (l + (1 << i) > r) {
		i--;
	}
	return max(dv[i][l], dv[i][r - (1 << i)]);
}
void pri(vel& v) {
	if (v.size() == 0) { return; }
	cout << v[0];
	rep(i, v.size() - 1) { cout << " " << v[i + 1]; }
	cout << endl;
	return;
}
vvel dbl(vel& v) {
	vvel ans(20, vel(v));
	int n = v.size();
	rep(i, 19) {
		rep(j, n) {
			ans[i + 1][j] = ans[i][ans[i][j]];
		}
	}
	return ans;
}
int lca(int s, int t, int diff, vvel& pa) {
	if (diff < 0) { return lca(t, s, -diff, pa); }
	rep(i, 19) {
		if ((diff & (1 << i)) != 0) {
			s = pa[i][s];
		}
	}
	for (int i = 19; i >= 0; i--) {
		if (pa[i][s] != pa[i][t]) {
			s = pa[i][s];
			t = pa[i][t];
		}
	}
	if (s != t) {
		s = pa[0][s];
	}
	return s;
}
#define d double
d dist(d x1, d y1, d x2, d y2) {
	return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int sz = 1024 * 1024;
vel bit(2 * sz, 0);
vel bit2(2 * sz, 0);
void add(int a, int w) {
	a += sz; bit[a] += w; bit2[a] = bit[a]; a /= 2;
	while (a != 0) {
		bit[a] += w;
		bit2[a] = max(bit2[2 * a], bit[2 * a] + max(bit2[2 * a + 1], (int)0));
		a /= 2;
	}
}
#define vveb V<veb>
signed main() {
	int t; cin >> t;
	rep(_, t) {
		int n = 0;int m=0;
		scanf("%ld", &n);
		scanf("%ld", &m);
		vel pp = { p,p + 2,11111101 };
		int sz = pp.size();
		vel a(n); vvel le(n, vel(sz+1,0));
		vvel qle(n, vel(sz,0));
		rep(i, n) { cin >> a[i]; le[i][3] = i; }
		int ba = 3242123;
		rep(i, m) {
			int u, v; cin >> u >> v; u--; v--;
			rep(j,sz) {
				int p1 = pp[j];
				le[v][j] += rui(ba, u, p1); le[v][j] %= p1;
				qle[v][j] = le[v][j];
			}
		}
		sor(le);
		sor(qle);
		int ans = 0;
		int nsum = 0;
		vel ze(sz,0);
		rep(i, n) {
			if (qle[i] != ze) {
				nsum += a[le[i][sz]];
				if (i==n-1||qle[i] != qle[i + 1]) { ans = gcd(ans, nsum); nsum = 0; }
			}
		}
		printf("%ld\n",ans);
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<queue>
#include<stack>
#include<bitset>
#include<functional>
#include<map>
#include<iomanip>
#include<limits>
#include<unordered_set>
#include<cmath>
#include <numeric>
#include <array>
#include <complex>
using namespace std;
//long long p = 998244353;
long long p = 1000000007;
#define int long long
#define ll long long
#define vel vector<ll>
#define vvel vector<vel>
#define rep(i,n) for(int i=0;i<n;i++)
#define sor(v) sort(v.begin(),v.end())
#define mmax(a,b) a=max(a,b)
#define mmin(a,b) a=min(a,b)
#define mkp make_pair
#define pin pair<int,int>
#define qin pair<pin,int>
#define V vector
#define Endl endl
#define veb vector<bool>
#define fcout cout << fixed << setprecision(15)
#define rev(s) reverse(s.begin(),s.end())
#define lower(h,val) (lower_bound(h.begin(),h.end(),val)-h.begin())
#define upper(h,val) (upper_bound(h.begin(),h.end(),val)-h.begin())
vel kai;
vel inv_kai;
int rui(int a, int n, int mod) {
	if (n == 0) { return 1 % mod; }
	int x = rui(a, n / 2, mod);
	x *= x; x %= mod;
	if (n % 2 == 1) { x *= a; x %= mod; }
	return x;
}
int root(int x, vel& pa) {
	if (pa[x] == -1) { return x; }
	int ans = root(pa[x], pa); pa[x] = ans;
	return ans;
}
bool mar(int x, int y, vel& pa) {
	x = root(x, pa);
	y = root(y, pa);
	if (x != y) { pa[x] = y; }
	return (x != y);
}
int gcd(int x, int y) {
	if (x < y) { return gcd(y, x); }
	if (y == 0) { return x; }
	return gcd(y, x % y);
}
int lcm(ll x, ll y) {
	x = abs(x); y = abs(y);
	return x * y / gcd(x, y);
}
long long modinv(long long a, long long m) {
	long long b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}
void make_kai(int max_kai) {
	kai = vel(max_kai, 1);
	inv_kai = kai;
	rep(i, max_kai - 1) {
		kai[i + 1] = kai[i] * (i + 1); kai[i + 1] %= p;
		inv_kai[i + 1] = modinv(kai[i + 1], p);
	}
}
int com(int n, int r) {
	if ((n < 0) || (r < 0) || (r > n)) { return 0; }
	int ans = (kai[n] * inv_kai[r]) % p;
	return (ans * inv_kai[n - r]) % p;
}
vel uni(vel x) {
	if (x.size() == 0) { return x; }
	sor(x);
	int n = x.size();
	vel ans(1, x[0]);
	for (int j = 1; j < n; j++) {
		if (x[j - 1] != x[j]) { ans.push_back(x[j]); }
	}
	x = ans;
	return x;
}
void pr(vel& v) {
	int n = v.size();
	if (n != 0) {
		cout << v[0];
		rep(i, n - 1) {
			cout << " " << v[i + 1];
		}
		cout << endl;
	}
}
vel dijk(V<V<pin>>& way, int st, int inf) {
	int n = way.size();
	vel dist(n, inf); dist[st] = 0;
	priority_queue<pin, vector<pin>, greater<pin>> pq;
	pq.push(mkp(0, st));
	veb is_checked(n, false);
	while (!pq.empty()) {
		pin x = pq.top(); pq.pop();
		int pot = x.second;
		if (!is_checked[pot]) {
			is_checked[pot] = true;
			for (auto y : way[pot]) {
				int nex_dist = x.first + y.second;
				int nex_pot = y.first;
				if (dist[nex_pot] > nex_dist) {
					dist[nex_pot] = nex_dist;
					pq.push(mkp(nex_dist, y.first));
				}
			}
		}
	}
	return dist;
}
vel mul(vel& a, vel& b) {
	int n = a.size();
	int m = b.size();
	vel ans(n + m - 1, 0);
	rep(i, n) {
		rep(j, m) {
			ans[i + j] += a[i] * b[j];
			ans[i + j] %= p;
		}
	}
	return ans;
}
vel rui_p(vel& a, int n) {
	if (n == 0) { return { 1 }; }
	vel qans = rui_p(a, n / 2);
	qans = mul(qans, qans);
	if (n % 2 == 1) {
		qans = mul(qans, a);
	}
	return qans;
}
bool is_prime(int n) {
	if (n == 0 || n == 1) { return false; }
	for (int i = 2; i * i <= n; i++) {
		if (n % i == 0) { return false; }
	}
	return true;
}
#define bs bitset<50>
void per(int& ans) {
	ans %= p;
	if (ans < 0) { ans += p; }
}
#define upperbound(v,val) upper_bound(v.begin(),v.end(),val)-v.begin()
#define lowerbound(v,val) lower_bound(v.begin(),v.end(),val)-v.begin()
#define mat V<V<pin>>
vvel disj_min(vel& v) {
	int n = v.size();
	vvel ret(22, vel(n));
	ret[0] = v;
	rep(i, 21) {
		rep(j, n) {
			int nex = j + (1 << i);
			if (nex < n) {
				ret[i + 1][j] = min(ret[i][j], ret[i][nex]);
			}
			else {
				ret[i + 1][j] = ret[i][j];
			}
		}
	}
	return ret;
}
vvel disj_max(vel& v) {
	int n = v.size();
	vvel ret(22, vel(n));
	ret[0] = v;
	rep(i, 21) {
		rep(j, n) {
			int nex = j + (1 << i);
			if (nex < n) {
				ret[i + 1][j] = max(ret[i][j], ret[i][nex]);
			}
			else {
				ret[i + 1][j] = ret[i][j];
			}
		}
	}
	return ret;
}
int find_min(vvel& dv, int l, int r) {
	int i = 21;
	while (l + (1 << i) > r) {
		i--;
	}
	return min(dv[i][l], dv[i][r - (1 << i)]);
}
int find_max(vvel& dv, int l, int r) {
	int i = 21;
	while (l + (1 << i) > r) {
		i--;
	}
	return max(dv[i][l], dv[i][r - (1 << i)]);
}
void pri(vel& v) {
	if (v.size() == 0) { return; }
	cout << v[0];
	rep(i, v.size() - 1) { cout << " " << v[i + 1]; }
	cout << endl;
	return;
}
vvel dbl(vel& v) {
	vvel ans(20, vel(v));
	int n = v.size();
	rep(i, 19) {
		rep(j, n) {
			ans[i + 1][j] = ans[i][ans[i][j]];
		}
	}
	return ans;
}
int lca(int s, int t, int diff, vvel& pa) {
	if (diff < 0) { return lca(t, s, -diff, pa); }
	rep(i, 19) {
		if ((diff & (1 << i)) != 0) {
			s = pa[i][s];
		}
	}
	for (int i = 19; i >= 0; i--) {
		if (pa[i][s] != pa[i][t]) {
			s = pa[i][s];
			t = pa[i][t];
		}
	}
	if (s != t) {
		s = pa[0][s];
	}
	return s;
}
#define d double
d dist(d x1, d y1, d x2, d y2) {
	return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int sz = 1024 * 1024;
vel bit(2 * sz, 0);
vel bit2(2 * sz, 0);
void add(int a, int w) {
	a += sz; bit[a] += w; bit2[a] = bit[a]; a /= 2;
	while (a != 0) {
		bit[a] += w;
		bit2[a] = max(bit2[2 * a], bit[2 * a] + max(bit2[2 * a + 1], (int)0));
		a /= 2;
	}
}
#define vveb V<veb>
signed main() {
    ios::sync_with_stdio(false);
	int t; cin >> t;
	vel pp = { p};
	int inf = 500000;
	int ba = 3242123;
	vel rui(inf + 1, 1);
    rep(j, inf) {
        rui[j + 1] = rui[j] * ba;
		rui[j + 1] %= p;
	}
	int sz=1;
	rep(_, t) {
		int n = 0; int m = 0;
		cin>>n>>m;
		vel a(n);V<pin> le(n);
		rep(i, n) { cin >> a[i]; le[i].second = i; }
		rep(i, m) {
			int u, v; cin >> u >> v; u--; v--;
			le[v].first += rui[u];
		}
		sor(le);
		int ans = 0;
		int nsum = 0;
		rep(i, n) {
			if (qle[i].first != ze) {
				nsum += a[le[i].second;
				if (i == n - 1 || le[i].first != le[i + 1].first) { ans = gcd(ans, nsum); nsum = 0; }
			}
		}
		cout << ans<<"\n";
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<queue>
#include<stack>
#include<bitset>
#include<functional>
#include<map>
#include<iomanip>
#include<limits>
#include<unordered_set>
#include<cmath>
#include <numeric>
#include <array>
#include <complex>
using namespace std;
//long long p = 998244353;
long long p = 1000000007;
#define int long long
#define ll long long
#define vel vector<ll>
#define vvel vector<vel>
#define rep(i,n) for(int i=0;i<n;i++)
#define sor(v) sort(v.begin(),v.end())
#define mmax(a,b) a=max(a,b)
#define mmin(a,b) a=min(a,b)
#define mkp make_pair
#define pin pair<int,int>
#define qin pair<pin,int>
#define V vector
#define Endl endl
#define veb vector<bool>
#define fcout cout << fixed << setprecision(15)
#define rev(s) reverse(s.begin(),s.end())
#define lower(h,val) (lower_bound(h.begin(),h.end(),val)-h.begin())
#define upper(h,val) (upper_bound(h.begin(),h.end(),val)-h.begin())
vel kai;
vel inv_kai;
int rui(int a, int n, int mod) {
	if (n == 0) { return 1 % mod; }
	int x = rui(a, n / 2, mod);
	x *= x; x %= mod;
	if (n % 2 == 1) { x *= a; x %= mod; }
	return x;
}
int root(int x, vel& pa) {
	if (pa[x] == -1) { return x; }
	int ans = root(pa[x], pa); pa[x] = ans;
	return ans;
}
bool mar(int x, int y, vel& pa) {
	x = root(x, pa);
	y = root(y, pa);
	if (x != y) { pa[x] = y; }
	return (x != y);
}
int gcd(int x, int y) {
	if (x < y) { return gcd(y, x); }
	if (y == 0) { return x; }
	return gcd(y, x % y);
}
int lcm(ll x, ll y) {
	x = abs(x); y = abs(y);
	return x * y / gcd(x, y);
}
long long modinv(long long a, long long m) {
	long long b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}
void make_kai(int max_kai) {
	kai = vel(max_kai, 1);
	inv_kai = kai;
	rep(i, max_kai - 1) {
		kai[i + 1] = kai[i] * (i + 1); kai[i + 1] %= p;
		inv_kai[i + 1] = modinv(kai[i + 1], p);
	}
}
int com(int n, int r) {
	if ((n < 0) || (r < 0) || (r > n)) { return 0; }
	int ans = (kai[n] * inv_kai[r]) % p;
	return (ans * inv_kai[n - r]) % p;
}
vel uni(vel x) {
	if (x.size() == 0) { return x; }
	sor(x);
	int n = x.size();
	vel ans(1, x[0]);
	for (int j = 1; j < n; j++) {
		if (x[j - 1] != x[j]) { ans.push_back(x[j]); }
	}
	x = ans;
	return x;
}
void pr(vel& v) {
	int n = v.size();
	if (n != 0) {
		cout << v[0];
		rep(i, n - 1) {
			cout << " " << v[i + 1];
		}
		cout << endl;
	}
}
vel dijk(V<V<pin>>& way, int st, int inf) {
	int n = way.size();
	vel dist(n, inf); dist[st] = 0;
	priority_queue<pin, vector<pin>, greater<pin>> pq;
	pq.push(mkp(0, st));
	veb is_checked(n, false);
	while (!pq.empty()) {
		pin x = pq.top(); pq.pop();
		int pot = x.second;
		if (!is_checked[pot]) {
			is_checked[pot] = true;
			for (auto y : way[pot]) {
				int nex_dist = x.first + y.second;
				int nex_pot = y.first;
				if (dist[nex_pot] > nex_dist) {
					dist[nex_pot] = nex_dist;
					pq.push(mkp(nex_dist, y.first));
				}
			}
		}
	}
	return dist;
}
vel mul(vel& a, vel& b) {
	int n = a.size();
	int m = b.size();
	vel ans(n + m - 1, 0);
	rep(i, n) {
		rep(j, m) {
			ans[i + j] += a[i] * b[j];
			ans[i + j] %= p;
		}
	}
	return ans;
}
vel rui_p(vel& a, int n) {
	if (n == 0) { return { 1 }; }
	vel qans = rui_p(a, n / 2);
	qans = mul(qans, qans);
	if (n % 2 == 1) {
		qans = mul(qans, a);
	}
	return qans;
}
bool is_prime(int n) {
	if (n == 0 || n == 1) { return false; }
	for (int i = 2; i * i <= n; i++) {
		if (n % i == 0) { return false; }
	}
	return true;
}
#define bs bitset<50>
void per(int& ans) {
	ans %= p;
	if (ans < 0) { ans += p; }
}
#define upperbound(v,val) upper_bound(v.begin(),v.end(),val)-v.begin()
#define lowerbound(v,val) lower_bound(v.begin(),v.end(),val)-v.begin()
#define mat V<V<pin>>
vvel disj_min(vel& v) {
	int n = v.size();
	vvel ret(22, vel(n));
	ret[0] = v;
	rep(i, 21) {
		rep(j, n) {
			int nex = j + (1 << i);
			if (nex < n) {
				ret[i + 1][j] = min(ret[i][j], ret[i][nex]);
			}
			else {
				ret[i + 1][j] = ret[i][j];
			}
		}
	}
	return ret;
}
vvel disj_max(vel& v) {
	int n = v.size();
	vvel ret(22, vel(n));
	ret[0] = v;
	rep(i, 21) {
		rep(j, n) {
			int nex = j + (1 << i);
			if (nex < n) {
				ret[i + 1][j] = max(ret[i][j], ret[i][nex]);
			}
			else {
				ret[i + 1][j] = ret[i][j];
			}
		}
	}
	return ret;
}
int find_min(vvel& dv, int l, int r) {
	int i = 21;
	while (l + (1 << i) > r) {
		i--;
	}
	return min(dv[i][l], dv[i][r - (1 << i)]);
}
int find_max(vvel& dv, int l, int r) {
	int i = 21;
	while (l + (1 << i) > r) {
		i--;
	}
	return max(dv[i][l], dv[i][r - (1 << i)]);
}
void pri(vel& v) {
	if (v.size() == 0) { return; }
	cout << v[0];
	rep(i, v.size() - 1) { cout << " " << v[i + 1]; }
	cout << endl;
	return;
}
vvel dbl(vel& v) {
	vvel ans(20, vel(v));
	int n = v.size();
	rep(i, 19) {
		rep(j, n) {
			ans[i + 1][j] = ans[i][ans[i][j]];
		}
	}
	return ans;
}
int lca(int s, int t, int diff, vvel& pa) {
	if (diff < 0) { return lca(t, s, -diff, pa); }
	rep(i, 19) {
		if ((diff & (1 << i)) != 0) {
			s = pa[i][s];
		}
	}
	for (int i = 19; i >= 0; i--) {
		if (pa[i][s] != pa[i][t]) {
			s = pa[i][s];
			t = pa[i][t];
		}
	}
	if (s != t) {
		s = pa[0][s];
	}
	return s;
}
#define d double
d dist(d x1, d y1, d x2, d y2) {
	return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int sz = 1024 * 1024;
vel bit(2 * sz, 0);
vel bit2(2 * sz, 0);
void add(int a, int w) {
	a += sz; bit[a] += w; bit2[a] = bit[a]; a /= 2;
	while (a != 0) {
		bit[a] += w;
		bit2[a] = max(bit2[2 * a], bit[2 * a] + max(bit2[2 * a + 1], (int)0));
		a /= 2;
	}
}
#define vveb V<veb>
signed main() {
	int t; cin >> t;
	rep(_, t) {
		int n = 0;int m=0;
		scanf("%ld", &n);
		scanf("%ld", &m);
		vel pp = { p };
		int sz = pp.size();
		vel a(n); vvel le(n, vel(sz+1,0));
		vvel qle(n, vel(sz,0));
		rep(i, n) { cin >> a[i]; le[i][3] = i; }
		int ba = 3242123;
		rep(i, m) {
			int u, v; cin >> u >> v; u--; v--;
			rep(j,sz) {
				int p1 = pp[j];
				le[v][j] += rui(ba, u, p1); le[v][j] %= p1;
				qle[v][j] = le[v][j];
			}
		}
		sor(le);
		sor(qle);
		int ans = 0;
		int nsum = 0;
		vel ze(sz,0);
		rep(i, n) {
			if (qle[i] != ze) {
				nsum += a[le[i][sz]];
				if (i==n-1||qle[i] != qle[i + 1]) { ans = gcd(ans, nsum); nsum = 0; }
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<queue>
#include<stack>
#include<bitset>
#include<functional>
#include<map>
#include<iomanip>
#include<limits>
#include<unordered_set>
#include<cmath>
#include <numeric>
#include <array>
#include <complex>
using namespace std;
//long long p = 998244353;
long long p = 1000000007;
#define int long long
#define ll long long
#define vel vector<ll>
#define vvel vector<vel>
#define rep(i,n) for(int i=0;i<n;i++)
#define sor(v) sort(v.begin(),v.end())
#define mmax(a,b) a=max(a,b)
#define mmin(a,b) a=min(a,b)
#define mkp make_pair
#define pin pair<int,int>
#define qin pair<pin,int>
#define V vector
#define Endl endl
#define veb vector<bool>
#define fcout cout << fixed << setprecision(15)
#define rev(s) reverse(s.begin(),s.end())
#define lower(h,val) (lower_bound(h.begin(),h.end(),val)-h.begin())
#define upper(h,val) (upper_bound(h.begin(),h.end(),val)-h.begin())
vel kai;
vel inv_kai;
int rui(int a, int n, int mod) {
	if (n == 0) { return 1 % mod; }
	int x = rui(a, n / 2, mod);
	x *= x; x %= mod;
	if (n % 2 == 1) { x *= a; x %= mod; }
	return x;
}
int root(int x, vel& pa) {
	if (pa[x] == -1) { return x; }
	int ans = root(pa[x], pa); pa[x] = ans;
	return ans;
}
bool mar(int x, int y, vel& pa) {
	x = root(x, pa);
	y = root(y, pa);
	if (x != y) { pa[x] = y; }
	return (x != y);
}
int gcd(int x, int y) {
	if (x < y) { return gcd(y, x); }
	if (y == 0) { return x; }
	return gcd(y, x % y);
}
int lcm(ll x, ll y) {
	x = abs(x); y = abs(y);
	return x * y / gcd(x, y);
}
long long modinv(long long a, long long m) {
	long long b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}
void make_kai(int max_kai) {
	kai = vel(max_kai, 1);
	inv_kai = kai;
	rep(i, max_kai - 1) {
		kai[i + 1] = kai[i] * (i + 1); kai[i + 1] %= p;
		inv_kai[i + 1] = modinv(kai[i + 1], p);
	}
}
int com(int n, int r) {
	if ((n < 0) || (r < 0) || (r > n)) { return 0; }
	int ans = (kai[n] * inv_kai[r]) % p;
	return (ans * inv_kai[n - r]) % p;
}
vel uni(vel x) {
	if (x.size() == 0) { return x; }
	sor(x);
	int n = x.size();
	vel ans(1, x[0]);
	for (int j = 1; j < n; j++) {
		if (x[j - 1] != x[j]) { ans.push_back(x[j]); }
	}
	x = ans;
	return x;
}
void pr(vel& v) {
	int n = v.size();
	if (n != 0) {
		cout << v[0];
		rep(i, n - 1) {
			cout << " " << v[i + 1];
		}
		cout << endl;
	}
}
vel dijk(V<V<pin>>& way, int st, int inf) {
	int n = way.size();
	vel dist(n, inf); dist[st] = 0;
	priority_queue<pin, vector<pin>, greater<pin>> pq;
	pq.push(mkp(0, st));
	veb is_checked(n, false);
	while (!pq.empty()) {
		pin x = pq.top(); pq.pop();
		int pot = x.second;
		if (!is_checked[pot]) {
			is_checked[pot] = true;
			for (auto y : way[pot]) {
				int nex_dist = x.first + y.second;
				int nex_pot = y.first;
				if (dist[nex_pot] > nex_dist) {
					dist[nex_pot] = nex_dist;
					pq.push(mkp(nex_dist, y.first));
				}
			}
		}
	}
	return dist;
}
vel mul(vel& a, vel& b) {
	int n = a.size();
	int m = b.size();
	vel ans(n + m - 1, 0);
	rep(i, n) {
		rep(j, m) {
			ans[i + j] += a[i] * b[j];
			ans[i + j] %= p;
		}
	}
	return ans;
}
vel rui_p(vel& a, int n) {
	if (n == 0) { return { 1 }; }
	vel qans = rui_p(a, n / 2);
	qans = mul(qans, qans);
	if (n % 2 == 1) {
		qans = mul(qans, a);
	}
	return qans;
}
bool is_prime(int n) {
	if (n == 0 || n == 1) { return false; }
	for (int i = 2; i * i <= n; i++) {
		if (n % i == 0) { return false; }
	}
	return true;
}
#define bs bitset<50>
void per(int& ans) {
	ans %= p;
	if (ans < 0) { ans += p; }
}
#define upperbound(v,val) upper_bound(v.begin(),v.end(),val)-v.begin()
#define lowerbound(v,val) lower_bound(v.begin(),v.end(),val)-v.begin()
#define mat V<V<pin>>
vvel disj_min(vel& v) {
	int n = v.size();
	vvel ret(22, vel(n));
	ret[0] = v;
	rep(i, 21) {
		rep(j, n) {
			int nex = j + (1 << i);
			if (nex < n) {
				ret[i + 1][j] = min(ret[i][j], ret[i][nex]);
			}
			else {
				ret[i + 1][j] = ret[i][j];
			}
		}
	}
	return ret;
}
vvel disj_max(vel& v) {
	int n = v.size();
	vvel ret(22, vel(n));
	ret[0] = v;
	rep(i, 21) {
		rep(j, n) {
			int nex = j + (1 << i);
			if (nex < n) {
				ret[i + 1][j] = max(ret[i][j], ret[i][nex]);
			}
			else {
				ret[i + 1][j] = ret[i][j];
			}
		}
	}
	return ret;
}
int find_min(vvel& dv, int l, int r) {
	int i = 21;
	while (l + (1 << i) > r) {
		i--;
	}
	return min(dv[i][l], dv[i][r - (1 << i)]);
}
int find_max(vvel& dv, int l, int r) {
	int i = 21;
	while (l + (1 << i) > r) {
		i--;
	}
	return max(dv[i][l], dv[i][r - (1 << i)]);
}
void pri(vel& v) {
	if (v.size() == 0) { return; }
	cout << v[0];
	rep(i, v.size() - 1) { cout << " " << v[i + 1]; }
	cout << endl;
	return;
}
vvel dbl(vel& v) {
	vvel ans(20, vel(v));
	int n = v.size();
	rep(i, 19) {
		rep(j, n) {
			ans[i + 1][j] = ans[i][ans[i][j]];
		}
	}
	return ans;
}
int lca(int s, int t, int diff, vvel& pa) {
	if (diff < 0) { return lca(t, s, -diff, pa); }
	rep(i, 19) {
		if ((diff & (1 << i)) != 0) {
			s = pa[i][s];
		}
	}
	for (int i = 19; i >= 0; i--) {
		if (pa[i][s] != pa[i][t]) {
			s = pa[i][s];
			t = pa[i][t];
		}
	}
	if (s != t) {
		s = pa[0][s];
	}
	return s;
}
#define d double
d dist(d x1, d y1, d x2, d y2) {
	return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int sz = 1024 * 1024;
vel bit(2 * sz, 0);
vel bit2(2 * sz, 0);
void add(int a, int w) {
	a += sz; bit[a] += w; bit2[a] = bit[a]; a /= 2;
	while (a != 0) {
		bit[a] += w;
		bit2[a] = max(bit2[2 * a], bit[2 * a] + max(bit2[2 * a + 1], (int)0));
		a /= 2;
	}
}
#define vveb V<veb>
#define omaji cin.tie(0);ios::sync_with_stdio(false)
#define endl "\n"
signed main() {
    omaji;
	int t; cin >> t;
	vel pp = { p};
	int inf = 500000;
	int ba = 3242123;
	vel rui(inf + 1, 1);
    rep(j, inf) {
        rui[j + 1] = rui[j] * ba;
		rui[j + 1] %= p;
	}
	int sz=1;
	rep(_, t) {
		int n = 0; int m = 0;
		cin>>n>>m;
		vel a(n);V<pin> le(n);
		rep(i, n) { cin >> a[i]; le[i].second = i; }
		rep(i, m) {
			int u, v; cin >> u >> v; u--; v--;
			le[v].first += rui[u];
		}
		sor(le);
		int ans = 0;
		int nsum = 0;
		rep(i, n) {
			if (le[i].first != 0) {
				nsum += a[le[i].second;
				if (i == n - 1 || le[i].first != le[i + 1].first) { ans = gcd(ans, nsum); nsum = 0; }
			}
		}
		cout << ans<<"\n";
	}
	return 0;
}