#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t,n,m,res;
    long long g,a,b,delj;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        bool bol=1;
        vector <long long> x;
        vector <long long> y;
        vector <long long> poz;
        vector <long long> poz2;
        cin>>n>>m;
        for(int q=0;q<n;q++)
        {
            cin>>g;
            x.push_back (g);
        }
        for(int q=0;q<m;q++)
        {
            cin>>g;
            y.push_back (g);
        }
        if (n>2)
        {
            for(int q=0;q<n;q++)
        {
            g=x[0]-x[q];
            poz.push_back (g);
        }
        int t;
            a = abs(poz[1]);
            b = abs(poz[2]);
            if(b>a)
                swap(a,b);
            while(b != 0)
            {
                t = b;
                b = a % b;
                a = t;
            }
            delj=a;
         for(int q=1;q<n;q++)
         {
             if(abs(poz[q])%a!=0)
                bol=0;
         }
         for(int q=0;q<n;q++)
       {
           if(abs(y[q]-x[0])%a==0)
                bol=0;
       }

        }
        else
        {
            for(int q=0;q<m;q++)
            if(abs(y[q]-x[0])%abs(x[0]-x[1])==0)
                bol=0;

        }
        cout<<bol<<endl;


    }

    return 0;
}