#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

long long i,j;

int nadjiX(vector<long long> A, vector<long long>B){
    sort(A.begin(),A.end());
    sort(B.begin(),B.end());
    vector<long long> razlika; 
    vector <long long> ::iterator it;
    long long br = 0;;
    
    
    for(i = 0; i < A.size() - 1; i++){
        razlika.push_back(A[i+1]-A[i]);
    }
    
    long long x = *min_element(razlika.begin(),razlika.end());
    
    it = A.begin();

    while((*it)-x > 0){
        *it -= x;
    }
    
    long long pocetak = *it;
    A.clear();
    A.resize(1);
    it = A.begin();
    *it = pocetak;


    while( *(A.end()-1) < *(B.end() - 1) ){
        A.push_back(*(A.end()-1)+x);
    }

   for(i = 0; i < A.size(); i++){
       for(j = 0; j < B.size(); j++){
           if(*(A.begin() + i) == *(B.begin() + j)) br = 1;
       }
   }

    if(br == 1){
        return 0;
    } else {
        return 1;
    }
}

int main(){
   int t,g,k;
   long N,M;
   long long v;
   vector <long long> A;
   vector <long long> B;

   scanf("%d",&t);

   int res[t];

   for(g = 0; g < t; g++){
       scanf("%ld%ld",&N,&M);
       for(k = 0; k < N; k++){
           scanf("%lld",&v);
           A.push_back(v);
       }

       for(k = 0; k < M; k++){
           scanf("%lld",&v);
           B.push_back(v);
       }

       res[g] = nadjiX(A,B);
       A.clear();
       B.clear();
       A.resize(0);
       B.resize(0);
   }
   for(i = 0; i < t; i++){
       printf("%d \n",res[i]);
   }
     return 0;
}