#include <bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    int n,m;
    scanf("%d", &t);

    for(int i=0;i<t;i++)
    {
        int ispis=1;
        scanf("%d%d", &n, &m);
        long long prihvacene[n];
        long long odbijene[m];
        for(int j=0;j<n;j++)scanf("%lld", &prihvacene[j]);
        for(int j=0;j<m;j++)scanf("%lld", &odbijene[j]);
        sort(prihvacene, prihvacene+n);
        sort(odbijene, odbijene+m);
        long long nzd = prihvacene[1]-prihvacene[0];
        for(int i=1;i<n-1;i++)
        {
            if( __gcd(prihvacene[i+1]-prihvacene[i],nzd)<nzd)nzd=__gcd(prihvacene[i+1]-prihvacene[i],nzd);

        }
        for(int j=0;j<m;j++)
        {
            if(odbijene[j]%nzd==prihvacene[0]%nzd)ispis=0;
        }
        printf("%d \n", ispis);
    }

    return 0;
}