#include <iostream>
#include <algorithm>
#include <climits>
using namespace std;
int main(int argc, const char * argv[]) {
    long long unsigned int N = 0,M = 0,T;
    cin>>T;
    long long unsigned int ispis[T];
    for (long long unsigned int j=0; j < T; j ++)
    {
    cin>>N>>M;
    long long unsigned int A[N], B[M];
    for (long long unsigned int i =0; i<N; i ++)
    {
        cin>>A[i];
    }
    for (long long unsigned int i =0; i<M; i ++)
    {
        cin>>B[i];
    }
    
    sort(A,A+N);
    sort(B,B+M);
    long long unsigned int minimalni_razmak= ULLONG_MAX;
    for (long long unsigned int i = 1; i < N; i ++)
    {
        if(A[i]-A[i-1]<minimalni_razmak)
        {
            minimalni_razmak=A[i]-A[i-1];
        }
    }
    for (long long unsigned int i = 1; i < N; i ++)
    {
        if((A[i]-A[i-1])%minimalni_razmak!=0)
        {
            if (minimalni_razmak>A[0])
            minimalni_razmak=A[0];
            else
            {
                minimalni_razmak=1;
            }
        }
    }
    
    for (long long unsigned int i = 0; i < M; i ++)
    {
        if(abs(int(B[i]-A[0]))%minimalni_razmak==0)
        {
            ispis[j]=0;
            break;
        }
        if (i==M-1)
        {
            ispis[j]=1;
        }
    }
    }
    for (long long unsigned int i=0; i < T; i ++)
    {
        cout<<ispis[i]<<endl;
    }
    return 0;
}