#include <stdio.h>

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

int pos(int a){
  if(a>0)
    return a;
  return -a;

}


int main(void) {
  long long dist[75005],a,b,gcd;
  int t,n,m,resenje[5];
  scanf("%d",&t);


  for(int i = 0; i<t; i++){
    resenje[i] = 1;
    scanf("%d%d",&n,&m);

    scanf("%lld",&a);
    for(int j = 0; j<n-1; j++){
      scanf("%lld",&b);
      dist[j] = pos(b-a);
      a = b;
    }
    
    
    gcd = dist[0];
    for(int j = 1; j<n-1; j++){
      gcd = nzd(dist[j], gcd);
    }

    for(int j = 0; j<m; j++){
      scanf("%lld",&b);
      if((a - b)%gcd == 0)
        resenje[i] = 0;
    }
  }

  for(int i = 0; i<t; i++)
    printf("%d\n",resenje[i]);

  return 0;
}