#include <iostream>
#include <math.h>

using namespace std;

const long SIZE = 1e5;

int main()	 {
	short T;
	long N, M;
	long long A1, A2, B, delta, x, initial;
	bool good[5];
	
	cin>>T;
	
	for(int i=0; i<T; i++){
		cin>>N>>M;
		
		good[i]=true;

		cin>>A1>>A2;
		x=abs(A2-A1);
		
		A1=A2;
		for(long j=2; j<N; j++){
			cin>>A2;
			delta=abs(A2-A1);
			if(x<delta){
				if(delta%x==0)
					continue;
				else
					x=1;
			}
			if(x>delta){
				if(x%delta==0)
					x=delta;
				else
					x=1;
			}
			A1=A2;
		}
		
		initial=A1%x;
		
		if(x==1)
			good[i] = false;
			

		for(long j=0; j<M; j++) {
			cin>>B;
			if((B%x) == initial){
				good[i]=false;
			}
		}
		
	}
	
	for(int i=0; i<T; i++)
		cout<<good[i]<<endl;
	
	return 0;
}