#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    cin>>t;
    int rez[t];
    int n,m;
    unsigned long long mini;
    vector <unsigned long long> a;
    vector <unsigned long long> b;
    unsigned long long r;
    for (int i=0;i<t;i++)
    {
        set<unsigned long long> raz;
        cin>>n>>m;
        a.resize(n);
        b.resize(m);
        rez[i]=1;
        for (int j=0;j<n;j++)
        {
            cin>>a[j];
        }
        sort( a.begin() , a.end() );

        for (int j=0;j<m;j++)
        {
            cin>>b[j];
        }
        for (int k=1;k<n;k++)
        {
            r=a[k]-a[k-1];
            if (r==1)
            {
                rez[i]=0;
                break;
            }
            raz.insert(r);
        }
        if (rez[i]!=0)
        {
            set <unsigned long long> :: iterator itraz;
            itraz=raz.begin();
            mini=*itraz;
            unsigned long long d,pera;
            d=mini;
            int brojac;
            if (raz.size()!=1)
            {
                for (int l=0;l<mini;l++)
                {
                    brojac=0;
                    d=mini-l;
                    if (mini%d==0)
                    {
                        itraz=raz.begin();
                        itraz++;
                        while (itraz!=raz.end())
                        {
                            if (*itraz %d!=0)
                            {
                                brojac++;
                                break;
                            }
                            itraz++;
                        }
                        if (brojac==0)
                            break;
                    }

                }
            }
            for (int f=0;f<m;f++)
            {
                if (b[f] <a[0])
                {
                    pera=a[0]-b[f];
                }
                else
                {
                    pera=b[f]-a[0];
                }
                if (pera%d==0)
                {
                    rez[i]=0;
                    break;
                }
            }
        }
    }
    for (int i=0;i<t;i++)
    {
        cout<<rez[i]<<endl;
    }
}