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



void swap(long int* a,long int* b)
{
    long int t = *a;
    *a = *b;
    *b = t;
}


long int partition (long int arr[],long int low,long int high)
{
    int pivot = arr[high];
    int i = (low - 1);

    for (int j = low; j <= high - 1; j++)
    {

        if (arr[j] < pivot)
        {
            i++;
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    return (i + 1);
}

void quickSort(long int arr[], long int low,long int high)
{
    if (low < high)
    {

        long int pi = partition(arr, low, high);


        quickSort(arr, low, pi - 1);
        quickSort(arr, pi + 1, high);
    }
}

int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

long int findGCD(long int arr[],long int n)
{
    long int result = arr[1];
    for (int i = 2; i < n; i++)
    {
        result = gcd(arr[i], result);

        if(result == 1)
        {
           return 1;
        }
    }
    return result;
}

void subtraction_arr(long int arr[],long int n)
{
    int i;
    for(i=1;i<n;i++)
        arr[i]-=arr[0];

}

void printArray(int arr[], int size)
{
    int i;
    for (i = 0; i < size; i++)
        cout << arr[i] << " ";
    cout << endl;
}

void loadArray(long int arr[], int size)
{
    int i;
    for (i = 0; i < size; i++)
        cin>> arr[i];
}


int main()
{
    int tak,m=0;
    cin>>tak;
    int rezultati[tak];

    long int ar[2];
    long int arr[75000];
    long int arri[75000];

    while(m<tak)
    {

    loadArray(ar,2);


    int n = ar[0];
    int n1 = ar[1];

    loadArray(arr,n);
    loadArray(arri,n1);


    //int arr[] = {1, 7, 4, 13};
    //int arri[] = {3, 11, 9};

    bool rumors1=false,rumors2=true,t=false;

    quickSort(arr, 0, n - 1);
    subtraction_arr(arr, n);
    long int gcd=findGCD(arr, n);
    if(gcd>1)
    {
        rumors1=true;
    }
    for(int i=0;i<n1;i++)
        if((arri[i]-arr[0])%gcd==0)
        {
            rumors2=false;
            break;
        }
    if(rumors1==true && rumors2==true) t=true;

    rezultati[m]=t;
    m++;


    }




    printArray(rezultati,tak);
    /*/
    cout<<t;
    printArray(arr, n);
    cout << findGCD(arr, n) << endl;
    /*/
    return 0;
}