#include <bits/stdc++.h>

using namespace std;

int nzd(long long int a, long long int b)
{
    while(b != 0)
    {
        long long int r = b;
        b = a % b;
        a = r;
    }
    return a;
}

void resenje(long long int prih[],long long int odbj[], int N, int M)
{
    long long int period = prih[1] - prih[0];
    for(int i = 1; i < N - 1; i++)
    {
        period = nzd(period, prih[i+1] - prih[i]);
        if(period == 1)
        {
            if(M > 0)
            {
                cout << 0;
                return;
            }
        }
    }

    int pocetak = prih[0] % period;

    for(int i = 0; i < M; i++)
    {
        if((odbj[i] - pocetak) % period == 0)
        {
            cout << 0;
            return;
        }
    }
    cout << 1;
    
}


int main()
{
    int T;
    cin >> T;
    for (int t = 0; t < T; t++)
    {
        int N, M;
        cin >> N >> M;
        long long int prih[N];
        long long int odbj[M];
        for(int i = 0; i < N; i++)
        {
            cin >> prih[i];
        }
        for(int i = 0; i < M; i++)
        {
            cin >> odbj[i];
        }
        
        sort(prih, prih + N);
        sort(odbj, odbj + M);
        
        resenje(prih, odbj, N, M);
    }
    
    return 0;
}