#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	int t,n,m,i=0,p;
	cin>>t;
	while(t--)
	{
		p=1;
		cin>>n>>m;
		long long x[n],y[m],s[m],r;
		for(i=0;i<n;i++)
		cin>>x[i];
		for(i=0;i<m;i++)
	    {
		cin>>y[i];
		s[i]=(long)abs(y[i]-x[0]);
	    }
	    r=abs(x[1]-x[0]);
	    for(i=2;i<n;i++)
	    {
	    	if(r%(long)abs(x[i]-x[i-1])==0)
	    	r=abs(x[i]-x[i-1]);
	    	else if((long)abs(x[i]-x[i-1])%r!=0)
	    	p=0;
	    }
	    for(i=0;i<m;i++)
	    if(s[i]%r==0)
	    p=0;
	    cout<<p<<endl;
	}
	return 0;
}