#include <bits/stdc++.h>

using namespace std;

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

int main()
{
    int t, n, m, k;
    long long a, b, c, d, x, y;

    cin >> t;

    for(int i = 1; i <= t; ++i)
    {
        cin >> n >> m;

        cin >> a >> b;

        if(n == 2)
        {
            d = abs(a - b);
            c = b;
        }
        else
        {
            x = abs(a - b);
            for(int j = 3; j <= n; ++j)
            {
                cin >> c;
                y = abs(b - c);
                d = NZD(x, y);
                b = c;
                x = d;
            }
        }

        k = 1;

        for(int j = 1; j <= m; ++j)
        {
            cin >> x;
            y = abs(x - c);
            if(y % d == 0) k = 0;
        }

        cout << k << endl;
    }

    return 0;
}