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

bool nacinesto();
int main() {
    cin >> T;
    int t[T];
    for (int i=1; i<=T; i++) {
        t[i]=nacinesto();
    }

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

    return 0;
}

bool nacinesto() {
    int N, M, A, B;
    int c=0;
    bool q=1;
    //cout << "uneti N i M";
    cin >> N >> M;
    int a[N];
    //cout << "uneti A";
    for (int i=1; i<=N; i++) {
        cin >> A;
        a[i]=A;

        if (i!=1) {
           for (int j=i-1; j<=N; j++) {
               if (a[j]>a[i]) swap(a[j], a[i]);
           }
        }
    }

    int minimum=100000000000000;
    for(int i=N; i>1; i--) {
            if ((a[i]-a[i-1])<= minimum) {
                minimum= a[i]-a[i-1];
            }
    }

    int ostatak=a[N]%minimum;

    //cout << "uneti B";
    for (int i=1; i<=M; i++) {
        cin >> B;

        if (B%minimum==ostatak) {
            q=0;
        }
    }
    return q;
}