#include <bits/stdc++.h>
#define mod 998244353
#define gcd __gcd
#define ll long long
#define ull unsigned long long
#define inf 1e15
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define pii pair<int,int>
#define sz(a) int(a.size())
#define all(a) a.begin(),a.end()
#define rall(a) a.begin(),a.end(),greater<int>()
using namespace std;
#define maxn 75010
int n,m;
ull a[maxn];
ull b[maxn];
int main(){
	ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        cin >> n >> m;
        for(int i = 1;i<=n;i++) cin >> a[i];
        for(int i = 1;i<=m;i++) cin >> b[i];
        sort(a+1,a+n+1);
        ull r = a[2]-a[1];
        ull p;
        for(int i = 3;i<=n;i++){
            r = gcd(r,a[i]-a[i-1]);
            if(r==1){
                cout<<"0\n";
                goto lol;
            }
        }
        p = a[1]%r;
        for(int i = 1;i<=m;i++){
            if(b[i]%r==p){
                cout<<"0\n";
                goto lol;
            }
        }
        cout<<"1\n";
        lol:;
    }
    return 0;
}