#include <iostream>

using namespace std;

int main()
{
    long long t, i, n, m, j, k, br=0, l=1, p=10000000000000000, minn=0;
    cin >> t;
    for(i=0;i<t;i++){
        cin >> n >> m;
        long long a[n], b[m];

        for(j=0;j<n;j++){
            cin >> a[j];
            br=a[j]-a[j-1];}

        for(k=0;k<m;k++)
            cin >> b[k];

    l=max(br, l);
    p=min(br, p); // minimalna razlika
    minn = a[0];
    
    if(a[j]>minn)
        minn = a[j]; // minimalan brooj niza

    if(p==0)
            cout << 0 << endl;
    else if(l%p == 0){

        if((b[k]-minn)%p == 0)
            cout << 0 << endl;
        else
            cout << 1 << endl;}
    else
        cout << 0 << endl;
    }
    return 0;
}