#include <iostream>
#include <algorithm>

using namespace std;

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

long long findGCD(long long arr[], long long n)
{
    long long result = arr[0];
    for (int i = 1; i < n; i++)
    {
        result = gcd(arr[i], result);

        if(result == 1)
        {
           return 1;
        }
    }
    return result;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    long t,n,m;
    cin>> t;
    long res [t];
    long brojac = 0;
    for (int i = 0; i < t; i++) {
        cin>> n;
        cin>> m;
        long long a [n];
        long long b [m];
        for (int j = 0; j < n; j++) {
            cin>> a[j];
        }
        long long asize = sizeof(a) / sizeof(a[0]);
        sort(a, a + asize);
        long long temp = a[0];
        for (int j = 0; j < n; j++) {
            a[j] -= temp;
        }
        asize = sizeof(a) / sizeof(a[0]);
        long long gcd = findGCD(a, asize);
        bool z = true;
        for (int j = 0; j < m; j++) {
            cin>> b[j];
            if((b[j] - temp)%gcd == 0){
                z = false;
            }
        }

        if((gcd != 1) && (z)){
            res[brojac] = 1;
            brojac++;
        }else{
            res[brojac] = 0;
            brojac++;
        }
    }

    for (int i = 0; i < t; i++){
        cout<< res[i] << endl;
    }

    return 0;
}