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

using namespace std;

int main()
{
    long long t,n,m,br1=0,br2=0,minn=0;
    scanf("%lld",&t);
    for (long long i=0;i<t;i++)
    {
        scanf("%lld%lld",&n,&m);
        long long *a=(long long*) malloc (n*sizeof(long long));
        for (long long i=0;i<n;i++) scanf("%lld",&a[i]);
        long long *b=(long long*) malloc (m*sizeof(long long));
        for (long long i=0;i<m;i++) scanf("%lld",&b[i]);
        sort(a,a+n);
        sort(b,b+m);
        minn=a[1]-a[0];
        br1=0;
        br2=0;
        for (long long i=0;i<n-1;i++)
            if (a[i+1]-a[i]<minn) minn=a[i+1]-a[i];
        for (long long i=1;i<n;i++)
            if ((a[i]-a[0])%minn==0) br1++;
        for (long long i=0;i<m;i++)
            if ((b[i]-a[0])%minn!=0) br2++;
        if (br1==n-1 && br2==m) printf("1\n");
        else printf("0\n");
    }
    return 0;
}