#include<bits/stdc++.h>
#define ll long long
#define MAXN (int)1e5
using namespace std;
int t, n, m;
ll a[MAXN];
ll razlike[MAXN];
ll b[MAXN];
int main()
{
    cin >> t;
    while(t--)
    {
        cin >> n >> m;
        for(int i=0;i<n;i++)cin >> a[i];
        for(int i=0;i<m;i++)cin >> b[i];
        sort(a, a+n);
        for(int i=1;i<n;i++)razlike[i-1] = a[i]-a[i-1];
        ll nzd;
        if(n==2)nzd = razlike[0];
        else
        {
            nzd = __gcd(razlike[0], razlike[1]);
            for(int i=2;i<n-1;i++)nzd = __gcd(nzd, razlike[i]);
        }
        ll mod = a[0]%nzd;
        bool ima = false;
        for(int i=0;i<m;i++)
        {
            if(b[i]%nzd==mod)
            {
                ima = true;
                cout << "0\n";
                break;
            }
        }
        if(!ima)cout << "1\n";
    }
    return 0;
}