#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int t, n, m, k;
    cin >> t;
    vector<int> c(t,1);
    long long x = 0;
    for (int i = 0;i < t;i++) {
        cin >> n >> m;
        vector<long long> a(n);
        long long b;
        for (int j = 0;j < n;j++)
            cin >> a[j];
        sort(a.begin(), a.end());
        for (int j = 0;j < n - 1;j++)
            x = a[j + 1] - a[j];
        k = 0;
        while (k < m) {
            cin >> b;
            if ((b % x) - (a[0] % x) == 0) {
                c[i] = 0;
                k++;
                break;
            }
            k++;
        }
        while (k < m) {
            cin >> b;
            k++;
        }
    }
    for (auto y : c)
        cout << y << endl;
}