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

void swap(unsigned long* a, unsigned long* b) 
	{ 
	    unsigned long t = *a; 
	    *a = *b; 
	    *b = t; 
	} 
	  
	
	int partition (unsigned long arr[], int low, int high) 
	{ 
	    unsigned long pivot = arr[high];    
	    int i = (low - 1);  
	  
	    for (int j = low; j <= high- 1; j++) 
	    { 
	        if (arr[j] <= pivot) 
	        { 
	            i++;    
	            swap(&arr[i], &arr[j]); 
	        } 
	    } 
	    swap(&arr[i + 1], &arr[high]); 
	    return (i + 1); 
	} 
	  
	void quickSort(unsigned long arr[], int low, int high) 
	{ 
	    if (low < high) 
	    { 
	        int pi = partition(arr, low, high); 
	  
	        quickSort(arr, low, pi - 1); 
	        quickSort(arr, pi + 1, high); 
	    } 
	} 

	unsigned long NZD(unsigned long a, unsigned long b){
		unsigned long r=a%b;
		
		if(r==0)
			return b;
		else
			return NZD(b,r);
	}

using namespace std;

int main(){
	int T;
	
	scanf("%d",&T);
	int rez[T];
	int k=-1;
	for(int i=0;i<T;i++){
		int N,M;
		int x=1;
		scanf("%d%d",&N,&M);
		unsigned long a[N],a1[N-1],b[M];
		for(int j=0;j<N;j++){
			scanf("%lu",&a[j]);
		}
		for(int j=0;j<M;j++){
			scanf("%lu",&b[j]);
		}
		quickSort(a,0,N-1);
		unsigned long c;
		if(N>2){
			for(int j=1;j<N;j++){
				a1[j-1]=a[j]-a[j-1];
			}
			c=NZD(a1[0],a1[1]);
			for(int j=2;j<N-1;j++){
				c=NZD(c,a1[j]);
			}
		}else
			c=a[1]-a[0];
	//	printf("ovo je c %d\n",c);
		
		if(c==1){
			x=0;
		}else{
			
	//	printf("ovo je x %d\n",x);
			int j=0;
			bool ind=true;
			while(j<M && ind==true){
				float n=((float)b[j]-(float)a[0])/(float)c;
				//printf("ovo je int n %f %f %d ",n,,c);
				if((int)n==n)
				{
					ind=false;
					x=0;
				}
				j++;
			}
	}
	x1[k++]=x;

	}
	for(int i=0;i<T;i++){
		printf("%d\n",rez[i]);
	}
}