#include <bits/stdc++.h>

using namespace std;

#define pii pair<int,int>
#define ll long long
#define s second
#define f first

const int MAX_N = 7.5 * 10e4;

ll t, n, m, min_dif = LLONG_MAX, a[MAX_N], b[MAX_N];

void query(){
  cin >> n >> m;
  for(int i = 0; i < n; ++i){
    cin >> a[i];
  }
  for(int i = 0; i < m; ++i){
    cin >> b[i];
  }
  sort(a, a + n);
  min_dif = LLONG_MAX;
  for(int i = 0; i < n - 1; ++i){
    min_dif = min(min_dif, a[i + 1] - a[i]);
  }
  ll md = a[0] % min_dif;
  for(int i = 0; i < n; ++i){
    if(a[i] % min_dif != md){cout << 0 << endl; return;}
  }
  for(int i = 0; i < m; ++i){
    if(b[i] % min_dif == md){cout << 0 << endl; return;}
  }
  cout << 1 << endl;
}

int main(){
  cin >> t;
  while(t--){
    query();
  }
  return 0;
}