#include<iostream>
#include<algorithm>

bool asking(){
    unsigned n,m;
    scanf("%u%u",&n,&m);
    long unsigned a[n],b[m];
    for(unsigned i=0;i<n;i++)
        scanf("%lu",&a[i]);
    for(unsigned i=0;i<m;i++)
        scanf("%lu",&b[i]);
    std::sort(a,a+n);
    long unsigned d=a[n-1]-a[0];
    for(unsigned i=1;i<n;i++)
        if(a[i]-a[i-1]<d) d=a[i]-a[i-1];
    for(unsigned i=1;i<n;i++){
        d=std::__gcd(a[i]-a[i-1],d);
        if(d==1)return 0;
    }
    for(unsigned i=0;i<m;i++)
        if((b[i]-(a[0]%d))%d==0)return 0;
    return 1;
}

int main(){
    short unsigned MyNigga;
    scanf("%hu",&MyNigga);
    bool DontRemember[MyNigga];
    for(unsigned ThatsCrazy=0;ThatsCrazy<MyNigga;ThatsCrazy++)
        DontRemember[ThatsCrazy]=asking();
    for(unsigned ThatsCrazy=0;ThatsCrazy<MyNigga;ThatsCrazy++)
        printf("%d\n",DontRemember[ThatsCrazy]);
}