#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
    //init
    unsigned long long int T, N, M, temp, minEl;
    std::vector<unsigned long long int> A, B;
    bool out;
    std::vector<bool> outArr;
    
    //input
    std::cin >> T;

    for (unsigned int i = 0; i < T; i++)
    {
        //isprazni vectore
        A.clear();
        B.clear();
        out = true;

        //imput
        std::cin >> N >> M;
        for (unsigned int i = 0; i < N; i++)
        {
            std::cin >> temp;
            A.push_back(temp);
        }
        for (unsigned int i = 0; i < M; i++)
        {
            std::cin >> temp;
            B.push_back(temp);
        }

        //sort
        std::sort(A.begin(), A.end());
        std::sort(B.begin(), B.end());

        //najmanja razlika
        auto x = A[N-1];
        for(int i = 0; i < N-1; i++)
        {
            if (x > A[i+1]-A[i])
                x = A[i+1]-A[i];
        }

        minEl = *std::min_element(A.begin(), A.end());
        //provera
        for (unsigned long long int &num : A)
            if ((num-minEl) % x != 0)
                out = false;
        for (unsigned long long int &num : B)
            if ((num-minEl) % x == 0)
                out = false;
        
        outArr.push_back(out);
    }
    for (bool b : outArr)
        std::cout << b << std::endl;
}