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

int n, m;
typedef long long ll;

ll a[maxn], bV, ans;

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

void solveC(){
	cin >> n >> m;
	ans = 0;
	for(int i = 0; i < n; ++i){
		cin >> a[i];
		if(i){
			ans = gcd(ans, a[i]-a[i-1]);
		}
	}
	bool isOk = 1;
	for(int i = 0; i < m; ++i){
		cin >> bV;
		if(bV % ans == a[0] % ans){
			isOk = 0;
		}
	}
	cout << isOk << '\n';
}

int main(){
	ios_base::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);
	int t;
	cin >> t;
	while(t--){
		solveC();
	}
}