#include <bits/stdc++.h>

using namespace std;

vector<long long> a;
vector<long long> b;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    for(int T = 0; T < t; T++)
    {
        a.clear();
        b.clear();
        long long n, m;
        cin >> n >> m;
        for(int i = 0; i < n; i++)
        {
            long long tr;
            cin >> tr;
            a.push_back(tr);
        }
        for(int i = 0; i < m; i++)
        {
            long long tr;
            cin >> tr;
            b.push_back(tr);
        }
        sort(a.begin(), a.end());
        if(a.size() == 1)
        {
            bool ind = true;
            for(int i = 0; i < b.size(); i++)
            {
                if(b[i] == a[0])
                {
                    ind = false;
                    break;
                }
            }
            if(ind)
            {
                cout << 1 << endl;
            }
            else
            {
                cout << 0 << endl;
            }
            continue;
        }
        long long nzd = a[1] - a[0];
        for(int i = 2; i < n; i++)
        {
            nzd = __gcd(nzd, a[i] - a[i - 1]);
        }
        long long br = a[0] % nzd;
        //cout << nzd << "----------------------------------------" << endl;

        bool ind = true;
        for(int i = 0; i < m; i++)
        {
            if(b[i] % nzd == br)
            {
                ind = false;
                break;
            }
        }
        if(ind)
        {
            cout << 1 << endl;
        }
        else
        {
            cout << 0 << endl;
        }
    }

    return 0;
}