#include<bits/stdc++.h>
#define pii pair<int,int>
#define ll long long
#define pll pair<ll,ll>
#define ff first
#define ss second
#define pb push_back
using namespace std;
/*ll digni(ll x,ll y)
{
    if(y==0)return 1;
    if(y%2==0)
    {
        ll res=digni(x,y/2);
        return (res*res)%mod;
    }
    return (digni(x,y-1)*x)%mod;
}*/

int n,m;
ll a[100005];
ll b[100005];
ll gcd;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	
    int t;
    cin>>t;
    while(t > 0)
    {
        t--;
        cin>>n>>m;
        for(int i=0;i<n;i++)cin>>a[i];
        for(int i=0;i<m;i++)cin>>b[i];

        gcd = b[1] - b[0];
        for(int i=2;i<m;i++)
        {
            gcd = __gcd(gcd, b[i] - b[i - 1]);
        }

        bool ans = true;
        for(int i=0;i<n;i++)
        {
            ll diff = abs(a[i] - b[0]);
            if(diff % gcd == 0)
            {
                ans = false;
            }
        }

        cout<<ans<<"\n";
    }
 	return 0;
}