#include <iostream>
#include <cmath>
#include <sstream>
#include <iterator>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

int main(){
    int t;
    cin>>t;
    int listt[t];
    for(int i=0; i<t; i++){
        bool stop = false;
        bool stop2 = false;
        int n,m;
        cin >> n>>m;
        int listnum[n-1];

        string s;

        getline( cin, s );
        getline( cin, s );
        istringstream is( s );
        vector<int> list1;
        int q;
        while ( is >> q ) list1.push_back( q );

        for(int x=1; x<n; x++){
            int num;
            num = abs(list1[x]-list1[0]);
            listnum[x-1] = num;
        }
        /*for (int x=0; x<(n-1);x++){
            cout<<listnum[x]<<endl;
        }*/
        int minlistnum = listnum[0];
        for (int x = 1; x<n-1; x++){
            if (listnum[x]<= minlistnum){
                minlistnum = listnum[x];
            }
        }
        for (int x=0; x<(n-1);x++){
                if ((listnum[x]%minlistnum)!=0){
                    stop = true;
                    /*break;       /*watch this maybe*/
                }
        }
        string s2;
        getline( cin, s2 );
        /*getline( cin, s2 );*/
        istringstream is2( s2 );
        vector<int> list2;
        int q2;
        while ( is2 >> q2 ) list2.push_back( q2 );
        if (stop!=false){
            listt[i] = 0;
        }else{
            for(int x=0; x<m; x++){
                if ((list2[x]-list1[0])%minlistnum==0){
                    stop2 = true;
                    break;
                }
            }
            if (stop2==true){ /*this is true i guess*/
                listt[i] = 0;
            }else{
                listt[i] = 1;
            }
        }
    }

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

    return 0;
}