#include <bits/stdc++.h>
#define ll long long
#define MAXN 75010

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

    int t, res[5];
    cin >> t;
    ll a[MAXN], b[MAXN];
    for (int t_i = 0; t_i < t; t_i++)
    {
        int n, m;
        cin >> n >> m;
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
        }
        for (int i = 0; i < m; i++)
        {
            cin >> b[i];
        }
        ll x = abs(a[0] - a[1]);
        for (int i = 1; i < n; i++)
        {
            x = __gcd(x, abs(a[i] - a[i-1]));
        }
        ll mod = a[0] % x;
        res[t_i] = true;
        for (int i = 0; i < m; i++)
        {
            if (b[i] % x == mod)
            {
                res[t_i] = false;
                break;
            }
        }
    }
    for (int i = 0; i < t; i++)
        cout << res[i] << '\n';
    return 0;
}