#include <bits/stdc++.h>
#define pb push_back
#define sc scanf
#define pr printf
#define tc int t; cin>>t; while(t--)
#define ccout if(debug) cout

typedef long long ll;
using namespace std;
const int mxN=75010,mxM=75010,mod=1e9+7;
int n, m;
ll a[mxN], b[mxN], x;

ll nzd(ll a, ll b) {
   if (b == 0) return a;
   return nzd(b, a % b);
}
void unos(){
   sc("%d %d", &n, &m);
   for(int i=1; i<=n; i++){
      sc("%lld", &a[i]);
   }
   for(int i=1; i<=m; i++){
      sc("%lld", &b[i]);
   }
}
void nadji_x(){
   if(n==2){
      x=a[2]-a[1];
      return;
   }
   else{
      x=nzd(a[3]-a[2], a[2]-a[1]);
   }
   for(int i=4; i<=n; i++){
      x=nzd(x, a[i]-a[i-1]);
   }
}
int solve(){
   sort(a+1, a+1+n);
   sort(b+1, b+1+m);
   nadji_x();
//   cout<<"nzd=="<<x<<endl;

   ll ostatak=a[1]%x;
   for(int i=1; i<=m; i++){      ///provera svih b-ova
      if(b[i]%x==ostatak) return 0;
   }

   return 1;
}

int main(){
   tc{
      unos();
      cout<<solve()<<endl;
   }

   return 0;
}