#include <bits/stdc++.h>

using namespace std;

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

    int t;

    cin >> t;

    while (t--) {
        bool has = true;

        int n, m;

        cin >> n >> m;

        uint64_t a[n], diff[n - 1];

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

        sort(a, a + n);

        for (int i = 0; i < n - 1; i++) {
            diff[i] = a[i + 1] - a[i];
        }

        uint64_t other = diff[0];

        for (int i = 1; i < n - 1; i++) {
            other = __gcd(other, diff[i]);
        }

        if (other == 1) {
            has = false;
        }

        uint64_t first = a[0] % other;
        uint64_t b;

        for (int i = 0; i < m; i++) {
            cin >> b;

            if ((b - first) % other == 0) {
                has = false;
            }
        }

        cout << has << "\n";
    }
}