#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define INF 1000000000
#define LINF 1000000000000000LL
#define pb push_back
#define all(x) x.begin(), x.end()
#define len(s) (int)s.size()
#define test_case { int t; cin>>t; while(t--)solve(); }
#define input(n, v) {for(int i = 0;i<n;i++) cin>>v[i];}
#define output(n, v) {for(int i = 0;i<n;i++) cout<<v[i]<<" "; cout<<endl;}
#define single_case solve();
#define line cout<<"------------"<<endl;
#define ios { ios_base::sync_with_stdio(false); cin.tie(NULL); }
using namespace std;
const int N = 75005;
ll a[N], b[N];

int main()
{
    ios
    int t;
    cin>>t;
    while(t--)
    {
        int n, m;
        cin>>n>>m;
        for(int i = 0;i<n;i++)
            cin>>a[i];
        for(int i = 0;i<m;i++)
            cin>>b[i];
        sort(a, a+n);
        sort(b, b+m);
        ll d = a[1] - a[0];
        int ans = 1;
        for(int i = 1;i<n;i++)
        {
            ll dd = a[i] - a[i-1];
            if(dd%d){ ans = 0; break; }
        }
        for(int i = 0;i<m;i++)
        {
            ll dd = b[i] - a[0];
            if(dd%d==0)
            {
                ans = 0;
                break;
            }
        }
        cout<<ans<<endl;
    }




    return 0;
}