#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int T, N, M, i, j = 0;
    unsigned long long int minR, r1;
    unsigned long long int a[75001];
    unsigned long long int b[75001];
    int output[5];
    bool flag;
    scanf_s("%d", &T);
    for (i = 1; i <= T; i++)
    {
        scanf_s("%d", &N);
        scanf_s("%d", &M);
        for (j = 0; j < N; j++)
        {
            scanf_s("%llu", &a[j]);
        }
        for (j = 0; j < M; j++)
        {
            scanf_s("%llu", &b[j]);
        }
        flag = true;
        sort(a, a + N);
        minR = a[1] - a[0];
        for (j = 2; j < N; j++)
        {
            r1 = a[j] - a[j - 1];
            if (r1 < minR)
            {
                minR = r1;
            }
        }
        for (j = 1; j < N && flag; j++)
        {
            if (minR == 0)
            {
                flag = false;
            }
            else if ((a[j] - a[0]) % minR != 0)
            {
                flag = false;
            }
        }
        for (j = 0; j < M&&flag; j++)
        {
            if (minR != 0)
            {
                if ((b[j] - a[0]) % minR == 0)
                {
                    flag = false;
                }
            }
        }
        if (flag)
        {
            output[i] = 1;
        }
        else
        {
            output[i] = 0;
        }
    }
    i = 1;
    while (i <= T)
    {
        printf("%d\n", output[i]);
        i++;
    }
}