#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
#define ll long long
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int t, n, m;
    cin>>t;
    while(t--){
        cin>>n>>m;
        vector<ll> a(n);
        for(ll &x : a) cin>>x;
        ll me=*min_element(all(a));
        ll g=0;
        for(int i=0; i<n; i++){
            g=__gcd(g, a[i]-me);
        }
        ll b, ostatak=a[0]%g, ind=1;
        while(m--){
            cin>>b;
            if((b%g)==ostatak) ind=0;
        }
        cout<<ind<<endl;
    }
}