#include <iostream>
#include <cmath>
#include <algorithm>
typedef long long ll;
#define forn(i, n) for (ll i = 0; i < n; i++)
using namespace std;
ll gcd(ll a, ll b) {
    if (b==0) return a;
    return gcd(b, a%b);
}
const int oo = 75007;
ll a[oo], b[oo];
int main() {
    ios::sync_with_stdio(false);
    cout.tie(0);
    cin.tie(0);
    ll t, n, m;
    scanf("%lld", &t);
    while (t--) {
        scanf("%lld%lld", &n, &m);
        ll gici = 0;
        forn(i, n) scanf("%lld", &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];
        }
        forn(i, m) scanf("%lld", &b[i]);
        forn(i, m) {
            if (abs(b[i]-a[0])%gici==0) {
                printf("0\n");
                goto endit;
            }
        }
        printf("1\n");
        endit: {}
    }
}