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

const int N = 1e6 + 5;

int L[N], R[N], n, q, A[N];

set <int> S;

vector <int> pos[N];

bool pd(int i) {
	if(L[i] > R[i] || L[i - 1] > R[i - 1]) return 1;
	if(R[i] < L[i - 1] || R[i - 1] < L[i]) return 1;
	return (L[i] >= L[i - 1] && R[i] <= R[i - 1]);
}

void doit(int l, int r, int x) {
	if(l > r || !S.size()) return;
	set <int> :: iterator L = S.lower_bound(l);
	set <int> :: iterator R = S.upper_bound(r);
	if(R == S.begin()) return;
	-- R;
	if(L == S.end() || R == S.end()) return;
	if((*L) > (*R)) return;
	vector <int> cxt;
	cxt.clear();
	for(; L != R; ++ L) {
		A[*L] = x;
		cxt.push_back(*L);
	}
	A[*R] = x;
	cxt.push_back(*R);
	for(int i = 0; i < (int) cxt.size(); ++ i) S.erase(cxt[i]);
}

#define lowbit(x) (x & (-x))

int SS[N];

void add(int x, int v) {
	for(int i = x; i < N; i += lowbit(i)) SS[i] += v;
}

int sum(int x) {
	int res = 0;
	for(int i = x; i; i -= lowbit(i)) {
		res += SS[i];
	}
	return res;
}

bool pd(void) {
	for(int i = 0; i < (int) pos[0].size(); ++ i) add(pos[0][i], 1);
	for(int i = q; i >= 1; -- i) {
		for(int j = 0; j < (int) pos[i].size(); ++ j) add(pos[i][j], 1);
		if(R[i] < L[i]) continue;
		if(sum(R[i]) - sum(L[i] - 1) == R[i] - L[i] + 1) {
			continue;
		}
		return 1;
	}
	return 0;
}

queue <int> Q;

void BFS(void) {
	while(!Q.empty()) {
		int ind = Q.front();
		Q.pop();
		if(!A[ind - 1] && ind > 1) {
			A[ind - 1] = A[ind];
			Q.push(ind - 1);
		}
		if(ind < n && !A[ind + 1]) {
			Q.push(ind + 1);
			A[ind + 1] = A[ind];
		}
	}
}

main(void) {
	cin >> n >> q;
	memset(L, 127, sizeof(L));
	for(int i = 1; i <= n; ++ i) scanf("%d", &A[i]),
	L[A[i]] = min(L[A[i]], i), R[A[i]] = max(R[A[i]], i), pos[A[i]].push_back(i);
	if(pd()) {
		puts("NO");
		return 0;
	}
	for(int i = 1; i <= n; ++ i) {
		if(!A[i]) S.insert(i);
	}
	if(!S.size() && L[q] > R[q]) {
		puts("NO");
		return 0;
	}
	for(int i = q; i >= 1; -- i) {
		doit(L[i], R[i], i);
	}
	if(L[q] > R[q]) {
		bool ok = 0;
		for(int i = 1; i <= n; ++ i) if(!A[i]) {
			A[i] = q;
			ok = 1;
			break;
		}
		if(!ok) {
		    puts("Asd");
			puts("NO");
			return 0;
		}
	}
	for(int i = 1; i <= n; ++ i) {
		if(A[i])Q.push(i);
	}
	BFS();
	for(int i = 0; i <= q; ++ i) pos[i].clear();
	memset(SS, 0, sizeof(SS));
	memset(L, 127, sizeof(L));
	memset(R, 0, sizeof(R));
	for(int i = 1; i <= n; ++ i) pos[A[i]].push_back(i), L[A[i]] = min(L[A[i]], i), R[A[i]] = max(R[A[i]], i);
	if(pd()) {
		assert(0);
	}
	for(int i = 1; i <= n; ++ i) if(!A[i]) assert(0);
	puts("YES");
	for(int i = 1; i <= n; ++ i) printf("%d ", A[i]);
	puts("");
}


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

const int N = 1e6 + 5;

int L[N], R[N], n, q, A[N];

set <int> S;

vector <int> pos[N];

bool pd(int i) {
	if(L[i] > R[i] || L[i - 1] > R[i - 1]) return 1;
	if(R[i] < L[i - 1] || R[i - 1] < L[i]) return 1;
	return (L[i] >= L[i - 1] && R[i] <= R[i - 1]);
}

void doit(int l, int r, int x) {
	if(l > r || !S.size()) return;
	set <int> :: iterator L = S.lower_bound(l);
	set <int> :: iterator R = S.upper_bound(r);
	if(R == S.begin()) return;
	-- R;
	if(L == S.end() || R == S.end()) return;
	if((*L) > (*R)) return;
	vector <int> cxt;
	cxt.clear();
	for(; L != R; ++ L) {
		A[*L] = x;
		cxt.push_back(*L);
	}
	A[*R] = x;
	cxt.push_back(*R);
	for(int i = 0; i < (int) cxt.size(); ++ i) S.erase(cxt[i]);
}

#define lowbit(x) (x & (-x))

int SS[N];

void add(int x, int v) {
	for(int i = x; i < N; i += lowbit(i)) SS[i] += v;
}

int sum(int x) {
	int res = 0;
	for(int i = x; i; i -= lowbit(i)) {
		res += SS[i];
	}
	return res;
}

bool pd(void) {
	for(int i = 0; i < (int) pos[0].size(); ++ i) add(pos[0][i], 1);
	for(int i = q; i >= 1; -- i) {
		for(int j = 0; j < (int) pos[i].size(); ++ j) add(pos[i][j], 1);
		if(R[i] < L[i]) continue;
		if(sum(R[i]) - sum(L[i] - 1) == R[i] - L[i] + 1) {
			continue;
		}
		return 1;
	}
	return 0;
}

queue <int> Q;

void BFS(void) {
	while(!Q.empty()) {
		int ind = Q.front();
		Q.pop();
		if(!A[ind - 1] && ind > 1) {
			A[ind - 1] = A[ind];
			Q.push(ind - 1);
		}
		if(ind < n && !A[ind + 1]) {
			Q.push(ind + 1);
			A[ind + 1] = A[ind];
		}
	}
}

main(void) {
	cin >> n >> q;
	memset(L, 127, sizeof(L));
	for(int i = 1; i <= n; ++ i) scanf("%d", &A[i]),
	L[A[i]] = min(L[A[i]], i), R[A[i]] = max(R[A[i]], i), pos[A[i]].push_back(i);
	if(pd()) {
		puts("NO");
		return 0;
	}
	for(int i = 1; i <= n; ++ i) {
		if(!A[i]) S.insert(i);
	}
	if(!S.size() && L[q] > R[q]) {
		puts("NO");
		return 0;
	}
	for(int i = q; i >= 1; -- i) {
		doit(L[i], R[i], i);
	}
	if(L[q] > R[q]) {
		bool ok = 0;
		for(int i = 1; i <= n; ++ i) if(!A[i]) {
			A[i] = q;
			ok = 1;
			break;
		}
		if(!ok) {
			puts("NO");
			return 0;
		}
	}
	for(int i = 1; i <= n; ++ i) {
		if(A[i])Q.push(i);
	}
	BFS();
	for(int i = 0; i <= q; ++ i) pos[i].clear();
	memset(SS, 0, sizeof(SS));
	memset(L, 127, sizeof(L));
	memset(R, 0, sizeof(R));
	for(int i = 1; i <= n; ++ i) pos[A[i]].push_back(i), L[A[i]] = min(L[A[i]], i), R[A[i]] = max(R[A[i]], i);
	if(pd()) {
		assert(0);
	}
	for(int i = 1; i <= n; ++ i) if(!A[i]) assert(0);
	puts("YES");
	for(int i = 1; i <= n; ++ i) printf("%d ", A[i]);
	puts("");
}


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

const int N = 1e6 + 5;

int L[N], R[N], n, q, A[N];

set <int> S;

vector <int> pos[N];

bool pd(int i) {
	if(L[i] > R[i] || L[i - 1] > R[i - 1]) return 1;
	if(R[i] < L[i - 1] || R[i - 1] < L[i]) return 1;
	return (L[i] >= L[i - 1] && R[i] <= R[i - 1]);
}

void doit(int l, int r, int x) {
	if(l > r || !S.size()) return;
	set <int> :: iterator L = S.lower_bound(l);
	set <int> :: iterator R = S.upper_bound(r);
	if(R == S.begin()) return;
	-- R;
	if(L == S.end() || R == S.end()) return;
	if((*L) > (*R)) return;
	vector <int> cxt;
	cxt.clear();
	for(; L != R; ++ L) {
		A[*L] = x;
		cxt.push_back(*L);
	}
	A[*R] = x;
	cxt.push_back(*R);
	for(int i = 0; i < (int) cxt.size(); ++ i) S.erase(cxt[i]);
}

#define lowbit(x) (x & (-x))

int SS[N];

void add(int x, int v) {
	for(int i = x; i < N; i += lowbit(i)) SS[i] += v;
}

int sum(int x) {
	int res = 0;
	for(int i = x; i; i -= lowbit(i)) {
		res += SS[i];
	}
	return res;
}

bool pd(void) {
	for(int i = 0; i < (int) pos[0].size(); ++ i) add(pos[0][i], 1);
	for(int i = q; i >= 1; -- i) {
		for(int j = 0; j < (int) pos[i].size(); ++ j) add(pos[i][j], 1);
		if(R[i] < L[i]) continue;
		if(sum(R[i]) - sum(L[i] - 1) == R[i] - L[i] + 1) {
			continue;
		}
		return 1;
	}
	return 0;
}

queue <int> Q;

void BFS(void) {
	while(!Q.empty()) {
		int ind = Q.front();
		Q.pop();
		if(!A[ind - 1] && ind > 1) {
			A[ind - 1] = A[ind];
			Q.push(ind - 1);
		}
		if(ind < n && !A[ind + 1]) {
			Q.push(ind + 1);
			A[ind + 1] = A[ind];
		}
	}
}

main(void) {
	cin >> n >> q;
	memset(L, 127, sizeof(L));
	for(int i = 1; i <= n; ++ i) scanf("%d", &A[i]),
	L[A[i]] = min(L[A[i]], i), R[A[i]] = max(R[A[i]], i), pos[A[i]].push_back(i);
	if(pd()) {
		puts("NO");
		return 0;
	}
	for(int i = 1; i <= n; ++ i) {
		if(!A[i]) S.insert(i);
	}
	if(!S.size() && L[q] > R[q]) {
		puts("NO");
		return 0;
	}
	if(L[q] > R[q]) {
		bool ok = 0;
		for(int i = 1; i <= n; ++ i) if(!A[i]) {
			A[i] = q;
			S.erase(i);
			ok = 1;
			break;
		}
		if(!ok) {
			puts("NO");
			return 0;
		}
	}
	for(int i = q; i >= 1; -- i) {
		doit(L[i], R[i], i);
	}
	for(int i = 1; i <= n; ++ i) {
		if(A[i])Q.push(i);
	}
	BFS();
	for(int i = 0; i <= q; ++ i) pos[i].clear();
	memset(SS, 0, sizeof(SS));
	memset(L, 127, sizeof(L));
	memset(R, 0, sizeof(R));
	for(int i = 1; i <= n; ++ i) pos[A[i]].push_back(i), L[A[i]] = min(L[A[i]], i), R[A[i]] = max(R[A[i]], i);
	if(pd()) {
		assert(0);
	}
	for(int i = 1; i <= n; ++ i) if(!A[i]) assert(0);
	puts("YES");
	for(int i = 1; i <= n; ++ i) printf("%d ", A[i]);
	puts("");
}


/*
author: Apoorv Singh
*/

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

using namespace std;
using namespace __gnu_pbds;

typedef vector <int> vi;
typedef vector <long long> vl;
typedef pair <int, int> pii;
typedef pair <long long, long long> pll;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

#define ll long long
#define pb push_back
#define all(c) c.begin(), c.end()
#define watch(x) cout << (#x) << " " << (x) << endl;
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = a; i > b; --i)
#define um unordered_map
#define F first
#define S second

ll modulo = 1e9 + 7;

template <class X>

void printarr(X arr[], int n)
{
    for (int i = 0; i < n; ++i)
        cout << arr[i] << " ";
    cout << endl;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int M = 2e5 + 10;
    int arr[M] = {0};
    int n,q;
    cin >> n >> q;
    int seq[n];
    bool valid = false;
    FOR(i,0,n)
    {
        cin >> seq[i];
        if (seq[i] == q or seq[i] == 0)
            valid = true;
    }
    if (!valid)
    {
        cout << "NO";
        return 0;
    }
    ++arr[seq[0]];
    FOR(i,1,n)
    {
        if (seq[i] > seq[i-1] and seq[i-1] != 0)
        {
            if (arr[seq[i]]>=1)
            {
                valid = false;
                break;
            }
            else
                ++arr[seq[i]];
        }
        else if (seq[i] != 0)
            ++arr[seq[i]];
    }
    // printarr(arr,4);
    if (!valid)
    {
        cout << "NO";
        return 0;
    }
    FOR(i,0,n)
    {
        if (seq[i] == 0)
        {
            if (arr[q] == 0)
            {
                seq[i] = q;
                ++arr[q];
            }
            else
            {
                if (i > 0)
                    seq[i] = seq[i-1];
                else
                    seq[i] = 1;
            }
        }
    }
    cout << "YES" << endl;
    printarr(seq,n);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main() {
	int n,q;
	cin>>n>>q;
	int a[n];
	vector<int> v;
	int flag=0;
	for(int i=0;i<n;++i) {
		cin>>a[i];
		if(a[i]!=0) {
			v.push_back(a[i]);
		}
		if(a[i] == q) {
			flag = 1;
		}
	}
	if(flag == 0 && v.size()==n) {
		cout<<"NO\n";
		return 0;
	}
	if(flag == 0) {
		for(int i=0;i<n;++i) {
			if(!a[i]) {
				a[i] = q;
				break;
			}
		}
	}
	if(v.size() > 0) {
		int b[n] = {0};
		b[v[0]] = 1;
		for(int i =1;i<v.size();++i) {
			if(b[v[i]] >= 1 && v[i-1] < v[i]) {
				cout<<"NO\n";
				return 0;
			}
			else {
				b[v[i]] = 1;
			}
		}

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

				a[i] = a[i-1];

		}
	}
	cout<<"YES\n";
	for(int i=0;i<n;++i) {
		cout<<a[i]<<" ";
	}
	cout<<"\n";

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main() {
	int n,q;
	cin>>n>>q;
	int a[n];
	vector<int> v;
	int flag=0;
	for(int i=0;i<n;++i) {
		cin>>a[i];
		if(a[i]!=0) {
			v.push_back(a[i]);
		}
		if(a[i] == q) {
			flag = 1;
		}
	}
	if(flag == 0 && v.size()==n) {
		cout<<"NO\n";
		return 0;
	}
	if(flag == 0) {
		for(int i=0;i<n;++i) {
			if(!a[i]) {
				a[i] = q;
				break;
			}
		}
	}
	if(v.size() > 0) {
		int b[n] = {0};
		b[v[0]] = 1;
		for(int i =1;i<v.size();++i) {
			if(b[v[i]] >= 1 && v[i-1] < v[i]) {
				cout<<"NO\n";
				return 0;
			}
			else {
				b[v[i]] = 1;
			}
		}

	}
	if(a[0] == 0) {
		a[0] = a[1];
	}
	for(int i=0;i<n;++i) {
		if(!a[i]) {
				a[i] = a[i-1];
		}
	}
	cout<<"YES\n";
	for(int i=0;i<n;++i) {
		cout<<a[i]<<" ";
	}
	cout<<"\n";

	return 0;
}


/*
author: Apoorv Singh
*/

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

using namespace std;
using namespace __gnu_pbds;

typedef vector <int> vi;
typedef vector <long long> vl;
typedef pair <int, int> pii;
typedef pair <long long, long long> pll;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

#define ll long long
#define pb push_back
#define all(c) c.begin(), c.end()
#define watch(x) cout << (#x) << " " << (x) << endl;
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = a; i > b; --i)
#define um unordered_map
#define F first
#define S second

ll modulo = 1e9 + 7;

template <class X>

void printarr(X arr[], int n)
{
    for (int i = 0; i < n; ++i)
        cout << arr[i] << " ";
    cout << endl;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int M = 2e5 + 10;
    int arr[M] = {0};
    int n,q;
    cin >> n >> q;
    int seq[n];
    bool valid = false;
    FOR(i,0,n)
    {
        cin >> seq[i];
        if (seq[i] == q or seq[i] == 0)
            valid = true;
    }
    if (!valid)
    {
        cout << "NO";
        return 0;
    }
    ++arr[seq[0]];
    FOR(i,1,n)
    {
        if (seq[i] > seq[i-1] and seq[i-1] != 0)
        {
            if (arr[seq[i]]>=1)
            {
                valid = false;
                break;
            }
            else
                ++arr[seq[i]];
        }
        else if (seq[i] != 0)
            ++arr[seq[i]];
    }
    // printarr(arr,4);
    if (!valid)
    {
        cout << "NO";
        return 0;
    }
    FOR(i,0,n)
    {
        if (seq[i] == 0)
        {
            if (arr[q] == 0)
            {
                seq[i] = q;
                ++arr[q];
            }
            else
            {
                if (i > 0)
                    seq[i] = seq[i-1];
                else
                    seq[i] = 1;
            }
        }
    }
    printarr(seq,n);
    return 0;
}


/*
author: Apoorv Singh
*/

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

using namespace std;
using namespace __gnu_pbds;

typedef vector <int> vi;
typedef vector <long long> vl;
typedef pair <int, int> pii;
typedef pair <long long, long long> pll;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

#define ll long long
#define pb push_back
#define all(c) c.begin(), c.end()
#define watch(x) cout << (#x) << " " << (x) << endl;
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = a; i > b; --i)
#define um unordered_map
#define F first
#define S second

ll modulo = 1e9 + 7;

template <class X>

void printarr(X arr[], int n)
{
    for (int i = 0; i < n; ++i)
        cout << arr[i] << " ";
    cout << endl;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int M = 2e5 + 10;
    int arr[M] = {0};
    int n,q;
    cin >> n >> q;
    int seq[n];
    bool valid = false;
    FOR(i,0,n)
    {
        cin >> seq[i];
        if (seq[i] == q or seq[i] == 0)
            valid = true;
    }
    if (!valid)
    {
        cout << "NO";
        return 0;
    }
    ++arr[seq[0]];
    int mini = INT_MAX;
    FOR(i,1,n)
    {
        if (seq[i] > mini and seq[i-1] != 0)
        {
            if (arr[seq[i]]>=1)
            {
                valid = false;
                break;
            }
            else
                ++arr[seq[i]];
        }
        else if (seq[i] != 0)
            ++arr[seq[i]];
        if (seq[i] != 0)
            mini = min(seq[i], mini);
    }
    if (!valid)
    {
        cout << "NO";
        return 0;
    }
    FOR(i,0,n)
    {
        if (seq[i] == 0)
        {
            if (arr[q] == 0)
            {
                seq[i] = q;
                ++arr[q];
            }
            else
            {
                if (i > 0)
                    seq[i] = seq[i-1];
                else
                    seq[i] = 1;
            }
        }
    }
    cout << "YES" << endl;
    printarr(seq,n);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main() {
	int n,q;
	cin>>n>>q;
	int a[n];
	vector<int> v;
	int flag=0;
	for(int i=0;i<n;++i) {
		cin>>a[i];
		if(a[i]!=0) {
			v.push_back(a[i]);
		}
		if(a[i] == q) {
			flag = 1;
		}
	}
	if(flag == 0 && v.size()==n) {
		cout<<"NO\n";
		return 0;
	}
	if(flag == 0) {
		for(int i=0;i<n;++i) {
			if(!a[i]) {
				a[i] = q;
				break;
			}
		}
	}
	if(v.size() > 0) {
		int b[n] = {0};
		b[v[0]] = 1;
		for(int i =1;i<v.size();++i) {
			if(b[v[i]] >= 1 && v[i-1] < v[i]) {
				cout<<"NO\n";
				return 0;
			}
			else {
				b[v[i]] = 1;
			}
		}

	}
	int kk=0;
	for(int i=0;i<n;++i) {
		if(a[i]!=0) {
			kk = a[i];
		}
	}
	if(a[0] == 0) {
		a[0] = kk;
	}
	for(int i=0;i<n;++i) {
		if(!a[i]) {
				a[i] = a[i-1];
		}
	}
	cout<<"YES\n";
	for(int i=0;i<n;++i) {
		cout<<a[i]<<" ";
	}
	cout<<"\n";

	return 0;
}


/*
author: Apoorv Singh
*/

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

using namespace std;
using namespace __gnu_pbds;

typedef vector <int> vi;
typedef vector <long long> vl;
typedef pair <int, int> pii;
typedef pair <long long, long long> pll;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

#define ll long long
#define pb push_back
#define all(c) c.begin(), c.end()
#define watch(x) cout << (#x) << " " << (x) << endl;
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = a; i > b; --i)
#define um unordered_map
#define F first
#define S second

ll modulo = 1e9 + 7;

template <class X>

void printarr(X arr[], int n)
{
    for (int i = 0; i < n; ++i)
        cout << arr[i] << " ";
    cout << endl;
}

const int M = 2e5 + 10;
int segTree[4*M], seq[M];

void build(int node, int start, int end)
{
    if (start == end)
        segTree[node] = seq[start];
    else
    {
        int mid = (start+end)/2;
        build(2*node+1, start,mid);
        build(2*node+2, mid+1,end);
        segTree[node] = min(segTree[2*node+1],segTree[2*node+2]);
    }
}

int query(int node, int l, int r, int start, int end)
{
    if (end < l or start > r)
        return INT_MAX;
    if (start >= l and end <= r)
        return segTree[node];
    int mid = (start+end)/2;
    return min(query(2*node+1, l,r,start,mid),query(2*node+2, l,r,mid+1,end));
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int n,q;
    cin >> n >> q;
    bool valid = false;
    int p = 0;
    int arr[n];
    FOR(i,0,n)
    {
        cin >> arr[i];
        if (arr[i] == q or arr[i] == 0)
            valid = true;
        if (arr[i] == q)
            ++p;
    }
    if (!valid)
    {
        cout << "NO";
        return 0;
    }
    int cnt = 0;
    FOR(i,0,n)
    {
        if (arr[i] != 0)
        {
            seq[cnt] = arr[i];
            ++cnt;
        }
    }
    if (cnt > 0)
    {
        build(0,0,cnt-1);
        vi v[M];
        FOR(i,0,cnt)
            v[seq[i]].pb(i);
        FOR(i,1,M)
        {
            int x = v[i].size();
            FOR(j,0,x-1)
            {
                int y = query(0,v[i][j],v[i][j+1],0,cnt-1);
                if (y < i)
                {
                    cout << "NO";
                    return 0;
                }

            }
        }
    }
    FOR(i,0,n)
    {
        if (arr[i] == 0)
        {
            if (p == 0)
            {
                arr[i] = q;
                ++p;
            }
            else
            {
                if (i > 0)
                    arr[i] = arr[i-1];
                else
                    arr[i] = 1;
            }
        }
    }
    cout << "YES" << endl;
    printarr(arr,n);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main() {
	int n,q;
	cin>>n>>q;
	int a[n];
	vector<int> v;
	int flag=0;
	for(int i=0;i<n;++i) {
		cin>>a[i];
		if(a[i]!=0) {
			v.push_back(a[i]);
		}
		if(a[i] == q) {
			flag = 1;
		}
	}
	if(flag == 0 && v.size()==n) {
		cout<<"NO\n";
		return 0;
	}
	if(flag == 0) {
		for(int i=0;i<n;++i) {
			if(!a[i]) {
				a[i] = q;
				break;
			}
		}
	}
	if(v.size() > 0) {
		int b[n] = {0};
		b[v[0]] = 1;
		for(int i =1;i<v.size();++i) {
			if(b[v[i]] >= 1 && v[i-1] < v[i]) {
				cout<<"NO\n";
				return 0;
			}
			else {
				b[v[i]] = 1;
			}
		}

	}

	if(a[0] == 0) {
		int kk=0;
		for(int i=0;i<n;++i) {
			if(!a[i])
				kk = a[i];
		}
		a[0] = kk;
	}

	for(int i=0;i<n;++i) {
		if(!a[i]) {

				a[i] = a[i-1];

		}
	}
	cout<<"YES\n";
	for(int i=0;i<n;++i) {
		cout<<a[i]<<" ";
	}
	cout<<"\n";

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main() {
	int n,q;
	cin>>n>>q;
	int a[n];
	vector<int> v;
	int flag=0;
	for(int i=0;i<n;++i) {
		cin>>a[i];
		if(a[i]!=0) {
			v.push_back(a[i]);
		}
		if(a[i] == q) {
			flag = 1;
		}
	}
	if(flag == 0 && v.size()==n) {
		cout<<"NO\n";
		return 0;
	}
	if(flag == 0) {
		for(int i=0;i<n;++i) {
			if(!a[i]) {
				a[i] = q;
				break;
			}
		}

	}
	else {
	    if(a[0] == 0) {
    		int kk=0;
    		for(int i=0;i<n;++i) {
    			if(a[i]!=0) {
    				kk = a[i];
    				break;
    			}
    		}
    		a[0] = kk;
	    }
	}
	if(v.size() > 0) {
		int b[n] = {0};
		b[v[0]] = 1;
		for(int i =1;i<v.size();++i) {
			if(b[v[i]] >= 1 && v[i-1] < v[i]) {
				cout<<"NO\n";
				return 0;
			}
			else {
				b[v[i]] = 1;
			}
		}

	}

	for(int i=0;i<n;++i) {
		if(!a[i]) {

				a[i] = a[i-1];

		}
	}
	cout<<"YES\n";
	for(int i=0;i<n;++i) {
		cout<<a[i]<<" ";
	}
	cout<<"\n";

	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define fr(i,a,b) for(int i=a;i<=b;i++)
#define re_fr(i,a,b) for(int i=a;i>=b;i--)
#define frl(i,a,b) for(ll i=a;i<=b;i++)
#define re_frl(i,a,b) for(ll i=a;i>=b;i--)
#define mp make_pair
#define F first
#define S second
#define MX 5000010
#define eps 1e-9
#define PI acos(-1)
#define inf 1e18
#define pll pair<ll,ll>
//#define mod 1000000007
#define mod2 100000000
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define PI acos(-1)
#define fast() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
map<ll,ll>Map;
map<ll,ll>::iterator it;
ll a[200010];
int main()
{

    fast();
    ll n,q,x;
    cin>>n>>q;
    ll prev=-1;
    bool ck=true;
    ll hv=0;
    for(ll i=1;i<=n;i++)
    {
        cin>>x;
        a[i]=x;
        if(x==0) {hv++;continue;}
        if(x==prev)
        {
            Map[x]=i;
            continue;
        }
        if(Map.count(x))
        {

            it=Map.lower_bound(x);
            if(it==Map.begin())
            {
                Map[x]=i; prev=x;
            }
            else
            {

                for(it=Map.begin();it!=Map.end();it++)
                //cout<<it->S<<' '<<Map[x]<<endl;
                if(it->S>Map[x]) {ck=false; break;}
                Map[x]=i; prev=x;

            }

        }
        else
        {
            Map[x]=i;

        }
    }
        if(!ck || ( !Map.count(q) && hv==0) )  cout<<"NO"<<endl;
        else
        {
            cout<<"YES"<<endl;
            ll prev=-1;
            ll qq;
            if(!Map.count(q)) qq=q;
            else qq=1;
            for(ll i=n;i>=1;i--)
            {
                if(a[i]==0)
                {
                    if(prev==-1) a[i]=qq;
                    else a[i]=prev;
                }
                else prev=a[i];
            }
            for(ll i=1;i<=n;i++) cout<<a[i]<<' ';
        }

}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define fr(i,a,b) for(int i=a;i<=b;i++)
#define re_fr(i,a,b) for(int i=a;i>=b;i--)
#define frl(i,a,b) for(ll i=a;i<=b;i++)
#define re_frl(i,a,b) for(ll i=a;i>=b;i--)
#define mp make_pair
#define F first
#define S second
#define MX 5000010
#define eps 1e-9
#define PI acos(-1)
#define inf 1e18
#define pll pair<ll,ll>
//#define mod 1000000007
#define mod2 100000000
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define PI acos(-1)
#define fast() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
map<ll,ll>Map;
map<ll,ll>::iterator it;
ll a[200010];
int main()
{

    fast();
    ll n,q,x;
    cin>>n>>q;
    ll prev=-1;
    bool ck=true;
    for(ll i=1;i<=n;i++)
    {
        cin>>x;
        a[i]=x;
        if(x==0) continue;
        if(x==prev)
        {
            Map[x]=i;
            continue;
        }
        if(Map.count(x))
        {

            it=Map.lower_bound(x);
            if(it==Map.begin())
            {
                Map[x]=i; prev=x;
            }
            else
            {
                it--;
                //cout<<it->S<<' '<<Map[x]<<endl;
                if(it->S>Map[x]) ck=false;
                Map[x]=i; prev=x;

            }

        }
        else
        {
            Map[x]=i;

        }
    }
        if(!ck || ! Map.count(q)) cout<<"NO"<<endl;
        else
        {
            cout<<"YES"<<endl;
            ll prev=-1;
            for(ll i=n;i>=1;i--)
            {
                if(a[i]==0)
                {
                    if(prev==-1) a[i]=q;
                    else a[i]=prev;
                }
                else prev=a[i];
            }
            for(ll i=1;i<=n;i++) cout<<a[i]<<' ';
        }

}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define fr(i,a,b) for(int i=a;i<=b;i++)
#define re_fr(i,a,b) for(int i=a;i>=b;i--)
#define frl(i,a,b) for(ll i=a;i<=b;i++)
#define re_frl(i,a,b) for(ll i=a;i>=b;i--)
#define mp make_pair
#define F first
#define S second
#define MX 5000010
#define eps 1e-9
#define PI acos(-1)
#define inf 1e18
#define pll pair<ll,ll>
//#define mod 1000000007
#define mod2 100000000
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define PI acos(-1)
#define fast() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
map<ll,ll>Map;
map<ll,ll>::iterator it;
ll a[200010];
int main()
{

    fast();
    ll n,q,x;
    cin>>n>>q;
    ll prev=-1;
    bool ck=true;
    ll hv=0;
    for(ll i=1;i<=n;i++)
    {
        cin>>x;
        a[i]=x;
        if(x==0) {hv++;continue;}
        if(x==prev)
        {
            Map[x]=i;
            continue;
        }
        if(Map.count(x))
        {

            it=Map.lower_bound(x);
            if(it==Map.begin())
            {
                Map[x]=i; prev=x;
            }
            else
            {

                for(it=Map.begin();it!=Map.end();it++)
                //cout<<it->S<<' '<<Map[x]<<endl;
                {if(it->S>Map[x]) ck=false; break;}
                Map[x]=i; prev=x;

            }

        }
        else
        {
            Map[x]=i;

        }
    }
        if(!ck || ( !Map.count(q) && hv==0) )  cout<<"NO"<<endl;
        else
        {
            cout<<"YES"<<endl;
            ll prev=-1;
            ll qq;
            if(!Map.count(q)) qq=q;
            else qq=1;
            for(ll i=n;i>=1;i--)
            {
                if(a[i]==0)
                {
                    if(prev==-1) a[i]=qq;
                    else a[i]=prev;
                }
                else prev=a[i];
            }
            for(ll i=1;i<=n;i++) cout<<a[i]<<' ';
        }

}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define fr(i,a,b) for(int i=a;i<=b;i++)
#define re_fr(i,a,b) for(int i=a;i>=b;i--)
#define frl(i,a,b) for(ll i=a;i<=b;i++)
#define re_frl(i,a,b) for(ll i=a;i>=b;i--)
#define mp make_pair
#define F first
#define S second
#define MX 5000010
#define eps 1e-9
#define PI acos(-1)
#define inf 1e18
#define pll pair<ll,ll>
//#define mod 1000000007
#define mod2 100000000
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define PI acos(-1)
#define fast() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
map<ll,ll>Map;
map<ll,ll>::iterator it;
ll a[200010];
int main()
{

    fast();
    ll n,q,x;
    cin>>n>>q;
    ll prev=-1;
    bool ck=true;
    ll hv=0;
    for(ll i=1;i<=n;i++)
    {
        cin>>x;
        a[i]=x;
        if(x==0) {hv++;continue;}
        if(x==prev)
        {
            Map[x]=i;
            continue;
        }
        if(Map.count(x))
        {

            it=Map.lower_bound(x);
            if(it==Map.begin())
            {
                Map[x]=i; prev=x;
            }
            else
            {
                it--;
                //cout<<it->S<<' '<<Map[x]<<endl;
                if(it->S>Map[x]) ck=false;
                Map[x]=i; prev=x;

            }

        }
        else
        {
            Map[x]=i;

        }
    }
        if(!ck || ( !M.count(q) && hv==0) )  cout<<"NO"<<endl;
        else
        {
            cout<<"YES"<<endl;
            ll prev=-1;
            for(ll i=n;i>=1;i--)
            {
                if(a[i]==0)
                {
                    if(prev==-1) a[i]=q;
                    else a[i]=prev;
                }
                else prev=a[i];
            }
            for(ll i=1;i<=n;i++) cout<<a[i]<<' ';
        }

}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define fr(i,a,b) for(int i=a;i<=b;i++)
#define re_fr(i,a,b) for(int i=a;i>=b;i--)
#define frl(i,a,b) for(ll i=a;i<=b;i++)
#define re_frl(i,a,b) for(ll i=a;i>=b;i--)
#define mp make_pair
#define F first
#define S second
#define MX 5000010
#define eps 1e-9
#define PI acos(-1)
#define inf 1e18
#define pll pair<ll,ll>
//#define mod 1000000007
#define mod2 100000000
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define PI acos(-1)
#define fast() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
ll Map[200005];
ll arr[200005];
ll sparse[200005][20];

/// making sparse table in nlogn
void make(ll n){
    //memset(sparse,-1,sizeof(sparse));
    for(ll i=0;i<n;i++) sparse[i][0]=i;
    for(ll j=1;(1<<j)<=n;j++){
        for(ll i=0;(i+(1<<j)-1)<n;i++){

            if(arr[sparse[i][j-1]] < arr[sparse[i+(1<<(j-1))][j-1]])
			/// je duita index niye kaj krbo tar moddo 				konta minimum oita check krtisi
             sparse[i][j]=sparse[i][j-1]; /// minimum valuer index 							raktisi eikhane
            else sparse[i][j]=sparse[i+(1<<(j-1))][j-1];
			/// minimum valuer index raktisi eikhane

    }
}}
/// returning value in O(1)
ll RmQ(ll low,ll high){
    ll l=high-low+1;
    ll k=log2(l);
    return min(arr[sparse[low][k]],arr[sparse[low+l-(1<<k)][k]]) ;
    /// je duita index niye kaj krbo tar moddo konta minimum oita 		return krtisi

}

int main()
{

    fast();
    ll n,q,x;
    cin>>n>>q;
    ll prev=-1;
    bool ck=true;
    ll hv=0;
    for(ll i=0;i<n;i++)
    {
        cin>>arr[i];
        if(arr[i]==0) arr[i]=1e18;
    }
    make(n);
    for(ll i=0;i<n;i++)
    {
        x=arr[i];
        if(x==1e18) {hv++;continue;}
        if(x==prev)
        {
            Map[x]=i+1;
            continue;
        }
        if(Map[x]!=0)
        {
            //cout<<Map[x]<<' '<<i<<endl;
            ll p=RmQ(Map[x],i);
            //cout<<p<<endl;
            if(p<x) ck=false;
        }
        Map[x]=i+1;prev=x;
    }
        if(!ck || ( !Map[q] && hv==0) )  cout<<"NO"<<endl;
        else
        {
            cout<<"YES"<<endl;
            ll prev=-1;
            ll qq;
            bool hvvv=false;
            if(!Map[q]) qq=q;
            else qq=1, hvvv=true;
            for(ll i=n-1;i>=0;i--)
            {
                if(arr[i]==1e18)
                {
                    if(!hvvv) arr[i]=qq, hvvv=true;
                    else if(prev==-1) arr[i]=qq;
                    else arr[i]=prev;
                }
                prev=arr[i];
            }
            for(ll i=0;i<n;i++) cout<<arr[i]<<' ';
        }

}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define fr(i,a,b) for(int i=a;i<=b;i++)
#define re_fr(i,a,b) for(int i=a;i>=b;i--)
#define frl(i,a,b) for(ll i=a;i<=b;i++)
#define re_frl(i,a,b) for(ll i=a;i>=b;i--)
#define mp make_pair
#define F first
#define S second
#define MX 5000010
#define eps 1e-9
#define PI acos(-1)
#define inf 1e18
#define pll pair<ll,ll>
//#define mod 1000000007
#define mod2 100000000
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define PI acos(-1)
#define fast() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
map<ll,ll>Map;
map<ll,ll>::iterator it,it1;
ll a[200010];
int main()
{

    fast();
    ll n,q,x;
    cin>>n>>q;
    ll prev=-1;
    bool ck=true;
    ll hv=0;
    for(ll i=1;i<=n;i++)
    {
        cin>>x;
        a[i]=x;
        if(x==0) {hv++;continue;}
        if(x==prev)
        {
            Map[x]=i;
            continue;
        }
        if(Map.count(x))
        {

            it=Map.lower_bound(x);
            if(it==Map.begin())
            {
                Map[x]=i; prev=x;
            }
            else
            {

                for(it1=Map.begin();it1!=it;it1++)
                //cout<<it->S<<' '<<Map[x]<<endl;
                if(it1->S>Map[x]) {ck=false; break;}
                Map[x]=i; prev=x;

            }

        }
        else
        {
            Map[x]=i;
            prev=x;

        }
    }
        if(!ck || ( !Map.count(q) && hv==0) )  cout<<"NO"<<endl;
        else
        {
            cout<<"YES"<<endl;
            ll prev=-1;
            ll qq;
            bool hvvv=false;
            if(!Map.count(q)) qq=q;
            else qq=1, hvvv=true;
            for(ll i=n;i>=1;i--)
            {
                if(a[i]==0)
                {
                    if(!hvvv) a[i]=qq, hvvv=true;
                    else if(prev==-1) a[i]=qq;
                    else a[i]=prev;
                }
                prev=a[i];
            }
            for(ll i=1;i<=n;i++) cout<<a[i]<<' ';
        }

}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define fr(i,a,b) for(int i=a;i<=b;i++)
#define re_fr(i,a,b) for(int i=a;i>=b;i--)
#define frl(i,a,b) for(ll i=a;i<=b;i++)
#define re_frl(i,a,b) for(ll i=a;i>=b;i--)
#define mp make_pair
#define F first
#define S second
#define MX 5000010
#define eps 1e-9
#define PI acos(-1)
#define inf 1e18
#define pll pair<ll,ll>
//#define mod 1000000007
#define mod2 100000000
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define PI acos(-1)
#define fast() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
map<ll,ll>Map;
map<ll,ll>::iterator it;
ll a[200010];
int main()
{

    fast();
    ll n,q,x;
    cin>>n>>q;
    ll prev=-1;
    bool ck=true;
    for(ll i=1;i<=n;i++)
    {
        cin>>x;
        a[i]=x;
        if(x==0) continue;
        if(x==prev)
        {
            Map[x]=i;
            continue;
        }
        if(Map.count(x))
        {

            it=Map.lower_bound(x);
            if(it==Map.begin())
            {
                Map[x]=i; prev=x;
            }
            else
            {
                it--;
                //cout<<it->S<<' '<<Map[x]<<endl;
                if(it->S>Map[x]) ck=false;
                Map[x]=i; prev=x;

            }

        }
        else
        {
            Map[x]=i;

        }
    }
        if(!ck || ! M.count(q)) cout<<"NO"<<endl;
        else
        {
            cout<<"YES"<<endl;
            ll prev=-1;
            for(ll i=n;i>=1;i--)
            {
                if(a[i]==0)
                {
                    if(prev==-1) a[i]=q;
                    else a[i]=prev;
                }
                else prev=a[i];
            }
            for(ll i=1;i<=n;i++) cout<<a[i]<<' ';
        }

}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

#define REP(i, a, b) for(int i = a; i <= b; ++i)
#define DEC(i, a, b) for(int i = a; i >= b; --i)
#define getmid(l,r) ((l) + ((r) - (l)) / 2)
#define MEM(a,b) memset(a,b,sizeof(a))
#define FI first
#define SE second
#define SZ(x) ((int)(x).size())
#define MP make_pair
#define PB push_back

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<pii > VPI;
typedef vector<pll > VPL;
const int mod = 1000000007;
const int B = 239;

int t, n, m;
ll C[500010];
int H[500010], Hash[500010], vis[500010];
VI G[500010];

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

int main(){
    ios_base::sync_with_stdio(false);
    H[0] = 1;
    REP(i, 1, 500000) H[i] = 1LL * H[i - 1] * B % mod;
	cin >> t;
    REP(q, 1, t){
        cin >> n >> m;
        REP(i, 1, n) cin >> C[i], Hash[i] = 0, vis[i] = 0;
        REP(i, 1, m){
            int a, b;
            cin >> a >> b;
            vis[b] = 1;
            Hash[b] = (Hash[b] + H[a]) % mod;
        }
        map<int, ll> mp;
        REP(i, 1, n) if(vis[i]){
            mp[Hash[i]] += C[i];
        }
        ll ans = -1;
        for(auto pi: mp){
            if(pi.SE > 0){
                if(ans == -1) ans = pi.SE;
                else ans = gcd(ans, pi.SE);
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

#define REP(i, a, b) for(int i = a; i <= b; ++i)
#define DEC(i, a, b) for(int i = a; i >= b; --i)
#define getmid(l,r) ((l) + ((r) - (l)) / 2)
#define MEM(a,b) memset(a,b,sizeof(a))
#define FI first
#define SE second
#define SZ(x) ((int)(x).size())
#define MP make_pair
#define PB push_back

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<pii > VPI;
typedef vector<pll > VPL;
const int mod = 1000000007;
const int B = 239;

int t, n, m;
int H[500010];
pll Hash[500010];

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

int main(){
    ios_base::sync_with_stdio(false);
    H[0] = 1;
    REP(i, 1, 500000) H[i] = 1LL * H[i - 1] * B % mod;
    cin >> t;
    REP(q, 1, t){
        cin >> n >> m;
        REP(i, 1, n) cin >> Hash[i].SE, Hash[i].FI = -1;
        REP(i, 1, m){
            int a, b;
            cin >> a >> b;
            if(Hash[b].FI == -1) Hash[b].FI = H[a];
            else Hash[b].FI = (Hash[b].FI + H[a]) % mod;
        }
        ll ans = -1, cur = 0;
        sort(Hash + 1, Hash + n + 1);
        REP(i, 1, n) if(Hash[i].FI != -1){
            cur += Hash[i].SE;
            if(i < n && Hash[i].FI != Hash[i + 1].FI){
                if(ans == -1) ans = cur;
                else ans = __gcd(ans, cur);
                cur = 0;
            }
        }
        if(cur > 0) ans = (ans == -1) ? cur : __gcd(ans, cur);
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define uLL unsigned long long
#define N 500005
#include<ext/pb_ds/hash_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }
    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
int read(){
	int x, f = 1;
	char ch;
	while(ch = getchar(), ch < '0' || ch > '9') if(ch == '-') f = -1;
	x = ch - '0';
	while(ch = getchar(), ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - 48;
	return x * f;
}
uLL ans, v[N], rd[N], c[N];
int id[N];
mt19937_64 rdn(time(0));
gp_hash_table<uLL, uLL, custom_hash> f;
int main(){
	int i, j, n, m, T, a, b;
	scanf("%d", &T);
	while(T--){
		f.clear();
		ans = 0;
		scanf("%d%d", &n, &m);
		for(i = 1; i <= n; i++) rd[i] = rdn(), c[i] = read(), v[i] = 0;
		for(i = 1; i <= m; i++){
			a = read(); b = read();
			v[b] ^= rd[a];
		}
		for(i = 1; i <= n; i++){
			if(!v[i]) continue;
			f[v[i]] += c[i];
		}
		for(auto &u: f) ans = __gcd(ans, u.second);
		printf("%llu\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define uLL unsigned long long
#define N 500005
using namespace std;
uLL ans, v[N], rd[N], g[N], c[N];
int id[N];
mt19937_64 rdn(time(0));
int cmp(int a, int b){
	return v[a] < v[b];
}
int main(){
	int i, j, n, m, T, a, b;
	scanf("%d", &T);
	while(T--){
		ans = 0;
		scanf("%d%d", &n, &m);
		for(i = 1; i <= n; i++) g[i] = 0, rd[i] = rdn(), scanf("%llu", &c[i]), v[i] = 0, id[i] = i;
		for(i = 1; i <= m; i++){
			scanf("%d%d", &a, &b);
			v[b] ^= rd[a];
		}
		sort(id + 1, id + n + 1, cmp);
		i = 1;
		while(!v[id[i]]) i++;
		for(j = i; i <= n; i = j){
			while(v[id[i]] == v[id[j]] && j <= n){
				g[id[i]] += c[id[j]];
				j++;
			}
		}
		for(i = 1; i <= n; i++) ans = __gcd(ans, g[i]);
		printf("%llu\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define uLL unsigned long long
#define N 500005
using namespace std;
uLL ans, v[N], rd[N], g[N], c[N];
int id[N];
mt19937_64 rdn(time(0));
int cmp(int a, int b){
	return v[a] < v[b];
}
int main(){
	int i, j, n, m, T, a, b;
	scanf("%d", &T);
	while(T--){
		unordered_map<uLL, uLL> f;
		ans = 0;
		scanf("%d%d", &n, &m);
		for(i = 1; i <= n; i++) g[i] = 0, rd[i] = rdn(), scanf("%llu", &c[i]), v[i] = 0, id[i] = i;
		for(i = 1; i <= m; i++){
			scanf("%d%d", &a, &b);
			v[b] ^= rd[a];
		}
		for(i = 1; i <= n; i++){
			if(!v[i]) continue;
			f[v[i]] += c[i];
		}
		for(auto u: f) ans = __gcd(ans, u.second);
		//for(i = 1; i <= n; i++) ans = __gcd(ans, g[i]);
		printf("%llu\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define N 500005
#define uLL unsigned long long
using namespace std;
uLL z = 1;
uLL c[N], g[N], hx, p, base, ans;
mt19937 rdm(time(0));
vector<int> vec[N];
uLL rnd(int a, int b){
	uLL t = rdm() % b;
	if(t <= a) t += a;
}
int main(){
	int i, j, n, m, T, a, b, cnt = 0;
	scanf("%d", &T);
	while(T--){
		ans = 0;
		unordered_map<uLL, uLL> f;
		p = rnd(1e8, 1e9);
		base = rnd(1e8, 1e9);
		scanf("%d%d", &n, &m);
		for(i = 1; i <= n; i++) vec[i].clear(), g[i] = 0, scanf("%llu", &c[i]);
		for(i = 1; i <= m; i++){
			scanf("%d%d", &a, &b);
			vec[b].push_back(a);
		}
		for(i = 1; i <= n; i++){
			if(!vec[i].size()) continue;
			sort(vec[i].begin(), vec[i].end());
			hx = 0;
			for(j = 0; j < vec[i].size(); j++) hx = (z * hx * base % p + vec[i][j]) % p;
			if(!f[hx]) f[hx] = i;
			g[f[hx]] += c[i];
		}
		for(i = 1; i <= n; i++) ans = __gcd(ans, g[i]);
		printf("%llu\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define uLL unsigned long long
#define N 500005
#include<ext/pb_ds/hash_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }
    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
LL read(){
	LL x, f = 1;
	char ch;
	while(ch = getchar(), ch < '0' || ch > '9') if(ch == '-') f = -1;
	x = ch - '0';
	while(ch = getchar(), ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - 48;
	return x * f;
}
uLL ans, v[N], c[N], rd[N];
int id[N];
mt19937_64 rdn(time(0));
gp_hash_table<uLL, uLL, custom_hash> f;
int main(){
	int i, j, n, m, T, a, b;
	scanf("%d", &T);
	while(T--){
		f.clear();
		ans = 0;
		scanf("%d%d", &n, &m);
		for(i = 1; i <= n; i++) rd[i] = rdn(), c[i] = read(), v[i] = 0;
		for(i = 1; i <= m; i++){
			a = read(); b = read();
			v[b] ^= rd[a];
		}
		for(i = 1; i <= n; i++){
			if(!v[i]) continue;
			f[v[i]] += c[i];
		}
		for(auto &u: f) ans = __gcd(ans, u.second);
		printf("%llu\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define uLL unsigned long long
#define N 500005
#include<ext/pb_ds/hash_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }
    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
uLL ans, v[N], rd[N], c[N];
int id[N];
mt19937_64 rdn(time(0));
int main(){
	int i, j, n, m, T, a, b;
	scanf("%d", &T);
	while(T--){
		gp_hash_table<uLL, uLL, custom_hash> f;
		ans = 0;
		scanf("%d%d", &n, &m);
		for(i = 1; i <= n; i++) rd[i] = rdn(), scanf("%llu", &c[i]), v[i] = 0;
		for(i = 1; i <= m; i++){
			scanf("%d%d", &a, &b);
			v[b] ^= rd[a];
		}
		for(i = 1; i <= n; i++){
			if(!v[i]) continue;
			f[v[i]] += c[i];
		}
		for(auto u: f) ans = __gcd(ans, u.second);
		printf("%llu\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define uLL unsigned long long
#define N 500005
#include<ext/pb_ds/hash_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }
    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
uLL ans, v[N], rd[N], c[N];
int id[N];
mt19937_64 rdn(time(0));
gp_hash_table<uLL, uLL, custom_hash> f;
int main(){
	int i, j, n, m, T, a, b;
	scanf("%d", &T);
	while(T--){
		f.clear();
		ans = 0;
		scanf("%d%d", &n, &m);
		for(i = 1; i <= n; i++) rd[i] = rdn(), scanf("%llu", &c[i]), v[i] = 0;
		for(i = 1; i <= m; i++){
			scanf("%d%d", &a, &b);
			v[b] ^= rd[a];
		}
		for(i = 1; i <= n; i++){
			if(!v[i]) continue;
			f[v[i]] += c[i];
		}
		for(auto &u: f) ans = __gcd(ans, u.second);
		printf("%llu\n", ans);
	}
	return 0;
}


#include<bits/stdc++.h>
#define F first
#define S second
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pi;
typedef vector<int> vi;
const int maxn=6e5+12;
ull a[maxn],b[maxn];
int n,m;
ll c[maxn];
map <ull,ll> f;
mt19937_64 gen(time(0));
int main(){
    int _; scanf("%d",&_);
    while (_--){
        scanf("%d%d",&n,&m); f.clear();
        for (int i=1;i<=n;i++) {
            a[i]=gen(); b[i]=0;
            scanf("%lld",&c[i]);
        }
        for (int i=1;i<=m;i++){
            int u,v; scanf("%d%d",&u,&v);
            b[v]^=a[u];
        }
        for (int i=1;i<=n;i++) f[b[i]]+=c[i];
        ll ans=0;
        for (auto x:f) if (x.F) ans=__gcd(ans,x.S);
        printf("%lld\n", ans);
    }
    return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define uLL unsigned long long
#define N 500005
using namespace std;
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }
    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
uLL ans, v[N], rd[N], c[N];
int id[N];
mt19937_64 rdn(time(0));
int main(){
	int i, j, n, m, T, a, b;
	scanf("%d", &T);
	while(T--){
		unordered_map<uLL, uLL, custom_hash> f;
		ans = 0;
		scanf("%d%d", &n, &m);
		for(i = 1; i <= n; i++) rd[i] = rdn(), scanf("%llu", &c[i]), v[i] = 0;
		for(i = 1; i <= m; i++){
			scanf("%d%d", &a, &b);
			v[b] ^= rd[a];
		}
		for(i = 1; i <= n; i++){
			if(!v[i]) continue;
			f[v[i]] += c[i];
		}
		for(auto u: f) ans = __gcd(ans, u.second);
		printf("%llu\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define N 500005
#define LL long long
using namespace std;
LL c[N], ans;
vector<int> vec[N];
int main(){
	int i, j, n, m, T, a, b;
	scanf("%d", &T);
	while(T--){
		ans = 0;
		scanf("%d%d", &n, &m);
		for(i = 1; i <= n; i++) vec[i].clear(), scanf("%lld", &c[i]);
		for(i = 1; i <= m; i++){
			scanf("%d%d", &a, &b);
			vec[b].push_back(a);
		}
		for(i = 1; i <= n; i++) if(vec[i].size()) sort(vec[i].begin(), vec[i].end());
		map<vector<int>, LL> f;
		for(i = 1; i <= n; i++) if(vec[i].size()) f[vec[i]] += c[i];
		for(auto &u: f) ans = __gcd(ans, u.second);
		printf("%lld\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define N 500005
using namespace std;
typedef long double LD;
typedef unsigned long long uLL;
uLL z = 1;
uLL c[N], g[N], hx, p, base, ans, con;
mt19937_64 rdm(time(0));
vector<int> vec[N];
uLL rnd(int a, int b){
	uLL t = rdm() % b;
	if(t <= a) t += a;
	return t;
}
uLL mul(uLL a, uLL b, uLL p){
	return ((a * b - uLL(LD(a) / p * b) * p) % p + p) % p;
}
int main(){
	int i, j, k, n, m, T, a, b, cnt = 0;
	scanf("%d", &T);
	while(T--){
		ans = 0;
		unordered_map<uLL, uLL> f;
		base = rdm();
		p = rdm();
		scanf("%d%d", &n, &m);
		for(i = 1; i <= n; i++) vec[i].clear(), g[i] = 0, scanf("%llu", &c[i]);
		for(i = 1; i <= m; i++){
			scanf("%d%d", &a, &b);
			vec[b].push_back(a);
		}
		for(i = 1; i <= n; i++){
			if(!vec[i].size()) continue;
			sort(vec[i].begin(), vec[i].end());
			hx = 0;
			for(j = 0; j < vec[i].size(); j++) hx = (mul(hx, base, p) + vec[i][j]) % p;
			if(!f[hx]) f[hx] = i;
			g[f[hx]] += c[i];
		}
		for(i = 1; i <= n; i++) ans = __gcd(ans, g[i]);
		printf("%llu\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define N 500005
#define uLL unsigned long long
using namespace std;
uLL z = 1;
uLL c[N], g[N], hx, p, base, ans, con;
mt19937_64 rdm(time(0));
vector<int> vec[N];
uLL rnd(int a, int b){
	uLL t = rdm() % b;
	if(t <= a) t += a;
	return t;
}
int main(){
	int i, j, k, n, m, T, a, b, cnt = 0;
	scanf("%d", &T);
	while(T--){
		ans = 0;
		unordered_map<uLL, uLL> f;
		base = rnd(1e8, 1e9);
		con = rdm();
		scanf("%d%d", &n, &m);
		for(i = 1; i <= n; i++) vec[i].clear(), g[i] = 0, scanf("%llu", &c[i]);
		for(i = 1; i <= m; i++){
			scanf("%d%d", &a, &b);
			vec[b].push_back(a);
		}
		for(i = 1; i <= n; i++){
			if(!vec[i].size()) continue;
			sort(vec[i].begin(), vec[i].end());
			hx = 0;
			for(j = 0; j < vec[i].size(); j++) hx = z * hx * base + vec[i][j] + con;
			if(!f[hx]) f[hx] = i;
			g[f[hx]] += c[i];
		}
		for(i = 1; i <= n; i++) ans = __gcd(ans, g[i]);
		printf("%llu\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define N 500005
#define uLL unsigned long long
using namespace std;
uLL z = 1;
uLL c[N], g[N], hx, p, base, ans, con;
mt19937 rdm(time(0));
vector<int> vec[N];
uLL rnd(int a, int b){
	uLL t = rdm() % b;
	if(t <= a) t += a;
	return t;
}
int main(){
	int i, j, k, n, m, T, a, b, cnt = 0;
	scanf("%d", &T);
	while(T--){
		ans = 0;
		unordered_map<uLL, uLL> f;
		base = rnd(1e8, 1e9);
		con = rnd(1e8, 1e9);
		scanf("%d%d", &n, &m);
		for(i = 1; i <= n; i++) vec[i].clear(), g[i] = 0, scanf("%llu", &c[i]);
		for(i = 1; i <= m; i++){
			scanf("%d%d", &a, &b);
			vec[b].push_back(a);
		}
		for(i = 1; i <= n; i++){
			if(!vec[i].size()) continue;
			sort(vec[i].begin(), vec[i].end());
			hx = 0;
			for(j = 0; j < vec[i].size(); j++) hx = z * hx * base + vec[i][j] + con;
			if(!f[hx]) f[hx] = i;
			g[f[hx]] += c[i];
		}
		for(i = 1; i <= n; i++) ans = __gcd(ans, g[i]);
		printf("%llu\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define N 500005
#define uLL unsigned long long
using namespace std;
uLL z = 1;
uLL c[N], g[N], hx, p, base, ans, con;
mt19937_64 rdm(time(0));
vector<int> vec[N];
uLL rnd(int a, int b){
	uLL t = rdm() % b;
	if(t <= a) t += a;
	return t;
}
int main(){
	int i, j, k, n, m, T, a, b, cnt = 0;
	scanf("%d", &T);
	while(T--){
		ans = 0;
		unordered_map<uLL, uLL> f;
		base = rdm();
		con = rdm();
		scanf("%d%d", &n, &m);
		for(i = 1; i <= n; i++) vec[i].clear(), g[i] = 0, scanf("%llu", &c[i]);
		for(i = 1; i <= m; i++){
			scanf("%d%d", &a, &b);
			vec[b].push_back(a);
		}
		for(i = 1; i <= n; i++){
			if(!vec[i].size()) continue;
			sort(vec[i].begin(), vec[i].end());
			hx = 0;
			for(j = 0; j < vec[i].size(); j++) hx = z * hx * base + vec[i][j] + con;
			if(!f[hx]) f[hx] = i;
			g[f[hx]] += c[i];
		}
		for(i = 1; i <= n; i++) ans = __gcd(ans, g[i]);
		printf("%llu\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define uLL unsigned long long
#define base 131
#define N 500005
using namespace std;
const int mod1 = 1e9 + 7;
const int mod2 = 1e9 + 9;
LL z = 1, c[N], g[N], ans;
int hx1, hx2;
vector<int> vec[N];
int main(){
	int i, j, n, m, T, a, b;
	pair<int, int> p;
	scanf("%d", &T);
	while(T--){
		map<pair<int, int>, int> f;
		ans = 0;
		scanf("%d%d", &n, &m);
		for(i = 1; i <= n; i++) g[i] = 0, vec[i].clear(), scanf("%lld", &c[i]);
		for(i = 1; i <= m; i++){
			scanf("%d%d", &a, &b);
			vec[b].push_back(a);
		}
		for(i = 1; i <= n; i++) if(vec[i].size()) sort(vec[i].begin(), vec[i].end());
		for(i = 1; i <= n; i++){
			if(!vec[i].size()) continue;
			hx1 = hx2 = 0;
			for(j = 0; j < vec[i].size(); j++){
				hx1 = (z * hx1 * base % mod1 + vec[i][j]) % mod1;
				hx2 = (z * hx2 * base % mod2 + vec[i][j]) % mod2;
			}
			p = make_pair(hx1, hx2);
			if(!f[p]) f[p] = i;
			g[f[p]] += c[i];
		}
		for(i = 1; i <= n; i++) ans = __gcd(ans, g[i]);
		printf("%lld\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define N 500005
#define uLL unsigned long long
using namespace std;
uLL z = 1;
uLL c[N], g[N], hx, p, base, ans;
mt19937 rdm(time(0));
vector<int> vec[N];
uLL rnd(int a, int b){
	uLL t = rdm() % b;
	if(t <= a) t += a;
	return t;
}
int main(){
	int i, j, k, n, m, T, a, b, cnt = 0;
	scanf("%d", &T);
	while(T--){
		ans = 0;
		unordered_map<uLL, uLL> f;
		base = rnd(1e8, 1e9);
		scanf("%d%d", &n, &m);
		for(i = 1; i <= n; i++) vec[i].clear(), g[i] = 0, scanf("%llu", &c[i]);
		for(i = 1; i <= m; i++){
			scanf("%d%d", &a, &b);
			vec[b].push_back(a);
		}
		for(i = 1; i <= n; i++){
			if(!vec[i].size()) continue;
			sort(vec[i].begin(), vec[i].end());
			hx = 0;
			for(j = 0; j < vec[i].size(); j++) hx = z * hx * base + vec[i][j];
			if(!f[hx]) f[hx] = i;
			g[f[hx]] += c[i];
		}
		for(i = 1; i <= n; i++) ans = __gcd(ans, g[i]);
		printf("%llu\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define uLL unsigned long long
#define base 131
#define N 500005
using namespace std;
const int mod1 = 1e9 + 7;
const int mod2 = 1e9 + 9;
LL z = 1, c[N], g[N], ans;
int hx1, hx2;
vector<int> vec[N];
int main(){
	int i, j, n, m, T, a, b;
	pair<int, int> p;
	scanf("%d", &T);
	while(T--){
		//map<pair<int, int>, int> f;
		map<int, int> f;
		ans = 0;
		scanf("%d%d", &n, &m);
		for(i = 1; i <= n; i++) g[i] = 0, vec[i].clear(), scanf("%lld", &c[i]);
		for(i = 1; i <= m; i++){
			scanf("%d%d", &a, &b);
			vec[b].push_back(a);
		}
		for(i = 1; i <= n; i++) if(vec[i].size()) sort(vec[i].begin(), vec[i].end());
		for(i = 1; i <= n; i++){
			if(!vec[i].size()) continue;
			hx1 = hx2 = 0;
			for(j = 0; j < vec[i].size(); j++){
				hx1 = (z * hx1 * base % mod1 + vec[i][j]) % mod1;
				hx2 = (z * hx2 * base % mod2 + vec[i][j]) % mod2;
			}
			p = make_pair(hx1, hx2);
			if(!f[hx1]) f[hx1] = i;
			g[f[hx1]] += c[i];
		}
		for(i = 1; i <= n; i++) ans = __gcd(ans, g[i]);
		printf("%lld\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define N 500005
#define uLL unsigned long long
using namespace std;
uLL z = 1;
uLL c[N], g[N], hx, p, base, ans;
mt19937 rdm(time(0));
vector<int> vec[N];
uLL rnd(int a, int b){
	uLL t = rdm() % b;
	if(t <= a) t += a;
	return t;
}
int main(){
	int i, j, n, m, T, a, b, cnt = 0;
	scanf("%d", &T);
	while(T--){
		ans = 0;
		unordered_map<uLL, uLL> f;
		base = rnd(1e8, 1e9);
		scanf("%d%d", &n, &m);
		for(i = 1; i <= n; i++) vec[i].clear(), g[i] = 0, scanf("%llu", &c[i]);
		for(i = 1; i <= m; i++){
			scanf("%d%d", &a, &b);
			vec[b].push_back(a);
		}
		for(i = 1; i <= n; i++){
			if(!vec[i].size()) continue;
			sort(vec[i].begin(), vec[i].end());
			hx = 0;
			for(j = 0; j < vec[i].size(); j++) hx = z * hx * base + vec[i][j];
			if(!f[hx]) f[hx] = i;
			g[f[hx]] += c[i];
		}
		for(i = 1; i <= n; i++) ans = __gcd(ans, g[i]);
		printf("%llu\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define N 500005
#define uLL unsigned long long
using namespace std;
uLL z = 1;
uLL c[N], g[N], hx, p, base, ans;
mt19937 rdm(time(0));
vector<int> vec[N];
int main(){
	int i, j, n, m, T, a, b, cnt = 0;
	scanf("%d", &T);
	while(T--){
		ans = 0;
		unordered_map<uLL, uLL> f;
		p = rdm(); base = rdm();
		scanf("%d%d", &n, &m);
		for(i = 1; i <= n; i++) vec[i].clear(), g[i] = 0, scanf("%llu", &c[i]);
		for(i = 1; i <= m; i++){
			scanf("%d%d", &a, &b);
			vec[b].push_back(a);
		}
		for(i = 1; i <= n; i++){
			if(!vec[i].size()) continue;
			sort(vec[i].begin(), vec[i].end());
			hx = 0;
			for(j = 0; j < vec[i].size(); j++) hx = (z * hx * base % p + vec[i][j]) % p;
			if(!f[hx]) f[hx] = i;
			g[f[hx]] += c[i];
		}
		for(i = 1; i <= n; i++) ans = __gcd(ans, g[i]);
		printf("%llu\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define N 500005
#define uLL unsigned long long
using namespace std;
uLL z = 1;
uLL c[N], g[N], hx, p, base, ans;
mt19937 rdm(time(0));
vector<int> vec[N];
uLL rnd(int a, int b){
	uLL t = rdm() % b;
	if(t <= a) t += a;
	return t;
}
int main(){
	int i, j, n, m, T, a, b, cnt = 0;
	scanf("%d", &T);
	while(T--){
		ans = 0;
		unordered_map<uLL, uLL> f;
		p = rnd(1e8, 1e9);
		base = rnd(1e8, 1e9);
		scanf("%d%d", &n, &m);
		for(i = 1; i <= n; i++) vec[i].clear(), g[i] = 0, scanf("%llu", &c[i]);
		for(i = 1; i <= m; i++){
			scanf("%d%d", &a, &b);
			vec[b].push_back(a);
		}
		for(i = 1; i <= n; i++){
			if(!vec[i].size()) continue;
			sort(vec[i].begin(), vec[i].end());
			hx = 0;
			for(j = 0; j < vec[i].size(); j++) hx = (z * hx * base % p + vec[i][j]) % p;
			if(!f[hx]) f[hx] = i;
			g[f[hx]] += c[i];
		}
		for(i = 1; i <= n; i++) ans = __gcd(ans, g[i]);
		printf("%llu\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define uLL unsigned long long
#define N 500005
#include<ext/pb_ds/hash_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }
    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
uLL ans, v[N], rd[N], c[N];
int id[N];
mt19937_64 rdn(time(0));
int main(){
	int i, j, n, m, T, a, b;
	scanf("%d", &T);
	while(T--){
		cc_hash_table<uLL, uLL, custom_hash> f;
		ans = 0;
		scanf("%d%d", &n, &m);
		for(i = 1; i <= n; i++) rd[i] = rdn(), scanf("%llu", &c[i]), v[i] = 0;
		for(i = 1; i <= m; i++){
			scanf("%d%d", &a, &b);
			v[b] ^= rd[a];
		}
		for(i = 1; i <= n; i++){
			if(!v[i]) continue;
			f[v[i]] += c[i];
		}
		for(auto u: f) ans = __gcd(ans, u.second);
		printf("%llu\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define N 500005
#define uLL unsigned long long
using namespace std;
uLL z = 1;
uLL c[N], g[N], hx, p, base, ans, con;
mt19937 rdm(time(0));
vector<int> vec[N];
uLL rnd(int a, int b){
	uLL t = rdm() % b;
	if(t <= a) t += a;
	return t;
}
int main(){
	int i, j, k, n, m, T, a, b, cnt = 0;
	scanf("%d", &T);
	while(T--){
		ans = 0;
		unordered_map<uLL, uLL> f;
		base = rnd(1e8, 1e9);
		con = rdm();
		scanf("%d%d", &n, &m);
		for(i = 1; i <= n; i++) vec[i].clear(), g[i] = 0, scanf("%llu", &c[i]);
		for(i = 1; i <= m; i++){
			scanf("%d%d", &a, &b);
			vec[b].push_back(a);
		}
		for(i = 1; i <= n; i++){
			if(!vec[i].size()) continue;
			sort(vec[i].begin(), vec[i].end());
			hx = 0;
			for(j = 0; j < vec[i].size(); j++) hx = z * hx * base + vec[i][j] + con;
			if(!f[hx]) f[hx] = i;
			g[f[hx]] += c[i];
		}
		for(i = 1; i <= n; i++) ans = __gcd(ans, g[i]);
		printf("%llu\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define N 500005
using namespace std;
typedef long double LD;
typedef unsigned long long uLL;
uLL z = 1;
uLL c[N], g[N], hx, p, base, ans, con;
mt19937_64 rdm(time(0));
vector<int> vec[N];
uLL rnd(int a, int b){
	uLL t = rdm() % b;
	if(t <= a) t += a;
	return t;
}
uLL mul(uLL a, uLL b, uLL p){
	return ((a * b - uLL(LD(a) / p * b + 0.5) * p) % p + p) % p;
}
int main(){
	int i, j, k, n, m, T, a, b, cnt = 0;
	scanf("%d", &T);
	while(T--){
		ans = 0;
		unordered_map<uLL, uLL> f;
		base = rdm();
		p = rdm();
		scanf("%d%d", &n, &m);
		for(i = 1; i <= n; i++) vec[i].clear(), g[i] = 0, scanf("%llu", &c[i]);
		for(i = 1; i <= m; i++){
			scanf("%d%d", &a, &b);
			vec[b].push_back(a);
		}
		for(i = 1; i <= n; i++){
			if(!vec[i].size()) continue;
			sort(vec[i].begin(), vec[i].end());
			hx = 0;
			for(j = 0; j < vec[i].size(); j++) hx = (mul(hx, base, p) + vec[i][j]) % p;
			if(!f[hx]) f[hx] = i;
			g[f[hx]] += c[i];
		}
		for(i = 1; i <= n; i++) ans = __gcd(ans, g[i]);
		printf("%llu\n", ans);
	}
	return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;

#define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0)
#define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0)

template<typename T1,typename T2>ostream& operator<<(ostream& os,const pair<T1,T2>& a) {os << "(" << a.first << ", " << a.second << ")";return os;}

mt19937_64 r(std::chrono::high_resolution_clock::now().time_since_epoch().count());

ull const MASK30 = (1<<30)-1;
ull const MASK31 = INT_MAX;
ull const MOD = (1LL<<61)-1;
ull const MOD5 = MOD*5;
ull const base = r()%MOD;

inline ull mul(ull a,ull b) {
    ull au = a>>31;
    ull ad = (a&MASK31);
    ull bu = b>>31;
    ull bd = (b&MASK31);
    ull mid = au*bd+ad*bu;
    return ((au*bu)<<1)+ad*bd+(mid>>30)+((mid&MASK30)<<31);
}

inline ull calcmod(ull a) {
    a = (a&MOD)+(a>>61);
    if (a > MOD) a -= MOD;
    return a;
}

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

ull power[500010];
ull h[500010];
ull a[500010];
P b[500010];

int main() {
    power[0] = 1;
    for (int i = 0;i < 500000;++i) power[i+1] = calcmod(mul(power[i],base));
    int q;
    cin >> q;
    while (q--) {
        int n,m;
        scanf("%d%d",&n,&m);
        for (int i = 0;i < n;++i) scanf("%lld",&a[i]);
        for (int i = 0;i < m;++i) scanf("%d%d",&b[i].first,&b[i].second),b[i].second--;
        sort(b,b+m);
        fill(h,h+n,0);
        unordered_map<ull,ll> mp;
        for (int i = 0;i < m;++i) {
            h[b[i].second] = calcmod(mul(h[b[i].second],base)+b[i].first);
        }
        for (int i = 0;i < n;++i) mp[h[i]] += a[i];
        ll ans = 0;
        for (auto& p : mp) ans = gcd(ans,p.second);
        cout << ans << endl;
    }
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;

#define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0)
#define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0)

template<typename T1,typename T2>ostream& operator<<(ostream& os,const pair<T1,T2>& a) {os << "(" << a.first << ", " << a.second << ")";return os;}

mt19937_64 r(std::chrono::high_resolution_clock::now().time_since_epoch().count());

ull const MASK30 = (1<<30)-1;
ull const MASK31 = INT_MAX;
ull const MOD = (1LL<<61)-1;
ull const MOD5 = MOD*5;
ull const base = r()%MOD;

inline ull mul(ull a,ull b) {
    ull au = a>>31;
    ull ad = (a&MASK31);
    ull bu = b>>31;
    ull bd = (b&MASK31);
    ull mid = au*bd+ad*bu;
    return ((au*bu)<<1)+ad*bd+(mid>>30)+((mid&MASK30)<<31);
}

inline ull calcmod(ull a) {
    a = (a&MOD)+(a>>61);
    if (a > MOD) a -= MOD;
    return a;
}

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

ull h[500010];
ll a[500010];
P b[500010];
bool f[500010];

int main() {
    int q;
    cin >> q;
    while (q--) {
        int n,m;
        scanf("%d%d",&n,&m);
        for (int i = 0;i < n;++i) scanf("%lld",&a[i]);
        for (int i = 0;i < m;++i) scanf("%d%d",&b[i].first,&b[i].second),b[i].second--;
        sort(b,b+m);
        fill(h,h+n,0);
        fill(f,f+n,false);
        unordered_map<ull,ll> mp;
        for (int i = 0;i < m;++i) {
            h[b[i].second] = calcmod(mul(h[b[i].second],base)+b[i].first);
            f[b[i].second] = true;
        }
        for (int i = 0;i < n;++i) if (f[i]) mp[h[i]] += a[i];
        ll ans = 0;
        for (auto& p : mp) ans = gcd(ans,p.second);
        cout << ans << endl;
    }
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;

#define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0)
#define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0)

template<typename T1,typename T2>ostream& operator<<(ostream& os,const pair<T1,T2>& a) {os << "(" << a.first << ", " << a.second << ")";return os;}

mt19937_64 r(std::chrono::high_resolution_clock::now().time_since_epoch().count());

ull const MASK30 = (1<<30)-1;
ull const MASK31 = INT_MAX;
ull const MOD = (1LL<<61)-1;
ull const MOD5 = MOD*5;
ull const base = r()%MOD;

inline ull mul(ull a,ull b) {
    ull au = a>>31;
    ull ad = (a&MASK31);
    ull bu = b>>31;
    ull bd = (b&MASK31);
    ull mid = au*bd+ad*bu;
    return ((au*bu)<<1)+ad*bd+(mid>>30)+((mid&MASK30)<<31);
}

inline ull calcmod(ull a) {
    a = (a&MOD)+(a>>61);
    if (a > MOD) a -= MOD;
    return a;
}

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

ull h[500010];
ll a[500010];
P b[500010];
bool f[500010];

int main() {
    int q;
    cin >> q;
    while (q--) {
        int n,m;
        scanf("%d%d",&n,&m);
        for (int i = 0;i < n;++i) scanf("%lld",&a[i]);
        for (int i = 0;i < m;++i) scanf("%d%d",&b[i].first,&b[i].second),b[i].second--;
        sort(b,b+m);
        fill(h,h+n,0);
        fill(f,f+n,false);
        unordered_map<ull,ll> mp;
        for (int i = 0;i < m;++i) {
            h[b[i].second] = calcmod(mul(h[b[i].second],base)+b[i].first);
            f[b[i].second] = true;
        }
        for (int i = 0;i < n;++i) if (f[i]) mp[h[i]] += a[i];
        ll ans = 0;
        for (auto& p : mp) ans = gcd(ans,p.second);
        cout << ans << "\n";
    }
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;

#define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0)
#define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0)

template<typename T1,typename T2>ostream& operator<<(ostream& os,const pair<T1,T2>& a) {os << "(" << a.first << ", " << a.second << ")";return os;}

mt19937_64 r(std::chrono::high_resolution_clock::now().time_since_epoch().count());

ull const MASK30 = (1<<30)-1;
ull const MASK31 = INT_MAX;
ull const MOD = (1LL<<61)-1;
ull const MOD5 = MOD*5;
ull const base = r()%MOD;

inline ull mul(ull a,ull b) {
    ull au = a>>31;
    ull ad = (a&MASK31);
    ull bu = b>>31;
    ull bd = (b&MASK31);
    ull mid = au*bd+ad*bu;
    return ((au*bu)<<1)+ad*bd+(mid>>30)+((mid&MASK30)<<31);
}

inline ull calcmod(ull a) {
    a = (a&MOD)+(a>>61);
    if (a > MOD) a -= MOD;
    return a;
}

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

ull h[500010];
ll a[500010];
P b[500010];
bool f[500010];

int main() {
    unordered_map<ull,ll> mp;
    mp.reserve(500000);
    int q;
    cin >> q;
    while (q--) {
        int n,m;
        scanf("%d%d",&n,&m);
        for (int i = 0;i < n;++i) scanf("%lld",&a[i]);
        for (int i = 0;i < m;++i) scanf("%d%d",&b[i].first,&b[i].second),b[i].second--;
        sort(b,b+m);
        fill(h,h+n,0);
        fill(f,f+n,false);
        for (int i = 0;i < m;++i) {
            h[b[i].second] = calcmod(mul(h[b[i].second],base)+b[i].first);
            f[b[i].second] = true;
        }
        for (int i = 0;i < n;++i) if (f[i]) mp[h[i]] += a[i];
        ll ans = 0;
        for (auto& p : mp) ans = gcd(ans,p.second);
        mp.clear();
        cout << ans << endl;
    }
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;

#define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0)
#define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0)

template<typename T1,typename T2>ostream& operator<<(ostream& os,const pair<T1,T2>& a) {os << "(" << a.first << ", " << a.second << ")";return os;}

mt19937_64 r(std::chrono::high_resolution_clock::now().time_since_epoch().count());

ull const MASK30 = (1<<30)-1;
ull const MASK31 = INT_MAX;
ull const MOD = (1LL<<61)-1;
ull const MOD5 = MOD*5;
ull const base = r()%MOD;

inline ull mul(ull a,ull b) {
    ull au = a>>31;
    ull ad = (a&MASK31);
    ull bu = b>>31;
    ull bd = (b&MASK31);
    ull mid = au*bd+ad*bu;
    return ((au*bu)<<1)+ad*bd+(mid>>30)+((mid&MASK30)<<31);
}

inline ull calcmod(ull a) {
    a = (a&MOD)+(a>>61);
    if (a > MOD) a -= MOD;
    return a;
}

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

ull h[500010];
ll a[500010];
P b[500010];
bool f[500010];

int main() {
    unordered_map<ull,ll> mp;
    mp.reserve(500000);
    int q;
    cin >> q;
    while (q--) {
        int n,m;
        scanf("%d%d",&n,&m);
        for (int i = 0;i < n;++i) scanf("%lld",&a[i]);
        for (int i = 0;i < m;++i) scanf("%d%d",&b[i].first,&b[i].second),b[i].second--;
        sort(b,b+m);
        fill(h,h+n,0);
        fill(f,f+n,false);
        for (int i = 0;i < m;++i) {
            h[b[i].second] = calcmod(mul(h[b[i].second],base)+b[i].first);
            f[b[i].second] = true;
        }
        for (int i = 0;i < n;++i) if (f[i]) mp[h[i]] += a[i];
        ll ans = 0;
        for (auto& p : mp) ans = gcd(ans,p.second);
        mp.clear();
        cout << ans << "\n";
    }
}


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

// Hate My Life //

// primes - 31 53 167 367 1087 1187 125687 125887 325607 325697
// primes - 569609 569809 1299709 2750159 15485857 15485863 15485867 998244353

#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define ppb pop_back()
#define ppf pop_front()
#define all(vec) vec.begin(), vec.end()
#define fol(i,a,b) for(int i=a;i<b;i++)
#define loop(i,a,b) for(int i=a;i>=b;i--)
#define forr(x,arr) for(auto& x:arr)
#define mod 1000000007
#define INF 0x3f3f3f3f3f3f3f3f
#define EPS 1e-7
#define sz(x) ((lli)(x).size())

using   lli     =   long long;
using   lld     =   long double;
using   ulli    =   unsigned long long int;
using   pll     =   pair<lli, lli>;
using   ttt     =   pair<lli, pll>;
using   vttt    =   vector<ttt>;
using   vll     =   vector<pll>;
using   vl      =   vector<lli>;
using   vi      =   vector<int>;
using   vvi     =   vector<vector<int>>;
using   cd      =   complex<lld>;
const double PI = acos(-1);

#ifdef tejasp
#include "library/debugger.cpp"
#else
#define trace(...) {}
#define cotra(...) {}
#define end_routine() {}
#define localsys 0
#define endl '\n'
#endif

inline lli mpow(lli a, lli b, lli m=mod) {
    lli ans = 1;
    // a=((a%mod)+mod)%mod; b=((b%mod)+mod)%mod;
    while (b) {
        if (b&1) ans=(ans*a)%m;
        a=(a*a)%m; b/=2;
    }
    return ans;
}

lli gcd(lli a, lli b) { return (b==0) ? a : gcd(b, a%b); }

int tejas_919(int kkkk)
{
    lli n, m, k, q, u, v, temp=0, ans=0;
    cin>>n>>m;
    lli h1[n+5]={}, h2[n+5]={}, val[n+5];
    fol(i,1,n+1) cin>>val[i];
    fol(i,0,m)
    {
        cin>>u>>v;
        h1[v]=(h1[v]+mpow(1087,u))%mod;
        h2[v]=(h2[v]+mpow(1187,u))%mod;
    }
    map<pll,lli> mp;
    fol(i,1,n+1)
    {
        if(h1[i]==0&&h2[0]==0) continue;
        mp[{h1[i],h2[i]}]+=val[i];
    }
    forr(x,mp)  { trace(x.ss) ans=gcd(ans,x.ss); }
    cout<<ans<<endl;
    return 0;
}

signed main()
{
    #ifdef tejasp
            freopen("input.txt", "rt", stdin);
            freopen("output.txt", "wt", stdout);
    #endif
    if(!localsys) { ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); }
    cout << fixed << setprecision(10);
    int t=1;
    cin>>t;
    fol(i,0,t) { tejas_919(i+1); }
    end_routine();
    if(localsys) system("pause");
}


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

// Hate My Life //

// primes - 31 53 167 367 1087 1187 125687 125887 325607 325697
// primes - 569609 569809 1299709 2750159 15485857 15485863 15485867 998244353

#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define ppb pop_back()
#define ppf pop_front()
#define all(vec) vec.begin(), vec.end()
#define fol(i,a,b) for(int i=a;i<b;i++)
#define loop(i,a,b) for(int i=a;i>=b;i--)
#define forr(x,arr) for(auto& x:arr)
#define mod 1000000007
#define INF 0x3f3f3f3f3f3f3f3f
#define EPS 1e-7
#define sz(x) ((lli)(x).size())

using   lli     =   long long;
using   lld     =   long double;
using   ulli    =   unsigned long long int;
using   pll     =   pair<lli, lli>;
using   ttt     =   pair<lli, pll>;
using   vttt    =   vector<ttt>;
using   vll     =   vector<pll>;
using   vl      =   vector<lli>;
using   vi      =   vector<int>;
using   vvi     =   vector<vector<int>>;
using   cd      =   complex<lld>;
const double PI = acos(-1);

#ifdef tejasp
#include "library/debugger.cpp"
#else
#define trace(...) {}
#define cotra(...) {}
#define end_routine() {}
#define localsys 0
#define endl '\n'
#endif

inline lli mpow(lli a, lli b, lli m=mod) {
    lli ans = 1;
    // a=((a%mod)+mod)%mod; b=((b%mod)+mod)%mod;
    while (b) {
        if (b&1) ans=(ans*a)%m;
        a=(a*a)%m; b/=2;
    }
    return ans;
}

lli gcd(lli a, lli b) { return (b==0) ? a : gcd(b, a%b); }

int tejas_919(int kkkk)
{
    lli n, m, k, q, u, v, temp=0, ans=0;
    cin>>n>>m;
    lli h1[n+5]={}, h2[n+5]={}, val[n+5];
    fol(i,1,n+1) cin>>val[i];
    fol(i,0,m)
    {
        cin>>u>>v;
        h1[v]=(h1[v]+mpow(1087,u))%mod;
        h2[v]=(h2[v]+mpow(1187,u))%mod;
    }
    map<pll,int> mp;
    fol(i,1,n+1)
    {
        if(h1[i]==0&&h2[0]==0) continue;
        mp[{h1[i],h2[i]}]+=val[i];
    }
    forr(x,mp)  { trace(x.ss) ans=gcd(ans,x.ss); }
    cout<<ans<<endl;
    return 0;
}

signed main()
{
    #ifdef tejasp
            freopen("input.txt", "rt", stdin);
            freopen("output.txt", "wt", stdout);
    #endif
    if(!localsys) { ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); }
    cout << fixed << setprecision(10);
    int t=1;
    cin>>t;
    fol(i,0,t) { tejas_919(i+1); }
    end_routine();
    if(localsys) system("pause");
}


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

// Hate My Life //

// primes - 31 53 167 367 1087 1187 125687 125887 325607 325697
// primes - 569609 569809 1299709 2750159 15485857 15485863 15485867 998244353

#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define ppb pop_back()
#define ppf pop_front()
#define all(vec) vec.begin(), vec.end()
#define fol(i,a,b) for(int i=a;i<b;i++)
#define loop(i,a,b) for(int i=a;i>=b;i--)
#define forr(x,arr) for(auto& x:arr)
#define mod 1000000007
#define INF 0x3f3f3f3f3f3f3f3f
#define EPS 1e-7
#define sz(x) ((lli)(x).size())

using   lli     =   long long;
using   lld     =   long double;
using   ulli    =   unsigned long long int;
using   pll     =   pair<lli, lli>;
using   ttt     =   pair<lli, pll>;
using   vttt    =   vector<ttt>;
using   vll     =   vector<pll>;
using   vl      =   vector<lli>;
using   vi      =   vector<int>;
using   vvi     =   vector<vector<int>>;
using   cd      =   complex<lld>;
const double PI = acos(-1);

#ifdef tejasp
#include "library/debugger.cpp"
#else
#define trace(...) {}
#define cotra(...) {}
#define end_routine() {}
#define localsys 0
#define endl '\n'
#endif

inline lli mpow(lli a, lli b, lli m=mod) {
    lli ans = 1;
    // a=((a%mod)+mod)%mod; b=((b%mod)+mod)%mod;
    while (b) {
        if (b&1) ans=(ans*a)%m;
        a=(a*a)%m; b/=2;
    }
    return ans;
}

lli gcd(lli a, lli b) { return (b==0) ? a : gcd(b, a%b); }

int tejas_919(int kkkk)
{
    lli n, m, k, q, u, v, temp=0, ans=0;
    cin>>n>>m;
    lli h1[n+5]={}, h2[n+5]={}, val[n+5];
    fol(i,1,n+1) cin>>val[i];
    fol(i,0,m)
    {
        cin>>u>>v;
        h1[v]=(h1[v]+mpow(u,1087))%mod;
        h2[v]=(h2[v]+mpow(u,1187))%mod;
    }
    map<pll,lli> mp;
    fol(i,1,n+1)
    {
        if(h1[i]==0&&h2[0]==0) continue;
        mp[{h1[i],h2[i]}]+=val[i];
    }
    forr(x,mp)  { trace(x.ss) ans=gcd(ans,x.ss); }
    cout<<ans<<endl;
    return 0;
}

signed main()
{
    #ifdef tejasp
            freopen("input.txt", "rt", stdin);
            freopen("output.txt", "wt", stdout);
    #endif
    if(!localsys) { ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); }
    cout << fixed << setprecision(10);
    int t=1;
    cin>>t;
    fol(i,0,t) { tejas_919(i+1); }
    end_routine();
    if(localsys) system("pause");
}


#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define pii pair<ll,ll>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(vr) vr.begin(),vr.end()
const int N=1e6+10;
const ll base=784697,mod=1e9+9;
int n,m;
ll val[N];
vector<int> G[N];

ll gcd(ll t1,ll t2)
{
    while (t2)
    {
        ll tmp=t1%t2;
        t1=t2;
        t2=tmp;
    }
    return t1;
}

void read()
{
    cin>>n>>m;
    for (int i=1;i<=n;i++) cin>>val[i],G[i].clear();
    for (int i=1,u,v;i<=m;i++)
    {
        cin>>u>>v;
        G[v].pb(u);
    }
}

void process()
{
    map<pii,ll> can;
    ll res=0;
    for (int i=1;i<=n;i++)
    {
        sort(all(G[i]));
        ll h=0;
        for (ll x : G[i]) h=(h*base+x)%mod;
        can[mp(G[i].size(),h)]+=val[i];
    }
    for (auto ir=can.begin();ir!=can.end();ir++)
    {
        ll tmp=ir->se;
        res=gcd(res,tmp);
    }
    cout<<res<<"\n";
}

int main()
{
    //freopen("ss.inp","r",stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while (t--)
    {
        read();
        process();
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define pii pair<ll,ll>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(vr) vr.begin(),vr.end()
const int N=1e6+10;
const ll base=784697,mod=1e9+9;
int n,m;
ll val[N];
vector<int> G[N];

ll gcd(ll t1,ll t2)
{
    while (t2)
    {
        ll tmp=t1%t2;
        t1=t2;
        t2=tmp;
    }
    return t1;
}

void read()
{
    cin>>n>>m;
    for (int i=1;i<=n;i++) cin>>val[i],G[i].clear();
    for (int i=1,u,v;i<=m;i++)
    {
        cin>>u>>v;
        G[v].pb(u);
    }
}

void process()
{
    map<vector<int>,ll> can;
    ll res=0;
    for (int i=1;i<=n;i++)
    {
        sort(all(G[i]));
        //ll h=0;
        //for (ll x : G[i]) h=(h*base+x)%mod;
        //can[mp(G[i].size(),h)]+=val[i];
        can[G[i]]+=val[i];
    }
    for (auto ir=can.begin();ir!=can.end();ir++)
    {
        ll tmp=ir->se;
        res=gcd(res,tmp);
    }
    cout<<res<<"\n";
}

int main()
{
    //freopen("ss.inp","r",stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while (t--)
    {
        read();
        process();
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(vr) vr.begin(),vr.end()
const int N=1e6+10;
int n,m,cnt[N];
vector<int> G[N];
ll val[N];

ll gcd(ll t1,ll t2)
{
    while (t2)
    {
        ll tmp=t1%t2;
        t1=t2;
        t2=tmp;
    }
    return t1;
}

void read()
{
    cin>>n>>m;
    for (int i=1;i<=n;i++) cin>>val[i],G[i].clear(),cnt[i]=0;
    for (int i=1,u,v;i<=m;i++)
    {
        cin>>u>>v;
        G[u].pb(v);
        cnt[v]++;
    }
}

void process()
{
    ll res=0;
    for (int i=1;i<=n;i++)
    {
        ll v1=0,v2=0;
        for (int v : G[i])
        {
            v1+=val[v];
            if (cnt[v]==1) v2+=val[v];
        }
        res=gcd(res,v1);
        res=gcd(res,v2);
    }
    cout<<res<<"\n";
}

int main()
{
    //freopen("ss.inp","r",stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while (t--)
    {
        read();
        process();
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define pii pair<ll,ll>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(vr) vr.begin(),vr.end()
const int N=1e6+10;
const ll base=784697,mod=1e9+9;
int n,m;
ll val[N];
vector<int> G[N];

ll gcd(ll t1,ll t2)
{
    while (t2)
    {
        ll tmp=t1%t2;
        t1=t2;
        t2=tmp;
    }
    return t1;
}

void read()
{
    cin>>n>>m;
    for (int i=1;i<=n;i++) cin>>val[i],G[i].clear();
    for (int i=1,u,v;i<=m;i++)
    {
        cin>>u>>v;
        G[v].pb(u);
    }
}

void process()
{
    map<pii,ll> can;
    ll res=0;
    for (int i=1;i<=n;i++)
    {
        sort(all(G[i]));
        ll h=0;
        for (ll x : G[i]) h=(h*base+x)%mod;
        if (G[i].size()>0) can[mp(G[i].size(),h)]+=val[i];
        //if (G[i].size()>0) can[G[i]]+=val[i];
    }
    for (auto ir=can.begin();ir!=can.end();ir++)
    {
        ll tmp=ir->se;
        res=gcd(res,tmp);
    }
    cout<<res<<"\n";
}

int main()
{
    //freopen("ss.inp","r",stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while (t--)
    {
        read();
        process();
    }
    return 0;
}


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

#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
using namespace __gnu_pbds;

void debug_out() { cerr << endl; }
template<class T> ostream& prnt(ostream& out, T v) { out << v.size() << '\n'; for(auto e : v) out << e << ' '; return out;}
template<class T> ostream& operator<<(ostream& out, vector <T> v) { return prnt(out, v); }
template<class T> ostream& operator<<(ostream& out, set <T> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, map <T1, T2> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> p) { return out << '(' << p.st << ' ' << p.nd << ')'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}

#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(int _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"
#define  st first
#define  nd second
#define int long long
#define ull unsigned long long

template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef cc_hash_table<int, int, hash<int > > ht;

const int  M    =  1000000007;
const int  maxn =  5000001   ;
const long long   inf  =  1e14     ;
const long double eps = 1.084202e-19;

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

ull mix(ull o){
    o+=0x9e3779b97f4a7c15;
    o=(o^(o>>30))*0xbf58476d1ce4e5b9;
    o=(o^(o>>27))*0x94d049bb133111eb;
    return o^(o>>31);
    /*Those constants supposedly are chosen to give this function better
     pseudo-random properties, but on any on-site contest, when one can't
     have team reference document/doesn't want to waste time searching it
     for implementation typing arbitrary large odd numbers by hand should
     be good enough
     */
}
unordered_map<ull,ull> ma;
void pre(){
   for(int i = 1 ; i<maxn ; i++){
       ma[i]=mix(i);
   }
}

void solve(){
    int n , m ;
    cin >> n >> m ;
    vector<int> valu(n+1);
    for(int i = 1 ; i <= n ; i++)
      cin >> valu[i];
    vector<int> adj[n+1];
    unordered_map<ull,ull> uu;
    for(int i = 0  ;i < m ; i++){
        ull x , y ;
        cin >> x >> y ;
        adj[y].push_back(x);
    }
    for(int i = 1;i<=n;i++){
        ull val = 0 ;
        for(auto j : adj[i]){
            val^=ma[j];
        }
        uu[val]+=valu[i];
    }

    ull ans = 0;
    for(auto j : uu){
        ans = __gcd(ans,j.nd);
    }
    cout<<ans;
}

int32_t main()
{
      ios_base::sync_with_stdio(false);
      cin.tie(NULL);
      int te;
      pre();
      cin >> te ;

      while(te--){
          solve();
          cout<<"\n";
      }

return 0;
}


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

#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
using namespace __gnu_pbds;

void debug_out() { cerr << endl; }
template<class T> ostream& prnt(ostream& out, T v) { out << v.size() << '\n'; for(auto e : v) out << e << ' '; return out;}
template<class T> ostream& operator<<(ostream& out, vector <T> v) { return prnt(out, v); }
template<class T> ostream& operator<<(ostream& out, set <T> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, map <T1, T2> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> p) { return out << '(' << p.st << ' ' << p.nd << ')'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}

#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(int _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"
#define  st first
#define  nd second
#define int long long
#define ull unsigned long long

template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef cc_hash_table<int, int, hash<int > > ht;

const int  M    =  1000000007;
const int  maxn =  5000001   ;
const long long   inf  =  1e14     ;
const long double eps = 1.084202e-19;

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

ull mix(ull o){
    o+=0x9e3779b97f4a7c15;
    o=(o^(o>>30))*0xbf58476d1ce4e5b9;
    o=(o^(o>>27))*0x94d049bb133111eb;
    return o^(o>>31);
    /*Those constants supposedly are chosen to give this function better
     pseudo-random properties, but on any on-site contest, when one can't
     have team reference document/doesn't want to waste time searching it
     for implementation typing arbitrary large odd numbers by hand should
     be good enough
     */
}
unordered_map<ull,ull> ma;
void pre(){
   for(ull i = 1 ; i<maxn ; i++){
       ma[i]=mix(i^ra);
   }
}

void solve(){
    int n , m ;
    cin >> n >> m ;
    vector<int> valu(n+1);
    for(int i = 1 ; i <= n ; i++)
      cin >> valu[i];
    vector<int> adj[n+1];
    unordered_map<ull,ull> uu;
    for(int i = 0  ;i < m ; i++){
        ull x , y ;
        cin >> x >> y ;
        adj[y].push_back(x);
    }
    for(int i = 1;i<=n;i++){
        ull val = 0 ;
        for(auto j : adj[i]){
            val^=ma[j];
        }
        uu[val]+=valu[i];
    }

    ull ans = 0;
    for(auto j : uu){
        ans = __gcd(ans,j.nd);
    }
    cout<<ans;
}

int32_t main()
{
      ios_base::sync_with_stdio(false);
      cin.tie(NULL);
      int te;
      pre();
      cin >> te ;

      while(te--){
          solve();
          cout<<"\n";
      }

return 0;
}


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

#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
using namespace __gnu_pbds;

void debug_out() { cerr << endl; }
template<class T> ostream& prnt(ostream& out, T v) { out << v.size() << '\n'; for(auto e : v) out << e << ' '; return out;}
template<class T> ostream& operator<<(ostream& out, vector <T> v) { return prnt(out, v); }
template<class T> ostream& operator<<(ostream& out, set <T> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, map <T1, T2> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> p) { return out << '(' << p.st << ' ' << p.nd << ')'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}

#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(int _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"
#define  st first
#define  nd second
#define int long long
#define ull unsigned long long

template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef cc_hash_table<int, int, hash<int > > ht;

const int  M    =  1000000007;
const int  maxn =  5000001   ;
const long long   inf  =  1e14     ;
const long double eps = 1.084202e-19;

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

ull mix(ull o){
    o+=0x9e3779b97f4a7c15;
    o=(o^(o>>30))*0xbf58476d1ce4e5b9;
    o=(o^(o>>27))*0x94d049bb133111eb;
    return o^(o>>31);
    /*Those constants supposedly are chosen to give this function better
     pseudo-random properties, but on any on-site contest, when one can't
     have team reference document/doesn't want to waste time searching it
     for implementation typing arbitrary large odd numbers by hand should
     be good enough
     */
}
unordered_map<ull,ull> ma;
void pre(){
   for(int i = 1 ; i<maxn ; i++){
       ma[i]=mix(i);
   }
}

void solve(){
    int n , m ;
    cin >> n >> m ;
    vector<int> valu(n+1);
    for(int i = 1 ; i <= n ; i++)
      cin >> valu[i];
    vector<int> adj[n+1];
    unordered_map<ull,ull> uu;
    for(int i = 0  ;i < m ; i++){
        ull x , y ;
        cin >> x >> y ;
        adj[y].push_back(x);
    }
    for(int i = 1;i<=n;i++){
        ull val = 0 ;
        for(auto j : adj[i]){
            val^=ma[j];
        }
        uu[val]+=valu[i];
    }

    ull ans = 0;
    for(auto j : uu){
        if(j.st){
        ans = __gcd(ans,j.nd);
        }
    }
    cout<<ans;
}

int32_t main()
{
      ios_base::sync_with_stdio(false);
      cin.tie(NULL);
      int te;
      pre();
      cin >> te ;

      while(te--){
          solve();
          cout<<"\n";
      }

return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e9;

int c[500001];

vector <int> g[1001];

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

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

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


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e9;

long long c[500001];

vector <int> g[1001];

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

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

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


#include <bits/stdc++.h>

using namespace std;

#pragma GCC optimize("O3")

const int MAX = 1e9;

long long c[500001];

vector <int> g[500001];

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

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

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


#include <bits/stdc++.h>

using namespace std;

#pragma GCC optimize("O3")

const int MAX = 1e9;

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

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

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


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e9;

long long c[500001];

vector <int> g[500001];

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

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

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


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e9;

long long c[500001];

vector <int> g[500001];

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

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

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