#include <bits/stdc++.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define ins insert
#define vi vector<int>
const int mxN = 1e9;
const ll mod = 1e9 + 7;

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, m;
        cin >> n >> m;
        vector<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.begin(), a.end());
        ll mndiff = a[1] - a[0];
        for (int i = 2; i < n; i++)
            mndiff = __gcd(mndiff, a[i] - a[i - 1]);
        if (mndiff == 1)
            cout << "0\n";
        else
        {
            bool ok = 1;
            for (int i = 0; i < m; i++)
            {
                if (abs(b[i] - a[0]) % mndiff == 0)
                {
                    ok = 0;
                    break;
                }
            }
            if (ok)
                cout << "1\n";
            else
                cout << "0\n";
        }
    }
}