#include <iostream>

using namespace std;

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

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

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

int main()
{
    int t, m, n;

    cin >> t;

    for(int j = 0; j < t; j++){
        cin >> m >> n;

        int p = 1;
        long long int a[m], r[m-1], b[n], x;

        for(int i = 0; i < m; i++)
            cin >> a[i];

        for(int i = 0; i < m-2; i++){
            if(a[i] >= a[i+1])
                r[i] = a[i]-a[i+1];
            else r[i] = a[i+1]-a[i];
        }

        x = findGCD(r, m-1);

        for(int i = 0 ; i < n; i++)
            cin >> b[i];

        for(int i = 0; i < n && p; i++)
            if(b[i]%x == 0)
                p = 0;

        cout << p << endl;
       /* cout << x;
        for(int i = 0; i < m-1; i++)
            cout << r[i] << endl;

        cout << endl;
        for(int i = 0; i < m; i++)
            cout << a[i] << endl;*/
    }
    return 0;
}