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

int t;
int n, m;
long long differ;
long long modu;
long long a[75005];
long long b[75005];
bool isOk;

int main()
{
    scanf("%d", &t);
    for (int i = 0; i < t; ++i)
    {
        isOk = true;
        scanf("%d %d", &n, &m);
        for (int j = 0; j < n; ++j)
        {
            scanf("%lld", &a[j]);
        }
        for (int j = 0; j < m; ++j)
        {
            scanf("%lld", &b[j]);
        }
        sort(a, a+n);
        differ = 10^18;
        for (int j = 1; j < n; ++j)
        {
            if (a[j] - a[j-1] < differ)
            {
                differ = a[j] - a[j-1];
            }
        }
        modu = a[0] % differ;
        for (int j = 1; j < n; ++j)
        {
            if (a[j]%differ != modu)
            {
                isOk = false;
                break;
            }
        }
        if (isOk == false)
        {
            printf("0\n");
            continue;
        }
        for (int j = 0; j < m; ++j)
        {
            if (b[j]%differ == modu)
            {
                isOk = false;
                break;
            }
        }
        if (isOk == false)
        {
            printf("0\n");
            continue;
        }
        printf("1\n");
    }
	return 0;
}