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





int n,m,t,a[75005],b[75005];
int main() {
    cin>>t;
        for(int l=1;l<=t;l++){
scanf("%d%d",&n,&m);
 for(int i=1;i<=n;i++){

   scanf("%d",&a[i]);

}
for(int j=1;j<=m;j++){
scanf("%d",&b[j]);
}

sort(a+1,a+1+n);int br=0,brb=3*10e3;

for(int i=n;i>=2;i--){
    br=a[i]-a[i-1];
    brb=min(br,brb);
}




for(int i=brb;i>1;i--){
if(brb%i==0)
{int brojac=0,pocetni=a[1]%i;if(pocetni==0)pocetni=i;
    for(int j=1;j<=m;j++){
        if((b[j]-pocetni)%i==0)
        brojac++;
    }

if(brojac==0)
   {cout<<1<<endl;
   goto jump;
  
   }



}

}
cout<<0<<endl;


jump:

;        }

  return 0;
}