#include <iostream>
#include <algorithm>
#define ll long long

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--){
        int n, m;
        cin >> n >> m;
        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, a+n);
        bool bad = false;
        ll diff = INT_MAX;
        for (int i = 0; i < n-1; ++i)
            diff = min(diff, a[i+1]-a[i]);
        for (int i = 0; i < n-1; ++i){
            if ((a[i+1] - a[i]) % diff != 0){
                cout << 0 << endl;
                bad = true;
                break;
            }
        }
        if (bad) continue;
        ll point = a[0] % diff;
        for (int i = 0; i < m; ++i){
            if (b[i] % diff == point){
                cout << 0 << endl;
                bad = true;
                break;
            }
        }
        if (bad) continue;
        cout << 1 << endl;
    }
    return 0;
}