#include <iostream>
#include <cmath>
#include <algorithm>
#define forn(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;
ll gcd(ll a, ll b) {
    if (b==0) return a;
    return gcd(b, a%b);
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int n, m;
        cin >> n >> m;
        ll gici = 0;
        ll a[n];
        forn(i, n) cin >> a[i];
        sort(a, a+n);
        ll lastnum = a[0];
        for (int i = 1; i < n; i++) {
            gici=gcd(gici, a[i]-lastnum);
            lastnum=a[i];
        }
        ll b[m];
        forn(i, m) cin >> b[i];
        forn(i, m) {
            if (abs(b[i]-a[0])%gici==0) {
                cout << 0 << endl;
                goto endit;
            }
        }
        cout << 1 << endl;
        endit: {}
    }
}