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

ll gcd(ll a, ll b) {
   if (b == 0)return a;
   return gcd(b, a % b);
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t; cin>>t;
    while(t--){
        int i,j,n,m;
        cin>>n>>m;
        vector<ll> a(n),b(m);
        for(i=0;i<n;++i)cin>>a[i];
        for(i=0;i<m;++i)cin>>b[i];

        j = min_element(begin(a),end(a)) - begin(a);
        ll x = 0;
        for(i=0;i<n;++i){
            if(i!=j){
                x = gcd(a[i]-a[j],x);
            }
        }
        ll y = a[0]%x;

        for(i=0;i<m && b[i]%x!=y;++i);
        cout << bool(i==m) << '\n';
    }
}