#include <iostream>
#include <algorithm>

using namespace std;

int max_step[5];
bool ima[5];

int main(void)
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    for( int i = 0; i < 5; i++ )
        ima[i] = 1;

    int t, n, m;
    long long int a, b, a0;

    cin >> t;

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

        cin >> a0;
        for( int j = 0; j < n-1; j++ )
        {
            cin >> a;
            max_step[i] = __gcd((long long int)max_step[i], abs(a-a0));
            a0 = a;
        }

        for( int j = 0; j < m; j++ )
        {
            cin >> b;
            ima[i] *= !(abs(a-b)%max_step[i]==0);
        }
    }

    for( int i = 0; i < t; i++ )
        cout << ima[i] << endl;

    return 0;
}