#include <iostream>
#include <algorithm>




bool sol(int a, int b, int c)
{
    if(abs(b-a)%c !=0)
    { return true;}
    else {return false;}

}



int main()
{
    int T,q;
    std::cin >> T;
    int* arrc = new int[T];

    for (q = 0; q < T; q++)
    {
       int N[75000];
       int M[75000];
       unsigned int ct_n;
       unsigned int ct_m;

        std::cin >> ct_n >> ct_m;

        for (int i =0; i< ct_n; i++)
        {std::cin >> N[i];}

        for (int i =0; i< ct_m; i++)
        {std::cin >> M[i];}

        std::sort(N, N+ct_n);

        int nm_d = N[1] -N[0];
        for (int i = 2 ; i < ct_n; i++) {

            nm_d = std::min(nm_d, (N[i]-N[i-1]));
        }

        bool state;
        for (int i =0; i < ct_m; i++)
        {
            if(M[i] > N[1])
            {state = sol(M[i], N[1], nm_d);}
            else
            {state = sol(N[1], M[i], nm_d);}

            if (state == false){break;}
        }

        if (state == true)
        {
            arrc[q] = 1;
        }
        else {arrc[q]=0;}

    }

    for (int i =0; i< T; i++){
        std::cout << arrc[i];
    }


    return 0;
}