#include <iostream>
#include <cmath>
#include <sstream>
#include <string>
#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];
        int list1[n];

        for (int x = 0; x <= n - 1; x++){
            cin >> list1[x];
        }
        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*/
                }
        }
        int list2[m];
        for (int x = 0; x <= m - 1; x++){
            cin >> list2[x];
        }
        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;
}