#include <bits/stdc++.h>

using namespace std;
vector<long long> razlike,da,ne;
int main()
{
    long long t,n,m,a,b;
    cin >> t;
    while (t--)
    {
        cin >> n >> m;
        long long maxi=-1;
        da.clear();
        ne.clear();
        razlike.clear();
        for (long long i=0;i<n;i++)
        {
            cin >> a;
            da.push_back(a);
            maxi=max(maxi,a);
        }
        for (long long i=0;i<m;i++)
        {
            cin >> b;
            ne.push_back(b);
            maxi=max(maxi,b);
        }
        sort(da.begin(),da.end());
        for (long long i=1;i<n;i++)
        {
            razlike.push_back(da[i]-da[i-1]);
        }
        sort(razlike.begin(),razlike.end());
        bool moze=true;
        long long br=0,ost_da;
        for (long long i=1;i<=razlike[0];i++)
        {
            moze=true;
            br=0;
            ost_da=da[0]%i;
            for (long long j=1;j<n;j++)
            {
                if (da[j]%i!=ost_da)
                {
                    moze=false;
                    break;
                }
            }
            for (long long j=0;j<m;j++)
            {
                if (ne[j]%i==ost_da)
                {
                    moze=false;
                    break;
                }
            }
            if (moze)
            {
                break;
            }
        }
        if (moze)
        {
            cout << 1 << endl;
            continue;
        }
        cout << 0 << endl;
    }
    return 0;
}