#include <iostream>
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll mxN = 75010;

ll A[mxN];
ll B[mxN];

ll gcd( ll u,  ll v)
    if (!a || !b)
        return a | b;
    unsigned shift = __builtin_ctz(a | b);
    a >>= __builtin_ctz(a);
    do {
        b >>= __builtin_ctz(b);
        if (a > b)
            swap(a, b);
        b -= a;
    } while (b);
    return a << shift;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll N,M,T;
    cin >> T;
    while(T--)
    {
        cin >> N >> M;
        cin >> A[0];
        ll minGod = A[0];
        for(int i = 1; i < N; i++)
        {
            cin >> A[i];
            minGod = min(minGod, A[i]);
        }
        for(int i = 0; i < M; i++)
        {
            cin >> B[i];
        }
        ll factor = abs(A[1] - A[0]);
        ll newFactor;
        for(int i = 2; i < N; i++)
        {
            newFactor = abs(A[i] - A[i - 1]);
            factor = gcd(factor, newFactor);
            if(factor == 1)
            {
                break;
            }
        }
        bool ok = true;
        for(int i = 0; i < M; i++)
        {
            if((B[i] - minGod) % factor == 0)
            {
                ok = false;
                break;
            }
        }
        cout << ok << "\n";
    }
}