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

ll a[100000];
ll b[100000];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll t, m, n, gcd, ostatak, rez;
    cin >> t;
    while(t--)
    {
        rez=1;
        cin >> n >> m;
        for(ll i=0;i<n;i++)
        {
            cin >> a[i];
        }
        for(ll i=0;i<m;i++)
        {
            cin >> b[i];
        }
        sort(a,a+n);
        gcd=a[1]-a[0];
        for(int i=2;i<n;i++)
        {
            gcd=__gcd(gcd, a[i]-a[i-1]);
        }
        ostatak = a[0]%gcd;
        for(int i=0;i<m;i++)
        {
            if(b[i]%gcd==ostatak && b[i]>=a[0] && b[i]<=a[n-1])
            {
                rez=0;
                break;
            }
        }
        cout << rez << "\n";
    }
    return 0;
}