#include <cstdio>
#include <vector>
#include <algorithm>
#include <iostream>


using namespace std;


int nadjiX(vector<long long> Vec1, vector<long long>Vec2){
    sort(Vec1.begin(),Vec1.end());
    sort(Vec2.begin(),Vec2.end());
    vector<long long> dif; 
    vector <long long> ::iterator it;
    vector <long long> ::iterator bit=Vec2.end();
    
    for(int i = 0; i < Vec1.size() - 1; i++){
        dif.push_back(Vec1[i+1]-Vec1[i]);
    }
    long long mindif = *min_element(dif.begin(),dif.end());
    it = Vec1.begin();
    
    while((*it)-mindif > 0){
        *it -= mindif;
    }
    long long prvielem = *it;
    Vec1.clear();
    Vec1.resize(1);
    it = Vec1.begin();
    *it = prvielem;

    while( *(Vec1.end()-1) < *(Vec2.end() - 1) ){
        Vec1.push_back(*(Vec1.end()-1)+mindif);
    }

    for (it = Vec1.begin() ; it != Vec1.end(); ++it) {
        for (bit = Vec2.begin() ; bit != Vec2.end(); ++bit)
        if(*it==*bit) {
            return 0;
        }
    }
    return 1;
}

int main(){
    
    vector <long long> Vec1(10^18);
    vector <long long> Vec2(10^18);
    long long N,M,x;
    vector <int> izlaz;
    int T,m;
    Vec1.clear();
    Vec2.clear();
    
    scanf("%d",&T);

    for (int i=0;i<T;i++) {
        scanf("%lld%lld",&N,&M);

        for(int j = 0; j < N; j++){
           scanf("%lld",&x);
           Vec1.push_back(x);
       }
            for(int j = 0; j < M; j++){
           scanf("%lld",&x);
           Vec2.push_back(x);
       }

    m=(nadjiX(Vec1,Vec2));
    izlaz.push_back(m);
        Vec1.clear();
       Vec2.clear();
       Vec1.resize(0);
       Vec2.resize(0);

    }
    for (auto it=izlaz.begin();it != izlaz.end(); ++it) {
        printf("%d\n",*(it));
        }

     return 0;
}