#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100001
void bublSort(int a[],int n)
{
    for(int i=n-2;i>=0;i--)
    {
        for(int j=0;j<=i;j++)
        {
            if(a[j]>a[j+1])
                swap(a[j],a[j+1]);
        }
    }
}
int findLargest(int* arr, int n)
{

    // Finding gcd of all the numbers
    // in the array
    int gcd = 0;
    for (int i = 0; i < n; i++)
        gcd = __gcd(arr[i], gcd);
    return gcd;
}
int main()
{
    int T,N,M,delilacA,delilacB;
    cin>>T;
    for(int i=0;i<T;i++)
    {
        cin>>N>>M;
        int a[N],b[M],razlikA[N-1],razlikB[M-1];
        for(int j=0;j<N;j++)
        {
            cin>>a[j];
        }
        for(int j=0;j<M;j++)
        {
            cin>>b[j];
        }
        bublSort(a,N);
        bublSort(b,M);
        for(int j=0;j<N-1;j++ )
        {
            razlikA[j]=a[j+1]-a[j];
        }
        for(int j=0;j<M-1;j++)
        {
            razlikB[j]=b[j+1]-b[j];
        }
        delilacA=findLargest(razlikA,N-1);
        delilacB=findLargest(razlikB,M-1);
        if(delilacA%delilacB==0 || delilacB%delilacA==0 )
            cout<<"0"<<endl;
        else
            cout<<"1"<<endl;

        }


}