#include <bits/stdc++.h>
using namespace std;
int T,x;
long long int N,M,g;
int main(){
    cin>>T;
    int nizsuma[T+5];
    int nizsuma1 [T+5];

for(int i=1;i<=T;i++){
            nizsuma[i]=1;
            nizsuma1[i]=1;

            scanf("%lld %lld",&N,&M);
            int niz [N+5];

        for(int j=1;j<=N;j++){
            scanf("%lld",&niz[j]);
            nizsuma[i]=nizsuma[i]*niz[j];
        }


        for(int k=1;k<=M;k++){
            scanf("%lld",&niz[k]);
            nizsuma1[i]=nizsuma1[i]*niz[k];
        }
    }


    for(int r=1;r<=T;r++){
    g=abs(nizsuma[r]-nizsuma1[r]);
    for(x=g-1;x>1;x--){
       if(g%x==0){cout<<"0"<<endl;break;}
    }
    if(x==1){cout<<"1"<<endl;}
    }

    return 0;
}