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

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

int main()
{
    int t;
    cin >> t;
    int t0 = t;
    int w = 0;
    int aw[t0];
    for(int r = 0; r < t; r++){
        long long n, m;
        cin >> n >> m;
        long long a[n], b[m];
        for(long long i = 0; i < n; i++)
            cin >> a[i];
        for(long long i = 0; i < m; i++)
            cin >> b[i];
        int v = sizeof(a) / sizeof(a[0]);
        sort(a, a + v);
        long long x = a[1] - a[0];
        for(long long i = 1; i < n; i++){
                if(a[i + 1] - a[i] < x)
                    x = gcd(a[i + 1] - a[i], x);
        }
        long long ans = 1;
        long long k = 0;
        while(ans && k < m){
            if(b[k] % x == a[0] % x)
                ans = 0;
            k++;
        }
        aw[w] = ans;
        w++;
    }
    for (int i = 0; i < t0; i++)
        cout << aw[i] << endl;
    return 0;
}