#include <bits/stdc++.h>

using namespace std;

int main()
{
    int T;
    cin >> T;
    int rez[T];
    if (T >= 1 && T <= 5) //uslov
    {
        for (int i = 0; i < T; i++)
        {
            int N, M;
            cin >> N >> M;
            if (N >= 2 && M >= 2 && N <= 75000 && M <= 75000)  //uslovi
            {
                unsigned long long A[N], B[M];
                for (int j = 0; j < N; j++)   //unos
                {
                    cin >> A[j];
                }
                for (int j = 0; j < M; j++)   //unos
                {
                    cin >> B[j];
                }
                sort(A, A + N);
                sort(B, B + M);
                for (int j = 0; j < N - 1; j++)  //uslov
                {
                    if (A[j] == A[j + 1])
                    {
                        return 0;
                    }
                }
                for (int j = 0; j < M - 1; j++)   //uslov
                {
                    if (B[j] == B[j + 1])
                    {
                        return 0;
                    }
                }
                for (int j = 0; j < N; j++)    //uslov
                {
                    for (int k = 0; k < M; k++)
                    {
                        if (A[j] == B[k])
                        {
                            return 0;
                        }
                    }
                }
                for (int j = 0; j < N - 1; j++)   //uslov
                {
                    if (A[j] < 1 || A[j] > 1000000000000000000)
                    {
                        return 0;
                    }
                }
                for (int j = 0; j < M - 1; j++)   //uslov
                {
                    if (B[j] < 1 || B[j] > 1000000000000000000)
                    {
                        return 0;
                    }
                }
                long long X = A[1] - A[0];
                long long VoA = A[0];
                int provera1 = 0;
                while(VoA <= A[N-1])        //Provera A niza
                {
                    if (VoA == A[provera1])
                    {
                        provera1++;
                    }
                    VoA = VoA + X;
                }
                VoA = A[0];
                bool provera2 = true;
                for (int j = 0; j < M; j++)         //provera B niza
                {
                    if (!provera2)
                    {
                        break;
                    }
                    for (int k = A[0]; k <= B[M-1]; k += X)
                    {
                        if (B[j] == k)
                        {
                            provera2 = false;
                            break;
                        }
                    }
                }

                if (provera2 && provera1 == N)          //cuvanje rezultata
                {
                    rez[i] = 1;
                }
                else
                {
                    rez[i] = 0;
                }
            }
            else
            {
                return 0;
            }

        }
        for(int i = 0; i<T ; i++)           //prikaz rezultata
        {
            cout << rez[i] << endl;
        }
    }
    else
    {
        return 0;
    }
}