#include <bits/stdc++.h>

using namespace std;

int Dif(long long a[], long long difc[], int n)
{
    int minc;
    for(int j = 0; j < n - 1; j++)
    {
        difc[j] = a[j + 1] - a[j];
        if(j == 0)
        {
            minc = difc[j];
        }
        else
        {
            if(difc[j] < minc)
            {
                minc = difc[j];
            }
        }
    }
    return minc;
}
int main()
{
    int t;
    cin >> t;
    for(int i = 0; i < t; i++)
    {
        int n, m, k, minc;
        bool f = true;
        bool bo = true;
        cin >> n >> m;
        long long a[n];
        long long 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);
        long long difc[n - 1];
        minc = Dif(a, difc, n);
        if(minc == 1)
        {
            cout << "0";
        }
        else
        {
            for(int j = 0; j < n - 1; j++)
            {
                if(difc[j] > minc)
                {
                    if((difc[j] % minc) != 0)
                    {
                        bo = false;
                    }
                }
                else
                {
                    if((minc % difc[j]) != 0)
                    {
                        bo = false;
                    }
                    else
                    {
                        minc = difc[j];
                    }
                }
            }
            int rest = a[0] % minc;
            if(n == 2)
            {
                minc = difc[0];
                bo = true;
            }
            if(!bo)
            {
                cout << "0" << endl;
            }
            else
            {
                for(int j = 0; j < m; j++)
                {
                    if((b[j] % minc) != rest)
                    {
                        f = false;
                    }
                }
                if(f)
                {
                    cout << "0" << endl;
                }
                else
                {
                    cout << "1" << endl;
                }
            }
        }

    }
    return 0;
}