#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t, res[101];
    cin >> t;

    for (int q = 0 ; q < t ; q++)
    {
        int n, m;
        cin >> n >> m;
        long long a[75001], b[75001], mini, d;
        mini = 9223372036854775807;
        d = 9223372036854775807;

        for (long long i = 0 ; i < n ; i++)
        {
            cin >> a[i];
            if (a[i] < mini) mini = a[i];
        }

        for (long long i = 0 ; i < m ; i++) cin >> b[i];

        for (long long i = 0 ; i < n - 1 ; i++)
            for (long long j = i + 1 ; j < n ; j++)
                if(abs(a[i] - a[j]) < d) d = abs(a[i] - a[j]);

        res[q] = 1;
        for (long long i = 0 ; i < n ; i++) if ((a[i] - mini) % d != 0) res[q] = 0;
        for (long long j = 0 ; j < m ; j++) if ((b[j] - mini) % d == 0) res[q] = 0;
    }
    for (int q = 0 ; q < t ; q++) cout << res[q] << endl;
}