#include <iostream>
using namespace std;
void merge1(long long int arr[], int l, int m, int r)
{
    int i, j, k;
    int n1 = m - l + 1;
    int n2 = r - m;

    /* create temp arrays */
    long long int L[n1], R[n2];

    /* Copy data to temp arrays L[] and R[] */
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1 + j];

    /* Merge the temp arrays back into arr[l..r]*/
    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2) {
        if (L[i] <= R[j]) {
            arr[k] = L[i];
            i++;
        }
        else {
            arr[k] = R[j];
            j++;
        }
        k++;
    }

    /* Copy the remaining elements of L[], if there
    are any */
    while (i < n1) {
        arr[k] = L[i];
        i++;
        k++;
    }

    /* Copy the remaining elements of R[], if there
    are any */
    while (j < n2) {
        arr[k] = R[j];
        j++;
        k++;
    }
}

/* l is for left index and r is right index of the
sub-array of arr to be sorted */
void mergeSort(long long int arr[], int l, int r)
{
    if (l < r) {
        // Same as (l+r)/2, but avoids overflow for
        // large l and h
        int m = l + (r - l) / 2;

        // Sort first and second halves
        mergeSort(arr, l, m);
        mergeSort(arr, m + 1, r);

        merge1(arr, l, m, r);
    }
}
void printArray(long long int A[], int size1)
{
    int i;
    for (i = 0; i < size1; i++)
        printf("%IId ", A[i]);
    printf("\n");
}
long long int gcd(long long int a,long long int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
const int max_N=100005;

long long int niz[max_N];
long long int niz12[max_N];
bool ohyeah[10];
int main()
{
    int T;
    scanf("%d",&T);
    for(int kl=0;kl<T;kl++){
    int N;
    int M;
    scanf("%d",&N);
    scanf("%d",&M);

    for(int i=0;i<N;i++)
        scanf("%lld",&niz[i]);


    mergeSort(niz,0,N-1);
   /* for(int i=0;i<N;i++)
       cout<<niz[i]<<endl;*/

    int nzd=niz[1]-niz[0];
    for(int i=2;i<=N-1;i++){
        nzd=gcd(nzd,niz[i]-niz[i-1]);
       // cout<<"gcd"<<nzd<<endl;

    }
   // cout<<nzd<<endl;
    for(int i=0;i<M;i++)
        scanf("%lld",&niz12[i]);
    bool b=true;
    for(int i=0;i<M;i++){
        if((niz12[i]-niz[0])%nzd==0){
            b=false;
            break;

        }

    }
    cout<<b<<endl;

  //  printArray(niz,N);
  //  cout << "Hello world!" << endl;

    }
    return 0;
}