#include <iostream>
#include <algorithm>


using namespace std;

int main()
{
    int T,jes=0,d;
    long long n,m;cin>>T;long long ende[T];
    for (long long i=0;i<T;i++)
    {
        cin>>n>>m;
        long long niz1[n],niz2[m];
        for (long long j=0;j<n;j++)
        {
            cin>>niz1[j];
        }
        sort(niz1,niz1+n);
        for (long long j=0;j<m;j++)
        {
            cin>>niz2[j];
        }
        sort(niz2,niz2+m);
        d=niz1[1]-niz1[0];

        for (long long br=1;br<n;br++)
        {
            if ((niz1[br+1]-niz1[br])<d)//ne stavljamo uslov za vece jer smo sortirali niz pre toga
            {
                d=niz1[br+1]-niz1[br];
            }
            else if (niz1[br+1]-niz1[br]==d)
            {
                jes=1;
            }
        }
        for (long long br=0;br<m;br++)
        {
            if (((niz2[br]-niz2[0])%d) != 0)
            {
                jes=1;
            }
        }
        ende[i]=jes;
        jes=0;d=0;
    }
    for (int t=0;t<T;t++)
    {
        cout<<ende[t]<<endl;
    }
    return 0;
}