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

using namespace std;

int main()
{
    int t, n, m;
    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);
        vector<long long> b(m);
        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];
        for (int j = 0;j < m;j++) {
            cin >> b[j];
            if ((b[j]%x) - (a[0]%x) == 0) c[i] = 0;
        }  
    }
    for (auto y : c)
        cout << y << endl;
}