#include <bits/stdc++.h>
using namespace std;

#define ios ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long
#define inf INT_MAX
#define llinf LLONG_MAX
#define mt make_tuple
#define mp make_pair
#define parll pair<ll,ll>
#define parii pair<int,int>

int main()
{
    ios;

    int q,n,m;

    cin >> q;
    while ( q-- )
    {
        cin >> n >> m;
        vector <ll> A(n),B(m),C;
        for ( int i = 0; i < n; i++ )
        {
            cin >> A[i];
        }
        for ( int i = 0; i < m; i++ )
        {
            cin >> B[i];
        }
        sort( A.begin(), A.end() );

        ll d = llinf;
        for ( int i = 0; i < n-1; i++ )
        {
            d = min( d, abs(A[i+1]-A[i]) );
        }

        C.push_back(d);
        for ( int i = 2; i*i <= d; i++ )
        {
            if ( d%i == 0 )
            {
                C.push_back(i);
                if ( i != d/i )
                {
                    C.push_back(d/i);
                }
            }
        }

        bool moze = true;
        for ( int i = 0; i < C.size(); i++ )
        {
            ll k = A[0]%C[i];
            moze = true;
       	    for ( int j = 0; j < n; j++ )
            {
                if ( k != A[j]%C[i] )
                {
                    moze = false;
                    break;
                }
            }
            if ( moze )
            {
                for ( int j = 0; j < m; j++ )
                {
                    if ( k == B[j]%C[i] )
                    {
                        moze = false;
                        break;
                    }
                }
                if ( moze )
                {
                    break;
                }
            }
        }

        if ( moze )
        {
            cout << "1\n";
            continue;
        }
        cout << "0\n";
    }

    return 0;
}