#include<iostream>
using namespace std;
unsigned int difabs(unsigned long int a, unsigned long int b)
{
    if(a>=b) return a-b;
    return b-a;
}
unsigned int gcd(unsigned long int a, unsigned long int b)
{
    if(a==0) return b;
    if(a<b) return gcd(b, a);
    if(b==1) return 1;
    return gcd(a%b, b);
}
bool resi()
{
    int n, m;
    cin>>n>>m;
    unsigned long int x;
    unsigned long int rem;
    unsigned long int a, b, c;
    for(int i=2; i<n; i++)
    {
        if(i==2)
        {
            cin>>a>>b>>c;
            x=gcd(difabs(a,b),difabs (b,c));
        }
        else
        {
            cin>>a;
            x=gcd(x, difabs(a,c));
            c=a;
        }
    }
    rem=c%x;
    bool k=true;
    for(int i=0; i<m; i++)
    {
        cin>>a;
        if(k && a>=rem && (a-rem)%x==0) k=false;
    }
    return k;
}
int main()
{
    int t;
    cin>>t;
    for(int i=0; i<t; i++)
    {
        if(resi()) cout<<1<<endl;
        else cout<<0<<endl;
    }
}