#include <bits/stdc++.h>
#include <bits/stdtr1c++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
#define pb push_back
#define endl "\n"
#define amfetamin ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cerr.tie(0);
#define ordered_set tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
using namespace __gnu_pbds;
const int N = 1e5 + 5;
int main()
{
amfetamin;
int t;
cin >> t;
while (t--)
{
    int n,m;
    int sol = 1;
    cin >> n >> m;
    ll a[n];
    ll b[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);
    ll gcd = a[1] - a[0];
    for (int i = 2; i < n; i++)
        gcd = __gcd(gcd, a[i] - a[i - 1]);
    ll c = a[0] % gcd;
    for (int i = 0; i < m; i++)
        if (b[i] % gcd == c)
        sol = 0;
    cout << sol << endl;
}


}