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

ll a[75010];
ll b[75010];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    int t;
    cin >> t;
    while(t--)
    {
    int n,m;
    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);
    sort(b,b+m);

    ll gcda = a[n-1]-a[0];
    for(int i = 1; i < n; i++) gcda = __gcd(gcda,a[i]-a[i-1]);


    //ll gcdb = b[m-1]-b[0];
    ll gcdtemp;
    ll r1,r2;
    auto it1 = lower_bound(a,a+n,b[0]);
    auto it2 = upper_bound(a,a+n,b[0]);
    r1 = abs(b[0]-*it1);
    r2 = abs(b[0]-*it2);
    gcdtemp = __gcd(r1,r2);
    ll lcm = abs(r1*r2)/gcdtemp;
    for(int i = 1; i < m; i++) {
        it1 = lower_bound(a,a+n,b[i]);
        it2 = upper_bound(a,a+n,b[i]);
        gcdtemp = __gcd(abs(b[i]-*it1),abs(b[i]-*it2));
        r1 = abs(b[i]-*it1);
        r2 = abs(b[i]-*it2);
        lcm = abs(lcm*(abs(r1*r2)/gcdtemp))/__gcd(lcm,gcdtemp);
    }
    gcdtemp = __gcd(gcda,lcm);
    if(gcdtemp < gcda)
        cout << 1 << "\n";
    else
        cout << 0 << "\n";
    //cout << gcda << " " << gcdb << endl;
    }

    return 0;
}