#include <iostream>
#include <string>
#include <bits/stdc++.h>
#include <stdlib.h>
#include <cstdlib>
using namespace std;
int main()
{
    int t;
    cin>>t;
    int res[t];
    int re=0;
    for(int i=0;i<t;i++)
    {
        int n,m;
        cin>>n>>m;

        unsigned long long dob[n];
        unsigned long long los[m];
        unsigned long long raz[n-1];

        for(int j=0;j<n;j++)
        {
            cin>>dob[j];
        }

        for(int j=0;j<m;j++)
        {
            cin>>los[j];
        }

        unsigned long long minn=9223372036854775807;

        for(int j=0;j<n-1;j++)
        {
           long long x=dob[j]-dob[j+1];
            raz[j]=abs(x);
            if(raz[j]<minn) minn=raz[j];
        }

        
        
        
        for(int j=0;j<n-1;j++)
        {
            ponovi:
            if(raz[j]%minn!=0)
            {
              for(int i=2;i<minn;i++) if(minn%i==0)minn=minn/i;
              goto ponovi;  
            }
        }

        //cout<<endl<<minn;
        int s=1;
        
        for(int j=0;j<m;j++)
        {
           if((los[j]-dob[0])%minn==0) s=0;
        }
        res[re]=s;
        re++;
    }
    for(int j=0;j<t;j++)
    {
       cout<<res[j]<<endl;
    }
  
}