#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(), a.end()
#define ff(i,a,b) for(int i=a;i<=b;i++)
#define fb(i,b,a) for(int i=b;i>=a;i--)

using namespace std;
typedef unsigned long long ll;
typedef pair<int,int> pii;
const int maxn = 100005;
const int inf = 1e9 + 5;

int n, m;
ll a[maxn];
ll b[maxn];

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

void Solve(){
    cin >> n >> m;
    ff(i,1,n)cin >> a[i];
    ff(i,1,m)cin >> b[i];
//    cout << endl;
    if(n == 2){
        cout << 1 << endl;
        return;
    }
    sort(a + 1, a + n + 1);
    ll nzd = 0;
    ff(i,2,n){
        ll x = a[i - 1];
        ll y = a[i];
        nzd = gcd(nzd, y - x);
    }
//    cout << "nzd: " << nzd << endl;
    ll zad = (a[n] - a[1]) / nzd;
//    cout << "zad: " << zad << endl;
    ff(i,1,n){
        ll x = b[i] - a[1];
        // jos samo pogledati ovo (b[i] <= a[n])
        if(x % nzd)continue;
        ll kol = x / nzd;
        if(kol <= zad){
            cout << 0 << endl;
            return;
        }
    }
    cout << 1 << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cout.tie(nullptr);
    cin.tie(nullptr);
    int t = 1;
    cin >> t;
    while(t--)Solve();
    return 0;
}
/**

4 3
1 7 4 13
3 11 9

3 2
9 5 3
7 11

2 3
3 15
1 7 2

**/