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

#define MIN(A, B) (((A) < (B))? (A) : (B))

int cmpfunc (const void * a, const void * b) {
    if( *(long*)a - *(long*)b < 0 )
        return -1;
    if( *(long*)a - *(long*)b > 0 )
        return 1;
    return 0;
}

int main() {
    int t;
    scanf("%d", &t);
    for (int i = 0; i < t; ++i) {
        int n, m;
        long min;
        scanf("%d%d", &n, &m);
        long *a = malloc(n * sizeof(*a));
        for (int j = 0; j < n; ++j) {
            scanf("%ld", a+j);
        }
        qsort(a, n, sizeof(*a), cmpfunc);
        min = a[n-1] - a[n-2];
        for (int j=n-2; j>0; --j) {
            min = MIN(min, a[j] - a[j-1]);
        }
        free(a);
        long *b = malloc(n * sizeof(*b));
        for (int j = 0; j < m; ++j) {
            scanf("%ld", b+j);
        }
        qsort(b, n, sizeof(*b), cmpfunc);
        short moze = 1;
        for (int j = m-1; j > 0; --j) {
            if(b[j] - b[j-1] == min) {
                moze = 0;
                break;
            }
        }
        if(moze) {
            printf("1");
        } else {
            printf("0");
        }
        if(i != t-1) {
            printf("\n");
        }
        free(b);
    }

    return 0;
}