#include <bits/stdc++.h>
using namespace std;

bool moze(long long niz[], long long n, long long prvi, long long minraz)
{
    for (long long i = 1; i <= minraz; i++)
    {
        long long k = prvi;
        bool aha = true;
        while(k >= 0)
        {
            if (niz[k] == -1)
            {
                aha = false;
                break;
            }
            k -= i;
        }
        if (!aha) continue;
        k = prvi;
        while(k < n)
        {
            if (niz[k] == -1)
            {
                aha = false;
                break;
            }
            k += i;
        }
        if (aha){
            return true;
        }
    }
    return false;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    vector<int> rez;
    int t;
    cin >> t;

    for (int i = 0; i < t; i++)
    {
        long long n, m, pom = 0;
        cin >> n >> m;
        long long a[n];
        long long b[m];
        long long mini = 2000000000000000000;
        long long maks = -2000000000000000000;

        for (long long j = 0; j < n; j++)
        {
            cin >> a[j];
            if (a[j] < mini) mini = a[j];
            if (maks < a[j]) maks = a[j];
        }

        for (long long j = 0; j < m; j++)
        {
            cin >> b[j];
            if (b[j] < mini) mini = b[j];
            if (maks < b[j]) maks = b[j];
        }
        if (n == 2)
        {
            rez.push_back(1);
            continue;
        }

        long long niz[maks-mini+1];
        for (long long j = 0; j < maks-mini+1; j++)
        {
            niz[j] = 0;
        }

        for (long long j = 0; j < n; j++)
        {
            a[j] -= mini;
            niz[a[j]] = 1;
        }
        for (long long j = 0; j < m; j++)
        {
            b[j] -= mini;
            niz[b[j]] = -1;
        }

        long long prvi;
        for (long long j = 0; j < maks-mini+1; j++)
        {
            if (niz[j] == 1)
            {
                prvi = j;
                break;
            }
        }
        long long minraz = 2000000000000000000;
        for (long long j = 0; j < maks-mini+1; j++)
        {
            pom++;
            if (j == prvi) pom = 0;
            if (j != prvi)
            {
                if (niz[j] == 1)
                {
                    if (pom < minraz)
                    {
                        minraz = pom;
                    }
                    pom = 0;
                }
            }
        }

        if (moze(niz, maks-mini+1, prvi, minraz))
        {
            rez.push_back(1);
        }
        else
        {
            rez.push_back(0);
        }
    }

    for (int i = 0; i < t; i++)
    {
        cout << rez[i] << "\n";
    }

    return 0;
}
/*
1
4 3
1 7 4 13
3 11 9
*/