#include <stdio.h>
#include <stdlib.h>

void swap(unsigned long long &x, unsigned long long &y){
 unsigned long long t=x;
 x=y;
 y=t;
}

void bubbleSort(unsigned long long arr[], int n){
    int i,j;
    for (i=0;i<n-1;i++)
        for (j=0;j<n-i-1;j++)
            if (arr[j]>arr[j+1]) swap(arr[j],arr[j+1]);
}

int main()
{
    int t,j,k,rez;
    long m,n,s;
    unsigned long long x;
    scanf("%d", &t);
    for (int i=1;i<=t;i++){
            rez=1;
            scanf("%ld %ld", &n, &m);
            unsigned long long a[n];
            unsigned long long b[m];
            for (j=0;j<n;j++)
                scanf ("%ull", &a[j]);
            s= sizeof(a)/sizeof(a[0]);
            bubbleSort(a,s);
            x=a[n-1];
            for (j=1;j<n;j++)
                    if (a[j]-a[j-1]<x) x=a[j]-a[j-1];
            for (j=1;j<n;j++)
                    if ((a[j]-a[j-1])%x!=0) rez=0;
            for (k=0;k<m;k++)
                scanf ("%ull", &b[k]);
            for (k=0;k<m;k++)
                if ((b[k]-a[0])%x==0){
                    rez=0;
                    break;
                }
            printf("%d \n", rez);

    }

    return 0;
}