#include <bits/stdc++.h>
#define ll long long
#define MAXN 75010
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

    int t, res[5];
    cin >> t;
    ll a[MAXN], b[MAXN];
    for (int t_i = 0; t_i < t; t_i++)
    {
        int n, m;
        ll min1 = LLONG_MAX, min2 = LLONG_MAX;
        cin >> n >> m;
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
            if (a[i] < min1) min1 = a[i];
            else if (a[i] < min2) min2 = a[i];
        }
        for (int i = 0; i < m; i++)
        {
            cin >> b[i];
        }
        ll x = min2 - min1;
        ll mod = min1 % x;
        res[t_i] = true;
        for (int i = 0; i < n; i++)
        {
            if (a[i] % x != mod)
            {
                res[t_i] = false;
                break;
            }
        }
        if (!res[t_i]) continue;
        for (int i = 0; i < m; i++)
        {
            if (b[i] % x == mod)
            {
                res[t_i] = false;
                break;
            }
        }
    }
    for (int i = 0; i < t; i++)
        cout << res[i] << '\n';
    return 0;
}