#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define fi first
#define se second
#define ld long double
#define MAXC 10000000
#define N_MAX 100010

using namespace std;
ll period,ostatak;
void solve() {
	int n, m ;
	cin >> n >> m ;

	vector<ll>v(n);
	for (int i = 0 ; i < n ; i++ ) cin >> v[i];

	sort(v.begin(),v.end());

	ll nzd = v[1] - v[0];

	for (int i = 2 ; i < n ; i++ ) {
		ll diff = v[i] - v[i-1];

		nzd = __gcd(nzd,diff);

	}

	ostatak = v[0]%nzd ;
	period= nzd ;
	bool bul = 1 ;
	for (int i =0 ; i < m ; i++) {
		ll a ;
		cin >> a;
		if ( a%nzd == ostatak ) bul = 0 ;
	}

	cout << bul << endl ;

} 


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

 
    int t =1;
    cin >>t ;
    while(t--) 
        solve();
}