#include <iostream>

using namespace std;
long long nzd(long long  a,long long b)
{
    if(a%b==0)
    return b;
    else return nzd(b,a%b);
}
int T,n,m;
int main()
{
    long long x=0,pomoc1,pomoc2;
    cin>>T;
    while(T--)
    {
        pomoc1=0,pomoc2=0;
        cin>>n>>m;
        //cout<<"n i m su"<<n<<" "<<m<<endl;
        for(int i=0;i<n;i++)
        {
            if(pomoc1==0)
            {
                cin>>pomoc1;
            }
            else if(pomoc2==0)
            {
                cin>>pomoc2;
                //cout<<"prvi put ucitavamo pomoc2"<<pomoc2<<endl;
                x=pomoc2-pomoc1;
            }
            else
            {
                pomoc1=pomoc2;
                cin>>pomoc2;

                if((pomoc2-pomoc1)%x!=0)
                {
                    x=nzd(pomoc2-pomoc1,x);
                    //cout<<"X nakon nzd"<<x<<endl;
                }
            }
        }
        //cout<<"x nakon svih n brojeva "<<x<<endl;
        int check=1;
        for(int k=0;k<m;k++)
        {
            cin>>pomoc2;
            if((pomoc2-pomoc1)%x==0)
                check=0;
        }
        cout<<check<<endl;
    }
    return 0;
}
/**
4
4 3
1 7 4 13
3 11 9
3 2
9 5 3
7 11
2 3
3 15
1 7 2
2 2
5643634654354 12346544323565
22341124534 7655867344
*/