#include <iostream>
using namespace std;

void printArray(int A[], int size)
{
    for (int i = 0; i < size; i++)
        cout << A[i] << " ";
    cout<<endl;
}

void swap(int* a, int* b)  
{  
    int t = *a;  
    *a = *b;  
    *b = t;  
}  

int partition (int arr[], int low, int high)  
{  
    int 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(int arr[], int low, int high)  
{  
    if (low < high)  
    {  
        
        int pi = partition(arr, low, high);  
  
          
        quickSort(arr, low, pi - 1);  
        quickSort(arr, pi + 1, high);  
    }  
}  
int findMinDiff(int arr[], int n) 
{ 
   int diff = arr[n-1]; 
   for (int i=0; i<n-1; i++) 
      for (int j=i+1; j<n; j++) 
          if (abs(arr[i] - arr[j]) < diff) 
                diff = abs(arr[i] - arr[j]); 
   return diff; 
} 
int proveri_x(int broj,int arr[75000],int N,int arr2[75000],int M)
{
    int i,j,rez=1,kol=0;
    for(i=1;i<N;i++)
    {
        if((arr[i]-arr[0]) % broj ==0)
        {
            kol++;
        }
    }
    if(kol==N-1)
        {
            for(j=0;j<M;j++)
            {
                if((arr2[j]-arr[0]) % broj ==0)
                {
                    rez=0;
                    break;
                }
            }
        }
    else
    rez = 0;
    
    return rez;
}
int main()
{
    int i,j,T,N,M,A[75000],B[75000],rezultat;
    cin>>T;
    for(j=0;j<T;j++)
    {
        cin>>N>>M;
        for(i=0;i<N;i++)
        {
            cin>>A[i];
        }
        for(i=0;i<M;i++)
        {
            cin>>B[i];
        }
        quickSort(A,0,N-1);
        quickSort(B,0,M-1);
        for(i=1;i<=findMinDiff(A,N);i++)
        {
            rezultat =proveri_x(i,A,N,B,M);
            if(rezultat==1)
            break;
        }
        cout<<rezultat<<endl;
    }
    
    
    
    return 0;
}