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

using namespace std;

int delioci(int a, int b)
{
    int l=0,i=a,A[a%2+1];
    for(i=a; i>=2; i--)
        if(a%i==0)
            A[l++]=i;
    if(b==-1) return l;
    return A[b];
}

int main()
{
    unsigned int x1,x2,k1;
    unsigned short int T;
    unsigned long int n,m;
    //int y1,y2
    unsigned int i,j,b,z;
    unsigned int duzina;
    bool boool1=true;
    scanf("%hu",&T);
    for(i=0; i<T; i++)
    {
        boool1=true;
        scanf("%lu%lu",&n,&m);
        unsigned long int M1[m],N1[n];
        for(j=0; j<n; j++)
            scanf("%lu",&N1[j]);
        for(j=0; j<m; j++)
            scanf("%lu",&M1[j]);
        sort(N1,N1+n);
        sort(M1,M1+m);
        x1=N1[1]-N1[0];
        for(j=0; j<n-1; j++)
            if(N1[j+1]-N1[j]<x1) x1=N1[j];
        duzina=delioci(x1,-1);
        for(b=0; b<duzina; b++)
        {
            for(j=0; j<n-1; j++)
                if((N1[j+1]-N1[j])%delioci(x1,b)!=0) boool1=false;
            if(boool1) break;
        }
        k1=delioci(x1,b);
    //////////////////////////////////////////////////////////////////
        x2=M1[1]-M1[0];
        for(j=0; j<m-1; j++)
            if(M1[j+1]-M1[j]<x2) x2=M1[j+1]-M1[j];
        duzina=delioci(x2,-1);
        for(b=0; b<duzina; b++)
        {
            for(j=0; j<m-1; j++)
                if((M1[j+1]-M1[j])%delioci(x2,b)!=0) boool1=false;
            if(boool1) break;
        }
        for(j=0; j<n-1; j++)
            {
                if(N1[j+1]-N1[j]!=k1)
                    for(b=N1[j]; b<=N1[i+1]; b+=k1)
                        for(z=0; z<m; z++)
                            if(M1[z]==b) boool1=false;
            }
        printf("%d\n",boool1);

    }


}