#include <iostream>

using namespace std;

int nzd(int x,int y)
{
    int a=x,b=y,c;
    while(b!=0)
    {
        c=a%b;
        a=b;
        b=c;
    }
    if(a>0) return a;
    else return -a;
}

int main()
{
    int t,i;
    int n,m,j;
    int a1,a;
    int b;
    int x;
    int ok;
    cin>>t;
    for(i=0;i<t;i++)
    {
        cin>>n>>m;
        cin>>a1;
        cin>>a;
        x=a-a1;
        for(j=2;j<n;j++)
        {
            cin>>a;
            x=nzd(a-a1,x);
        }
        ok=1;
        for(j=0;j<m;j++)
        {
            cin>>b;
            if((b-a1)%x==0) ok=0;
        }
        cout<<ok<<endl;
    }
}