#include <iostream>
#include <math.h>

using namespace std;

int nzd(long long int x,long long int y) 
{
	while (x != y) 
	{
		if (x < y) y -= x;
		else x -= y;
	}
	return x;
}

int zad() 
{
	long long int niz[75000];
	long long int P, D, X1, X2, a;
	bool fl = 1;
	
	cin >> P >> D;
	
	cin >> niz[0];
	cin >> niz[1];
	if (P == 2) X1 = abs(niz[0]-niz[1]);
	
	else {
		cin >> niz[2];
		X1 = abs(niz[0] - niz[1]);
		X2 = abs(niz[0] - niz[2]);
		X1 = nzd(X1, X2);
		if (X1 == 1) 
		{
			fl = 0;
		}	
		for (int i = 3; i < P; i++) 
		{
			cin >> niz[i];
			X2 = abs(niz[i] - niz[0]);
			X1 = nzd(X1, X2);
			if (X1 == 1) 
			{
				fl = 0;
			}	
		}
	}
	
	for (int i = 0; i < D; i++) 
	{
		cin >> a;
		if (abs(a-niz[0]) % X1 == 0) 
		{
			fl = 0;
		}
		
	}
	return fl;
}

int main () 
{
	int T, niz[6];
	
	cin >> T;
	
	for (int i = 0; i < T; i++) 
	{
		niz[i] = zad();
	}
	for (int i = 0; i < T; i++) cout << niz[i] << endl;
	
	return 0;
}