#include <iostream>
#include <cmath>

using namespace std;
typedef long long ll;

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

ll f[5];
ll a[75000];

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int t, n, m;
	cin >> t;	
	for (int i = 0; i < t; i++){
		f[i] = 1;
		cin >> n >> m;		
		ll d;
		for (int j = 0; j < n; j++){
			cin >> a[j];
			if (j == 1){
				d = abs(a[0] - a[1]);
			}
			else if (j > 1){
				d = nzd(d, abs(a[j - 1] - a[j]));				
			}
		}		
		ll ost = a[0] % d;
		ll b;
		bool reseno = false;
		for (int j = 0; j < m; j++){			
			cin >> b;
			if (!reseno && b % d == ost){				
				f[i] = 0;		
				reseno = true;		
			}
		}
	}
	for (int i = 0; i < t; i++){
		cout << f[i] << '\n';
	}
	return 0;
}