#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;

long long int gcdF(long long int a, long long int b)
{
	if (b == 0) return a;
	a %= b;
	return gcdF(b, a);
}

long long int aps(long long int x) {
	if (x < 0)return -x;
	return x;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
    int t;
	cin >> t;
	for (int i = 0; i < t; i++) {

		long long int gcd = 1;
		int n, m;
		cin >> n >> m;
		long long int a,p;
		cin >> a;
	    p = a;

		for (int i = 1; i < n; i++) {

			long long int x;
			cin >> x;
			
			if (i == 1) {
				gcd = aps(p- x);

			}
			else
			{
				gcd = gcdF(gcd, aps(x - p));
				p = x;
			}
				
		}
		bool check = true;

		for (int i = 0; i < m; i++) {

			long long int x;
			cin >> x;

			if (aps(x - a) % gcd == 0) 
				check = false;

		}

			if (!check)cout << 0 << "\n";
			else
				cout << 1 << "\n";

	}

}