#include <stdio.h>
#include <stdlib.h>
int main()
{
    int t,n,m;
    scanf("%d", &t);
    int c[t];
    for(int i = 0; i < t; i++){
        scanf("%d%d", &n, &m);
        int a[n];
        int b[m];
        for (int j = 0; j<n; j++)
            scanf("%d", &a[j]);
        for (int j = 0; j<m; j++)
            scanf("%d", &b[j]);
        int x,x1;
        for (int j = 0; j<n-1; j++){
            if (a[j]>a[j+1])
                x1=a[j]-a[j+1];
            else
                x1=a[j+1]-a[j];
            if (j==0 || x1<x)
                x=x1;
        }
        for (int j = 0; j<m && j<n; j++)
            if (b[j]%x!=a[j]%x)
                c[i]=1;
            else
                c[i]=0;

    }
    for (int i = 0; i<t; i++)
        printf("%d\n", c[i]);
    return 0;
}