#include <bits/stdc++.h>

using namespace std;

long long NZD(long long a, long long b){
if((a%b)==0)return b;
return NZD(b,(a%b));
}

long long najm (long long a[], int n)
{
    long long br = NZD(a[0], a[1]);
    for (int i = 2; i < n; i++)
        br = NZD (br, a[i]);
    return br;
}

int main()
{
    int t;
    cin >> t;
    for (int i = 0; i < t; i++)
    {
        int n, m;
        cin >> n >> m;
        long long a[n], b[m], c[n-1], br;
        for (int j = 0; j < n; j++)
            cin >> a[j];
        for (int j = 0; j < m; j++)
            cin >> b[j];
        sort (a, a+n);
        for (int j = 0; j < n-1; j++)
            c[j] = a[j+1] - a[j];
        if (n == 2)
            br = c[0];
        else
            br = najm(c,n-1);

        int p = a[0] % br, t = 1;
        for (int j = 0; j < m; j++)
        {
            if (b[j]%br == p)
            {
                t = 0;
                break;
            }
        }
        if (t == 0)
            cout << 0 << endl;
        else
            cout << 1 << endl;
    }
}