#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int MAX=75000;
ll mind(ll n){
    if(n%2==0)
        return 2;
    for(int i=3;i<=sqrt(n);i+=2)
        if(n%i==0)
            return i;
    return n;
}
int main(){
	int T,N,M,C[MAX];
	ll A[MAX],B[MAX];
	cin>>T;
	for(int i=0;i<T;i++){
        C[i]=1;
        cin>>N>>M;
        for(int j=0;j<N;j++)
            cin>>A[j];
        sort(A,A+N);
        ll X=mind(A[1]-A[0]);
        for(int j=2;j<N;j++)
            if((A[j]-A[j-1])%X!=0){
                C[i]=0;
                break;
            }
        for(int j=0;j<M;j++)
            cin>>B[j];
        sort(B,B+M);
        ll br=0;
        for(int j=1;j<M;j++)
            if((B[j]-B[j-1])%X==0)
                br++;
        if(br==M-1)
            C[i]=0;
	}
	for(int i=0;i<T;i++)
        cout<<C[i]<<endl;
	return 0;
}