#include <bits/stdc++.h>

using namespace std;

int nzd(long long int a,long long int b){
    if (a == 0)
       return b;
    if (b == 0)
       return a;
    if (a == b)
        return a;
    if (a > b)
        return nzd(a-b, b);
    return nzd(a, b-a);
}

int main()
{
    long long int t,n,m,a[75001],b[75001],s[6],x=0,y=0;
    cin >> t;
    s[0]=1;s[1]=1;s[2]=1;s[3]=1;s[4]=1;s[5]=1;
    for (int j=0;j<t;j++){
            x=0;y=0;
        cin >> n >> m;
        for (int i=0;i<n;i++){
            cin >> a[i];
                if (i==2) x=nzd(abs(a[2]-a[1]),abs(a[1]-a[0]));
                else if (i>2) {y=x;x=nzd(y,a[i]-a[i-1]);}
        }
        if (x!=0){
        if (x==1) s[j]*=0;
        for (int i=0;i<m;i++){
            cin >> b[i];
            if (nzd(x,abs(b[i]-a[0]))!=1) s[j]*=0;
        }
        }
        else {
            x=abs(a[0]-a[1]);
            for (int i=0;i<m;i++){
                cin >> b[i];
                if (abs(b[i]-a[0])%x==0) s[j]*=0;
            }
        }

    }
    for (int i=0;i<t;i++) cout << s[i] << endl;
    return 0;
}