#include <stdio.h>
#include <math.h>
long long nzd ( long long a, long long b )
{
  long long c;
  while ( a != 0 ) {
     c = a; a = b%a;  b = c;
  }
  return b;
}

int main()
{
    int t,m,n,i,j;
    long long x, a[10000], b[10000];
    int c[6];
    
    scanf("%d", &t);
    
    for(i=0;i<t;i++){
        scanf("%d %d",&m,&n);
        
        for(j=0;j<m;j++)
            scanf("%lli", &a[j]);
        
        for(j=0;j<n;j++)
            scanf("%lli", &b[j]);
        
        if(m>2){
            x = nzd( (abs(a[1]-a[0])) , (abs(a[2]-a[1])) );
            
            for(j=2;j<m-1;j++){
                if(x == 1) break; 
                x = nzd(x, (a[j+1] - a[j]));
            }
            
            if (x == 1){ c[i] == 0; continue;}
            
        }else x = a[0] - a[1];
       
        for(j=0;j<n;j++)
            if((b[j] - a[0]) % x == 0) break;
        
        if(j == n) c[i] = 1;
        else c[i] = 0;
            
    }
    for(i=0;i<t;i++)
        printf("%d\n",c[i]);
}