#include <cstdio>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <cmath>


int nzd(int a, int b)
{
    if (a == 0)
    {
        return b;
    }
    return nzd(b % a, a);
}


using namespace std;

bool r5[4];
//long long k[4];
/*long long a[75002];
long long b[75002];*/



int main()
{
    ios_base::sync_with_stdio(false);
    int t, n, m;
    r5[0]=true;
    r5[1]=true;
    r5[2]=true;
    r5[3]=true;
    r5[4]=true;
    long long k1, k2, k3, k4, k5;
    cin>>t;
    for(int i = 0; i<t; i++)
    {
        cin>>n;
        cin>>m;
        cin>>k1;
        cin>>k2;
        k3 = abs(k1 - k2);
        k4 = abs(k1 - k2);
        k5 = k4;
        for(int j =  2; j<n; j++)
        {
            cin>>k2;
            k3 = abs(k1 - k2);
            k5 = nzd(k3, k4);
            k4 = k5;
        }
        //cout<<"    "<<k5<<"    "<<endl;
        for(int j = 0; j<m; j++)
        {
            cin>>k2;
            k3=abs(k1 - k2);
            if(k3%k5==0)
            {
                r5[i]=false;
            }

        }
        //if(r5[i]==true) k[i]=k5;
        k1=0;
        k2=0;
        k3=0;
        k4=0;
        k5=0;
    }
    for(int i = 0; i<t; i++)
    {
        if(r5[i])
        {
            cout<<"1"<<endl;
        }
        else
        {
            cout<<"0"<<endl;
        }
    }







    return 0;
}