#include <bits/stdc++.h>

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

int main()
{
    int t;
    int n, m;
    long long a[75005];
    long long b[75005];
    int results[5];

    scanf("%d", &t);

    for(int i = 0; i<t; i++)
    {
        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]);

        std::sort(a, a+n);

        int result = 1;

        long long diff = a[1] - a[0];
        for(int j = 1; j<n; j++)
        {
            diff = NZD(diff, a[j] - a[j-1]);
            if(diff == 1)
            {
                result = 0;
                break;
            }
        }

        if(result == 0)
        {
            results[i] = 0;
            continue;
        }

        long long first = a[0];

        for(int j = 0; j<m; j++)
        {
            if(abs(b[j] - first) % diff == 0)
            {
                result = 0;
                break;
            }
        }

        results[i] = result;
    }

    for(int i = 0; i<t; i++)
        printf("%d\n", results[i]);

    return 0;
}