#include <iostream>

int IndeksMin(unsigned long long int niz[75001], int n, int i)
{
    int minlnd = i;
    for (int j = i + 1; j < n; j++)
    {
        if (niz[minlnd] > niz[j])
        {
            minlnd = j;
        }
    }
    return minlnd;
}

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