#include <iostream>

using namespace std;

int main()
{
    int t,n,m,i,j,min = INT_MAX, min1 = INT_MAX,x,max = 0;
    scanf("%d",&t);
    int niz[t+1];
    for(i=1;i<=t;i++)
    {
        scanf("%d%d",&n,&m);
        int a[n+1];
        int b[m+1];
        for(j=1;j<=n;j++)
        {
            scanf("%d",&a[j]);
            if(a[j]<min)
                min = a[j];
            else
            {
                if(a[j]>min && a[j]<min1)
                    min1 = a[j];
                if(a[j]>max)
                    max = a[j];
            }
        }
        for(j=1;j<=m;j++)
            scanf("%d",&b[j]);
        x = min1-min;
        while(min< max)
        {
            for(j=1;j<=m;j++)
            {
                if(min==b[j])
                    min+=max;
            }
            min+=x;
        }
        if(min==max)
            niz[i] = 1;
        else
            niz[i] = 0;
        max = 0;
        min = INT_MAX;
        min1 = INT_MAX;
    }
    for(i=1;i<=t;i++)
        printf("%d\n",niz[i]);
}