#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];

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]);
   }
}
int solve(){
   sort(a+1, a+1+n);
   sort(b+1, b+1+m);
//   cout<<"bardo\n";
   ll raza=1e18;
   for(int i=1; i<n; i++){
      raza=min(raza, (a[i+1]-a[i]) );
   }
//   cout<<"raza=="<<raza<<endl;
   ll pom;
   for(int i=2; i<=n; i++){      ///provera svih a-ova
      pom=a[i]-a[i-1];
      if(pom%raza) return 0;
   }
   ll ostatak=a[1]%raza;
   for(int i=1; i<=m; i++){      ///provera svih b-ova
      pom=b[i]%raza;
      if(pom==ostatak) return 0;
   }

   return 1;
}

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

   return 0;
}