#include <stdio.h>
#include <stdlib.h>

typedef struct nodes
{
    long value;
    struct nodes* next;
} node;
long aps(long a)
{
    if(a >= 0)
        return a;
    else
        return -a;
}
void swap(long* a, long* b)
{
    long t = *a;
    *a = *b;
    *b = t;
}
long partition (long arr[], long low, long high)
{
    long pivot = arr[high];
    long i = (low - 1);

    for (long j = low; j <= high- 1; j++)
    {
        if (arr[j] < pivot)
        {
            i++;
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    return (i + 1);
}
void quickSort(long arr[], long low, long high)
{
    if (low < high)
    {
        long pi = partition(arr, low, high);
        quickSort(arr, low, pi - 1);
        quickSort(arr, pi + 1, high);
    }
}

long prihvacene(node *a, int n)
{
    long b[n];
    node* temp;
    temp = a;
    for(int i = 0; i < n; i++)
    {
        b[i] = temp->value;
        temp = temp->next;
    }
    quickSort(b, 0, n-1);
    long x = aps(b[0]-b[1]);
    for(int i = 1; i < n-1; i++)
    {
        if(x > aps(b[i] - b[i+1]))
        {
            x = aps(b[i] - b[i+1]);
        }
    }
    for(int i = 0; i < n-1; i++)
    {
        if(aps(b[i] - b[i+1]) % x != 0)
        {
            x = 1;
            i = n;
        }
    }
    return x;
}
int odbijene(node *a, int n, long t, long x)
{
    int rez = 1;
    node* temp;
    temp = a;
    for(int i = 0; i < n; i++)
    {
        if(aps(temp->value - t) % x == 0)
        {
            rez = 0;
            break;
        }
        temp = temp->next;
    }
    return rez;
}

int main()
{
    int t;
    scanf("%d", &t);
    int n[t], m[t], tacno[t];
    long x[t];
    node* a[t];
    node* b[t];
    node* temp;
    long input;
    for(int i = 0; i < t; i++)
    {
        scanf("%d", &n[i]);
        scanf("%d", &m[i]);
        a[i] = (node*)malloc(sizeof(node));
        temp = a[i];

        for(int j = 0; j < n[i]; j++)
        {
            scanf("%ld", &input);
            temp->value = input;
            temp->next = (node*)malloc(sizeof(node));
            temp = temp->next;
        }
        temp = a[i];
        x[i] = prihvacene(temp, n[i]);

        b[i] = (node*)malloc(sizeof(node));
        temp = b[i];

        for(int j = 0; j < m[i]; j++)
        {
            scanf("%ld", &input);
            temp->value = input;
            temp->next = (node*)malloc(sizeof(node));
            temp = temp->next;
        }
        temp = b[i];
        tacno[i] = odbijene(temp, m[i], a[i]->value, x[i]);
    }
    for(int i = 0; i < t; i++)
    {
        printf("%d\n", tacno[i]);
    }
    return 0;
}