#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>

using namespace std;

int postoji_x(int n, int m)
{
    int  i, da_ne;
    unsigned long long a, b, x, br;
    vector<unsigned long long> niz_a, niz_b, niz_x;
    for(i = 0; i < n; i ++)
    {
        cin >> a;
        niz_a.push_back(a);
    }
    for(i = 0; i < m; i ++)
    {
        cin >> b;
        niz_b.push_back(b);
    }
    sort(niz_a.begin(), niz_a.end());
    sort(niz_b.begin(), niz_b.end());
    for(i = 0; i < n; i ++)
    {
        x = niz_a[i + 1] - niz_a[i];
        niz_x.push_back(x);
    }
    sort(niz_x.begin(), niz_x.end());
    da_ne = 1;
    x = niz_x[0];
    br = niz_a[0];
    for(i = 1; i < n; i ++)
    {
        if((niz_a[i] - br) % x != 0)
        {
            da_ne = 0;
            i = n;
        }
    }
    for(i = 0; i < m; i++)
    {
        if((niz_b[i] - niz_a[0]) % x == 0)
        {
            da_ne = 0;
            i = m;
        }
    }
    return da_ne;
}
int main()
{
    int t, n, m, i, da_ne[5];
    cin >> t;
    for(i = 0; i < t; i ++)
    {
        cin >> n >> m;
        da_ne[i] = postoji_x(n, m);
    }
    for(i = 0; i < t; i ++)
    {
        cout << da_ne[i] << endl;
    }
    return 0;
}