#include <iostream>
#include <algorithm>
using namespace std;
int n,m,a[100000],b[101000],t;
int main()
{ cin>>t;
while(t--){int s=1;
cin>>n>>m;
for(int i=1;i<=n; i++){
    cin>>a[i];
}
sort(a+1,a+n+1);
for(int i=1;i<=m; i++){
    cin>>b[i];
}
int r=20000;
for(int i=2;i<=n; i++){
    r=min(a[i]-a[i-1],r);
}
int j=1;
while(a[j]<b[m]){
    a[j]=a[1]+(j-1)*r;
    for(int i=1;i<=m; i++){
       if(b[i]==a[j]){s=0;a[j]=b[m]+2;i=m+1;}
}
j++;
}
    cout<<s<<endl;
} cout<<endl;
    return 0;
}