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

#define endl "\n"
#define ll long long
#define PB push_back
#define F first
#define S second
#define all(a) a.begin(), a.end()
#define MAX 1000
#define Fast ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define w(a) cout<<#a<<" : "<<a<<endl;

int n,m;

bool solve()
{
        ll a[n],b[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);
        ll raz=a[1]-a[0];
        for(int i=0; i<n-1; ++i)
        {
            raz=__gcd(raz,a[i+1]-a[i]);
        }
        ll mod=a[0]%raz;
        for(int i=0; i<m; ++i)
        {
            if(b[i]%raz==mod) return 0;
        }
        return 1;
}

int main()
{
    Fast;
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        cout<<solve()<<endl;
    }
    return 0;
}