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

int main()
{
	short int t, fl;
	int f[t];
	long long n, m, x, j;
	long long a[75000], b[75000];

	cin >> t;
	j = 0;
	for (int i = 0; i < t; i++)
	{
		cin >> n >> m;

		for (int i = 0; i < n; i++)
			cin >> a[i];

		for (int i = 0; i < m; i++)
			cin >> b[i];

		sort(a, a + n);
		sort(b, b + m);

		x = a[1] - a[0];
		for (int i = n - 1; i > 1; i--)
			if ((a[i] - a[i - 1]) < x)
				x = a[i] - a[i - 1];
		
		f[j] = 1;

		for (int i = m -1; i > 0; i--)
		{
			if ((b[i] - a[0]) % x == 0)
			{
			    f[j] = 0;
			}
		}

		j++;
	}
	
	for (int i = 0; i < j; i++)
	    cout << f[i] << "\n";

	return 0;
}