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

#define sp ' '
#define ar array
#define fi first
#define se second
#define vt vector
#define ends "\n"
#define pb push_back
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

const long long MOD=1e9+7;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

void DBG() {
	cerr << "]" << endl;
}
template<class H, class... T> void DBG(H h, T... t) {
	cerr << to_string(h);
	if(sizeof...(t))
		cerr << ", ";
	DBG(t...);
}
#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)

/*

    srand(time(0));
    int N, M;
    vector<ll> A;
    vector<ll> B;
    function<int()>solve1=[&](){
        sort(all(A));
        ll g=A[1]-A[0];
        for(int i=2; i<N; ++i)
            g=__gcd(g, (A[i]-A[i-1]));

        bool ok=true;
        for(int i=0; ok&&i<M; ++i){
            if(abs(B[i]-A[0])%g==0)ok=false;
        }
        if(ok) return 1;
        return 0;
    };

    function<int()>solve2=[&](){
        sort(all(A));
        bool ok=false;
        for(int S=0; !ok&&S<=200; ++S){
            for(int x=1; !ok&&x<=A[N-1]-A[0];++x){
                bool ok1=true, ok2=true;
                for(int i=0; ok1&&i<N; ++i)
                    if(abs(S-A[i])%x!=0)ok1=false;

                for(int i=0; ok2&&i<M; ++i)
                    if(abs(S-B[i])%x==0)ok2=false;
                ok|=(ok1&&ok2);
                //if(S==A[0])dbg(ok1, ok2, x);
            }
        }
        int a=ok;
        dbg(a, solve1());
        return a;
    };
    int tr=0;
    for(int Q=0; Q<10000; ++Q){
        dbg(Q);
        N=2+rand()%10; M=2+rand()%10;
        set<ll> st;
        A.clear(); B.clear();
        for(int i=0; i<N; ++i){
            ll f;
            while(true){
                f=1+rand()%100;
                auto it=st.find(f);
                if(it==st.end()){
                    st.insert(f);
                    break;
                }
            }
            A.pb(f);
        }
        for(int i=0; i<M; ++i){
            ll f;
            while(true){
                f=1+rand()%100;
                auto it=st.find(f);
                if(it==st.end()){
                    st.insert(f);
                    break;
                }
            }
            B.pb(f);
        }
        if(solve1()==solve2())++tr;
        else{
            cout << N << sp << M << ends;
            for(int i=0; i<N; ++i)cout << A[i] << sp;
            cout << ends;
            for(int i=0; i<M; ++i)cout << B[i] << sp;
            cout << ends;
        }
    }
    cout << tr << ends;


*/

int main(void){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);

    int Q; cin >> Q;
    while(Q--){
        int N, M; cin >> N >> M;
        vector<ll> A(N); for(auto&a:A) cin >> a;
        vector<ll> B(M); for(auto&b:B) cin >> b;
        sort(all(A));
        ll g=A[1]-A[0];
        for(int i=2; i<N; ++i)
            g=__gcd(g, (A[i]-A[i-1]));

        bool ok=true;
        for(int i=0; ok&&i<M; ++i){
            if(abs(B[i]-A[0])%g==0)ok=false;
        }
        if(ok)cout << 1 << ends;
        else cout << 0 << ends;
    }

}