#include <bits/stdc++.h>

using namespace std;

int main(){
	//ios_base::sync_with_stdio(false);
    //cin.tie(NULL);
	int t;
	cin>>t;
	int s[t];
	for(int p = 0;p<t;p++){
		s[p]=0;
		int n,m;
		cin>>n>>m;
		map<unsigned long long,bool> a;
		//input positive years
		for(int i=0;i<n;i++){
			unsigned long long c;
			cin>>c;
			a.insert(pair<unsigned long long, bool>(c,true));
		}
		//find min dist between postitive years
		//aka the max dist for x
		unsigned long long maxX = ULLONG_MAX;
		map<unsigned long long, bool>::iterator itr;
		map<unsigned long long, bool>::iterator itr2;
		itr2=a.begin();
		itr2++;
		for(itr = a.begin();itr2!=a.end();itr++){
			unsigned long long diff =(itr2->first)-(itr->first); 
			maxX = min(diff,maxX);
			itr2++;
		}
		unsigned long long pivot = a.begin()->first;
		//input negative years
		for(int i=n;i<n+m;i++){
			unsigned long long c;
			cin>>c;
			a.insert({c,false});
		}
	
		unsigned long long lowerX = 0;
		unsigned long long upperX = maxX+1;
		unsigned long long x = (lowerX+upperX)/2;
		
		
		bool overshot = false;
		//binary search the solution
		while(upperX-lowerX>1){
			unsigned long long prev=-1;
			bool succ = true;
			//check if the current x is good and if we overshot
			for(itr = a.begin();itr!=a.end();itr++){
				if(!itr->second && ((itr->first)-pivot)%x==0){
						succ=false;
						break;
				}
				if(prev==-1 && itr->second){
					prev = itr->first;
					continue;
				}
				if(itr->second){
					if((itr->first-prev)%x==0){
						prev=itr->first;
					}
					else if(prev+x>itr->first){
						overshot = true;
						succ  = false;
						break;
					}
					else{
						succ= false;
						break;
					}
				}
			}
			if(succ){
				s[p] = 1;
				break;
			}
			else if(overshot){
				upperX = x;
				x = (lowerX+upperX)/2;
			}
			else{
				lowerX = x;
				x = (lowerX+upperX)/2;
			}
			if(x==1){
				break;
			}
		}
	}
	for(int i=0;i<t;i++){
		cout<<s[i]<<endl;
	}
	return 0;
}