#include <bits/stdc++.h>

using namespace std;

void nzd(long long& a, long long b)
{ long long tmp;
  while(b)
  { tmp=b;
    b=a%b;
    a=tmp;
  }
}

int main()
{ long long a, b, c, s, t, m, n, i, del, mina, k;
  bool tf;
  cin>> t;
  vector<long long> veka(75000), peka(75000);
  for(i=0; i<t; i++)
  { cin>> n>> m>> veka[0]; mina=veka[0];
    for(k=1; k<n; k++)
    { cin>> veka[k];
      if(veka[k]<mina)mina=veka[k];
    }
    for(k=0; k<m; k++)cin>> peka[k];
    for(k=0; k<n; k++)
    { veka[k]-=mina;
    }//cout<<mina;
    if(veka[0]<veka[1]){del=veka[1]; nzd(del, veka[0]);}
    else{del=veka[0]; nzd(del, veka[1]);}
    //cout<<del;
    for(k=2; k<n; k++)
    { nzd(del, veka[k]);
      if(del==1)break;
    }
    if(del==1)cout<<"0\n";
    else
    { //cout<<del;
      tf=false;
      for(k=0; k<m; k++)
      { peka[k]-=mina;
        if((peka[k]%del)==0){tf=true; break;}
      }
      if(!tf)cout<<"1\n";
      else cout<<"0\n";
    }
  }

  return 0;
}