#include <stdlib.h>
#include <stdio.h>

long long NZD (long long x, long long y)
{
    long long t;
    while (y!=0)
    {
        t = y;
        y = x%y;
        x = t;
    }

    return x;
}

long long ABS (long long x)
{
    if (x>=0)
        return x;
    else
        return -x;
}

int main()
{
    int T;
    scanf ("%d", &T);

    for (int k=0; k<T; k++)
    {
        long N, M;
        scanf ("%ld%ld", &N, &M);

        long long p, t, pr, nzd;
        scanf ("%lld", &p);
        scanf ("%lld", &t);
        nzd = ABS (t-p);
        pr = t;
        for (long j=2; j<N; j++)
        {
            scanf ("%lld", &t);
            nzd = NZD (ABS (t-pr), nzd);
            pr = t;
            if (nzd==1)
                break;
        }

        int ind = 1;
        for (long j=0; j<M; j++)
        {
            scanf ("%lld", &t);
            if ((t-p)%nzd==0)
                ind = 0;
        }
        printf ("%d\n", ind);
    }

    return 0;
}