#include <stdio.h>
#include <stdlib.h>
unsigned long long int NZD(unsigned long long int a, unsigned long long int b)
{
    if(a%b!=0)
    {
        return NZD(b,a%b);
    }
    return b;
}

int main()
{
    int t;
    scanf("%d",&t);
    int i,j;
    unsigned long long int nz;
    int k[t];
    for(i=0;i<t;i++)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        unsigned long long int a,b,r,p;
        scanf("%llu%llu",&a,&b);
        if(a>b)
            nz=a-b;
        else
            nz=b-a;
        for(j=2;j<n;j++)
        {
            a=b;
            scanf("%llu",&b);
            if(a>b)
                r=a-b;
            else
                r=b-a;
            if(nz>r)
                nz=NZD(nz,r);
            else
                nz=NZD(r,nz);
        }
        p=b%nz;
        int z=1;
        if(nz==1)
            z=0;
        for(j=0;j<m;j++)
        {
            scanf("%llu",&b);
            if(z!=0)
            {
                if(b%nz==p)
                    z=0;
            }
        }
        k[i]=z;
    }
    for(i=0;i<t;i++)
    {
        printf("%d\n",k[i]);
    }
}