#include <bits/stdc++.h>
using namespace std;

int n,m;

long long x;

struct slog {
    int br;
    bool provera;
};

slog V[75009];

bool cmp (slog x, slog y){
    return x.br < y.br;
}

int razmak,prvi;

bool check, check1;

int t;

int main () {

    cin>>t;

    while (t--){

        cin>>n>>m;

        check = true ;
        check1 = true;

        for (int i=1;i<=n;i++){
            cin>>x;
            V[i].br = x;
            V[i].provera = 1;
        }

        for (int i=1;i<=m;i++){
            cin>>x;
            V[i+n].br = x;
            V[i+n].provera = 0;
        }

        sort (V+1 , V+n+m+1 , cmp);

        for (int i=1;i<=m+n;i++){
            if (V[i].provera == 1 and V[i-1].provera == 1 and i!=1){
                razmak = V[i].br - V[i-1].br;
                prvi = V[i].br;
                break;
            }
        }
        for (int i=prvi; i<=m+n; i+=razmak){
            if (V[i].provera == 0){
                cout<<0<<endl;
                check1=false;
                check=false;
                break;
            }
            else
            {
                V[i].provera = 0;
            }
        }

        if (check1){
            for (int i=prvi; i<=m+n; i+=razmak){
                if (V[i].provera == 1){
                    cout<<0<<endl;
                    check = false;
                    break;
                }
            }
        }

        if (check){
            cout<<1<<endl;
        }



    }

return 0;
}