#include <bits/stdc++.h>

#include <cstdio>
#include <cstdlib>

unsigned long long arr1 [75002];

const unsigned long long max_int = (1e+18)+5;

unsigned long long subtract(unsigned long long a, unsigned long long b)
{
    if(a>b)
        return a-b;
    return b-a;
}

int main()
{

    int t;
    scanf("%d",&t);
    long n,m;
    for(int k=0; k<t; k++) {
        bool flag = false;
        scanf("%ld%ld",&n,&m);
        unsigned long long min1=max_int, min2=max_int-1;
        for(long i=0; i<n; i++) {
            scanf("%llu",&arr1[i]);
            if(arr1[i]<min1) {
                min2=min1;
                min1=arr1[i];
                continue;
            }
            if(arr1[i]<min2) {
                min2=arr1[i];
            }
        }
        unsigned long long diff = min2-min1;
        unsigned long long temp;
        for(long i=0; i<m; i++) {
            scanf("%llu", &temp);
            if(subtract(min1,temp)%diff==0) {
                flag = true;
            }
        }
        if(flag) {
            puts("0");
            continue;
        }
        for(long i=0; i<n; i++)
            if(subtract(min1,arr1[i])%diff!=0) {
                puts("0");
                flag = true;
                break;
            }
        if(flag) continue;
        puts("1");
    }

    return 0;
}