#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ll;

int main() {
    int t; cin >> t;
    for (int I = 0; I < t; I++) {
        int n, m; cin >> n >> m;
        vector<ll> v(n);
        vector<ll> vm(m);
        bool flag = false;
        for (int i = 0; i < n; i++) {
            cin >> v[i];
        }
        for (int i = 0; i < m; i++) {
            cin >> vm[i];
        }
        sort(v.begin(), v.end());
        ll x = v[n-1] + 1;
        for (int i = 1; i < n; i++) {
            x = min(x, (v[i]-v[i-1]));
        }
        for (int i = 0; i < m && !flag; i++) {
            if(((vm[i] - 1) % x == 0 && vm[i] != 1) || (vm[i] == 1 && ((v[n-1] - 1) % x == 0))) {
                flag = true;
            }
        }
        if(flag) cout << "0\n";
        else cout << "1\n";
    }
    return 0;
}