#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 = 10e+18;

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

        sort(a, a + n);

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

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

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

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

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