#include <iostream>

using namespace std;

int main()
{
    int t, n, m, i, j, k, raz_min, tmp, p, g = 0, s, r = 0;
    cin >> t;
    for (k = 0; k < t; k++) {
        cin >> n >> m;
        int a[n], b[m];
        for(i = 0; i < n; i++)
            cin >> a[i];
        raz_min = abs(a[1] - a[0]);
        for (i = 0; i < n; i++) {
            for (j = i+1; j < n; j++) {
                tmp = abs(a[i] - a[j]);
                if (tmp < raz_min)
                    raz_min = tmp;
            }
        }
        s = a[0]%raz_min;
        for(j = 0; j < m; j++){
            cin >> b[j];
            if(b[j]%raz_min == s)
            r = 1;
        }
        if(r == 0)
            cout << 1;
        else
            cout << 0;
        }
    return 0;
}