#include <iostream>

using namespace std;


int main()
{
    int a, b, c;
    cin>>a;
    int d[9][74999];
    int i = 0;
    while(i <a){
        cin>>b>>c;
        for(int j = 0; j < b; j++){
            cin>>d[i][j];
        }
        for(int n = 0; n < c; n++){
            cin>>d[i][n];
        }
        i = i + 2;
    }
    i = 0;
    while (i < a){
        int razlika = d[i][1] - d[i][0];
        bool c = true;
        for(int j = 0; j < int(sizeof d[i] / sizeof(int)); j++){
            if(d[i][j+1] - d[i][j] != razlika && (d[i][j+1]-d[i][j]) % razlika != 0 ){
                c = false;
            }
        }
        for(int n = 0; n < int(sizeof d[i+1] / sizeof(int)); n++){
            if((d[i+1][n] - d[i][0]) % razlika == 0){
                c = false;
            }
        }
        if(c){
            cout<<1;
        }
        else{
            cout<<0;
        }
        i = i + 2;
    }
    return 0;

}